Questions complémentaires

Questions complémentaires

The Greatest Divisor of a number a is the biggest number ( except a itself) such that the division of a by this natural is an entire division.

Since \(0\) is divisible by any natural this may cause some problems if you will look for the bigger one, so we expect you to return None.

Recall that the operator % returns the remainder of the Euclidian division.


        
        

Suppose you now have base € on your savings account.

If you leave it x years on that account at a fixed cummulative interest rate of y %, what will be the balance on your account after these x years?

Return that amount.


        
        

The Greatest Common Divisor of two natural numbers a and b is the biggest natural number k such that the division of a and b by this natural k is an entire division.

Euclid found a very simple recursive algorithm to find the GCD of two numbers: $$\text{gcd}(a,0) = a$$ $$\text{gcd}(a,b) = \text{gcd}(b,a\%b)$$

Recall that the operator % returns the remainder of the euclidian division.


        
        

In mathematics, the Fibonacci series are the numbers in the following sequence of integers, which is characterized by the fact that every number after the first two is the sum of the two preceding ones:

/Example: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, ...

A very simple algorithm would be: $$\text{F}_0 = 0$$ $$\text{F}_1 = 1$$ $$\text{F}_n = \text{F}_{n-1} + \text{F}_{n-2}$$

Create a function fibonacci(n) that prints the nth element of the Fibonacci sequence.


        
        

The Body mass index (BMI) or Quetelet index, used by dieticians, is indicative of the condition of a person.

A normal person should have an index between 20 and 25. Below 20, she is considered thin, above 25 overweight. Beyond 30, the person is obese!

This index is calculated as the ratio between a person's weight, expressed in kg, and the square of the person's height, expressed in meters.

Create a function quetelet(height, weight) to calculate a person's Quetelet index and return thin string for a person with an index strictly less than 20, normal for a person whose index is between 20 and 25, overweight if the index is strictly greater than 25 and less than or equal to 30 and obese when it is strictly greater than 30.


        
        

The police is hiring you to develop a program to calculate the fine that a car driver will have to pay in the event of a violation. The law stipulates that the driver will have to pay 5 euros per km/h above the maximum authorized speed, with a minimum fine of 12.5€.

For any speeding of more than 10 km/h above the authorized speed, the fine is DOUBLED!

Your program takes as input the maximum allowed speed and the actual speed of the car. It calculates the possible fine.

Example:

  • If the maximum speed is 50km/h and the vehicle is traveling at 62km/h, the penalty will be 12 * 5 * 2 = 120€.
  • If the maximum speed is 50km/h and the vehicle is traveling at 56km/h, the penalty will be 6 * 5 = 30€.
  • If the maximum speed is 50km/h and the vehicle is traveling at 51km/h, the fine will be 12.5€.


        
        

Les équations du second degré sont des équations de la forme suivante:

ax2 + bx + c = 0

avec a ≠ 0

Pour déterminer si l'équation dispose d'une solution, on calcule le nombre ρ = b2 − 4ac. Si ρ est strictement positif, l'équation a exactement deux solutions. La première solution s'obtient via la formule suivante :

( − b + (ρ))/(2a)

Et la seconde racine s'obtient via la formule suivante :

( − b − (ρ))/(2a)

Si ρ est nul, l'équation a exactement une solution, dont la valeur est égale à  − b ⁄ (2a). Si ρ est négatif, l'équation n'a aucune solution.

Pour montrer que vous maîtrisez la décomposition en sous-problèmes, vous devrez définir trois fonctions. Chacune d'entre elles prendra uniquement comme paramètres les valeurs de a, b et c. Tout d'abord, définissez et implémentez la fonction rho, qui retourne la valeur du nombre ρ.

Ensuite, définissez et implémentez la fonction n_solutions, qui retourne le nombre de solutions de l'équation définie par a, b et c.

Finalement, définissez et implémentez la fonction solution, qui retourne la solution d'une équation qui n'a qu'une seule solution et la plus petite solution d'une équation qui a deux solutions. La fonction ne sera jamais appelée pour une équation qui ne dispose pas de solution.

Les fonctions n_solutions et solution doivent impérativement faire appel à la fonction rho .

Pour résoudre l'exercice, vous aurez besoin de la fonction racine_carree, décrite ci-dessous. Cette fonction est déjà implémentée, vous ne devez donc pas l'écrire vous-même.

def racine_carree(n):
"""
Calcule une racine carree
pre: n est un nombre réel
     n >= 0
post: retourne la racine carrée réelle de n
"""

Voici un exemple d'utilisation des fonctions que vous devez implémenter, avec l'équation x2 + 2x + 1 = 0 :

rho(1, 2, 1)
>>> 0

n_solutions(1, 2, 1)
>>> 1

solution(1, 2, 1)
>>> -1.0