



################
##            ##
## Exercice 3 ##
##            ##
################
print('\nExercice 3\n')

from random import randint


def noter():
    return randint(0,20)


def noter():
    return randint(0,40)/2



def noter_gentil(n):
    meilleur_note = noter()
    i = 1
    while i<n:
        note = noter()
        if note > meilleur_note:
            meilleur_note = note
        i = i+1
    return meilleur_note

i=0
while i<5:
    print("note numéro", i, ":" , noter_gentil(5))
    i = i+1





def moyenne(k,n):
    somme = 0
    i = 0
    while i<k:
        somme = somme + noter_gentil(n)
        i = i+1
    return somme/k


i=1
while i<=10:
    print("Moyenne pour", i, "tentatives :", moyenne(300,i))
    i = i+1










################
##            ##
## Exercice 4 ##
##            ##
################
print('\nExercice 5\n')

def étoile():
    print("*", end="")

def dièse():
    print("#", end="")

def nouvelle_ligne():
    print()

def frise(n):
    i=0
    while i<n:
        if i%2==0:
            dièse()
        else:
            étoile()
        i=i+1
    nouvelle_ligne()




i=0
while i<11:
    print("i =", i, end=" ")
    frise(i)
    i = i+1



################
##            ##
## Exercice 5 ##
##            ##
################
print('\nExercice 5\n')

def le(n):
    if n==0 or n==1:
        return 0
    else:
        return 1+le(n//2)

assert le(0) == 0  # par définition
assert le(1) == 0  # par définition
assert le(3) == 1  # 3 -> 1
assert le(5) == 2  # 5 -> 2 -> 1
assert le(11) == 3 # 11 -> 5 -> 2 -> 1


################
##            ##
## Exercice 6 ##
##            ##
################
print('\nExercice 6\n')

## Question 6-1

def affiche_calcul_binaire(n) :
    while n >= 2:
        q = n//2
        r = n%2
        print(r , 'car' , n , '=' , '2 ×' , q ,'+', r )
        n = q
    print(n)

affiche_calcul_binaire(13)


## Question 6-2

def affiche_binaire_récursive(n) :
    if n>1 :
        affiche_binaire_récursive(n//2)
    print(n%2 , end='')

# Pour le retour à la ligne final qui ne peut pas être fait
# au cours des appels récursifs
def affiche_binaire(n):
    affiche_binaire_récursive(n)
    print()

affiche_binaire(13)


## Question 6-3



# Pour être sûr de bien comprendre comment fonctionne un calcul,
# n'hésitez pas à utiliser Thonny et son exécution pas à pas.
def affiche_binaire_impérative(n) :
    binaire = 0
    i = 0
    while n > 0 :
        bit = n%2
        binaire = binaire + 10**i * bit # Pourquoi ?
        i = i+1
        n = n//2
    print(binaire)

affiche_binaire_impérative(13)



################
##            ##
## Exercice 7 ##
##            ##
################
print("\nExercice 7\n")


# On suppose n!=0 et d>1
# On regarde combien de fois d divise n
def multiplicité(n,d):
    mult = 0
    while n%d==0:
        mult = mult+1
        n = n//d
    return mult


def affiche_facteurs(n):
    if n==0 or n==1:
        produit=str(n)
    else:
        produit=''
        d=2
        while n>1:
            p = multiplicité(n,d)
            if p!=0:
                produit = produit  + str(d) + "**" + str(p) + " "
            n=n//(d**p) # Les parenthèses sont ici facultatives
            d=d+1
    print(produit)

affiche_facteurs(1176)


def affiche_facteurs(n):
    plus='' # Pour le premier facteur, on n'affiche pas le symbole +
    if n==0 or n==1:
        produit=str(n)
    else:
        produit=''
        d=2
        while n>1:
            p = multiplicité(n,d)
            if p!=0:
                produit = produit + plus + str(d) + "**" + str(p)
                plus=" + " # à partir de maintenant, on affichera les +
            n=n//(d**p) # Les parenthèses sont ici facultatives
            d=d+1
    print(produit)
affiche_facteurs(1176)
