diff --git a/Chapter_2/ex0.py b/Chapter_2/ex0.py new file mode 100644 index 0000000000000000000000000000000000000000..b1099024f97b7bde0a03babebe98b0522bc62f78 --- /dev/null +++ b/Chapter_2/ex0.py @@ -0,0 +1,24 @@ + +n_lines = int(input("Combien de lignes desirez-vous ?")) + +#Méthode 1 : en multipliant des strings +line_id = 0 +while line_id < n_lines: + print ((n_lines-line_id-1)*" "+(2*line_id+1)*"*") + line_id += 1 + + + +#Méthode 2 : Boucles imbriquées +line_id = 0 +while line_id < n_lines: + col_id = 0 + line_txt = "" + while col_id < n_lines + line_id: + if col_id < n_lines-line_id-1: + line_txt+=" " + else: + line_txt+="*" + col_id += 1 + print(line_txt) + line_id += 1 diff --git a/Chapter_2/ex1.py b/Chapter_2/ex1.py new file mode 100644 index 0000000000000000000000000000000000000000..f31df843df31f002f376fbfed170006577ea2828 --- /dev/null +++ b/Chapter_2/ex1.py @@ -0,0 +1,13 @@ + +u_m2 = 1 +u_m1 = 1 + +print ("1 : 1") +print ("2 : 1") +i = 0 +while i < 18: + u_m = u_m2+u_m1 + u_m2 = u_m1 + u_m1 = u_m + print ("{0} : {1}".format(i+2,u_m)) + i += 1 diff --git a/Chapter_2/ex2.py b/Chapter_2/ex2.py new file mode 100644 index 0000000000000000000000000000000000000000..a14d31b5dfdb4675c076333b7b05cd2e4cb19f3b --- /dev/null +++ b/Chapter_2/ex2.py @@ -0,0 +1,20 @@ +import math + + +nombre = int(input("Entrez un nombre : ")) +#On va utiliser la méthode brute-force... + +div_candidat = 2 +est_premier = True +while div_candidat <= math.sqrt(nombre): + division = nombre/div_candidat + if division == int(division): + est_premier = False + print("{0} n'est pas premier (il est divisible par {1})".format(nombre, div_candidat)) + break + div_candidat += 1 +if nombre == 1: + print ("1 n'est pas premier (il n'a qu'un seul diviseur)") +elif est_premier: + print ("{0} est premier !".format(nombre)) + diff --git a/Chapter_2/ex2_v2.py b/Chapter_2/ex2_v2.py new file mode 100644 index 0000000000000000000000000000000000000000..ab97fba57085c400e6953ebdaebfbdb3cfba3881 --- /dev/null +++ b/Chapter_2/ex2_v2.py @@ -0,0 +1,19 @@ +import math + +#Toujours en brute-force +n_premiers = 0 +nombre = 2 +while n_premiers <= 1000: + div_candidat = 2 + est_premier = True + while div_candidat <= math.sqrt(nombre): + division = nombre/div_candidat + if division == int(division): + est_premier = False + break + div_candidat += 1 + if est_premier: + n_premiers += 1 + print ("{0}/1000 : {1}".format(n_premiers,nombre)) + nombre += 1 + diff --git a/Chapter_2/ex3.py b/Chapter_2/ex3.py new file mode 100644 index 0000000000000000000000000000000000000000..0b2de1c0865d29c90ef327072fd0707175243580 --- /dev/null +++ b/Chapter_2/ex3.py @@ -0,0 +1,15 @@ +import math + +step = 1 +min_step = 1e-10 +x = 0 + +while step >= min_step: + while math.exp(-0.5*x*x) > 0: + x += step + x -=step + step/=10 + +print ("Le nombre le plus petit est : {0}".format(math.exp(-0.5*x*x))) +print ("Vient de e^(-0.5*x**2) avec x = {0}".format(x)) + diff --git a/Chapter_2/ex4.py b/Chapter_2/ex4.py new file mode 100644 index 0000000000000000000000000000000000000000..d317dc4b40904620323307ef9c30c48459f47b4e --- /dev/null +++ b/Chapter_2/ex4.py @@ -0,0 +1,17 @@ + +n_player = int(input("Combien de joueurs participent ? ")) +n_rounds = int(input("Combien de tours de jeu ? ")) + +n_played = 0 +while n_played < n_rounds: + n_played += 1 + to_say = "" + if n_played % 5 == 0: + to_say+="ding-ding " + if n_played % 7 == 0: + to_say+="bottle " + if to_say=="": + to_say=str(n_played) + + to_say = "Joueur {0} : {1}".format(n_played%n_player,to_say) + print(to_say)