$\require{color}$ $\require{xcolor}$ $\newcommand{\myfbox}[1]{\fcolorbox{red}{white}{$\textrm{#1}$}}$ $\require{stmaryrd}$ $\newcommand{\ient}{[\![}$ $\newcommand{\fient}{]\!]}$ $\newcommand{\R}{\mathbb R}$ $\newcommand{\K}{\mathbb K}$ $\newcommand{\N}{\mathbb N}$ $\newcommand{\C}{\mathbb C}$ $\newcommand{\id}{\operatorname{Id}}$ $\newcommand{\mat}{\operatorname{mat}}$ $\newcommand{\sp}{\operatorname{Sp}}$ $\newcommand{\In}{\operatorname{I}}$ $\newcommand{\vect}{\operatorname{Vect}\ }$ $\newcommand{\rg}{\operatorname{rg}}$ $\newcommand{\tr}{\operatorname{Tr}}$ $\newcommand{\dis}{\displaystyle}$ $\renewcommand{\Im}{\operatorname{Im}}$ $\newcommand{\im}{\operatorname{Im}}$ $\newcommand{\bordermatrix}[3]{ \begin{matrix} \begin{matrix}#1\end{matrix} & \\ #3 & \hspace{-1em} \begin{matrix}#2\end{matrix} \end{matrix}}$ $\newcommand{\fonction}[5]{#1\ \colon \left\{\begin{array}{ccl}#2 & \longrightarrow & #3\\#4 & \longmapsto & #5\end{array}\right.}$ $\newcommand{\revdots}{\mathinner{\mkern1mu\raise1pt{\kern7pt\hbox{.}}\mkern2mu\raise4pt\hbox{.}\mkern2mu\raise7pt\hbox{.}\mkern1mu}}$ $\newcommand{\q}[1]{{\bf Q #1}\rhd}$
import matplotlib.pyplot as plt
$\q{1}$ On peut par exemple écrire la fonction suivante :
def carre(n):
return abs(n**0.5 - int(n**0.5)) < 1e-10
carre(25)
True
carre(42)
False
$\q{2}$ On représente graphiquement les couples demandés :
X = []
Y = []
for x in range(1001):
for y in range(1001):
if carre(x**2 + y**2):
X.append(x)
Y.append(y)
plt.scatter(X, Y, s=5)
plt.axis("equal")
plt.show()
$\q3$ On adapte le script précédent :
def pgcd(a, b):
assert (a >= b)
while b > 0:
a, b = b, a % b
return a
X = []
Y = []
for x in range(1001):
for y in range(x+1):
if carre(x**2 + y**2) and pgcd(x,y)==1:
X.append(x)
X.append(y)
Y.append(y)
Y.append(x)
plt.scatter(X, Y, s=5)
plt.axis("equal")
plt.show()
$\q4$ On utilise des listes par compréhension (ce qui a également l'avantage de ne pas provoquer d'effet de bord) :
def rajouter_lancer(lancers, de):
return [L + [i] for L in lancers for i in range(1, de + 1)]
rajouter_lancer([[3, 5], [6, 1]], 4)
[[3, 5, 1], [3, 5, 2], [3, 5, 3], [3, 5, 4], [6, 1, 1], [6, 1, 2], [6, 1, 3], [6, 1, 4]]
$\q5$ On utilise la fonction précédente :
def liste_lancers(jeu):
L = [[]]
for de in jeu:
L = rajouter_lancer(L, de)
return L
liste_lancers([4, 2])
[[1, 1], [1, 2], [2, 1], [2, 2], [3, 1], [3, 2], [4, 1], [4, 2]]
$\q6$ On utilise la fonction précédente :
def resultats(jeu):
return [sum(L) for L in liste_lancers(jeu)]
resultats([4, 2])
[2, 3, 3, 4, 4, 5, 5, 6]
$\q7$ On construit le dictionnaire comme lors du TP correspondant :
def occurrences(jeu):
dico = {}
for r in resultats(jeu):
if r in dico:
dico[r] += 1
else:
dico[r] = 1
return dico
dico = occurrences([4, 2])
dico
{2: 1, 3: 2, 4: 2, 5: 2, 6: 1}
$\q8$ On modifie la fonction précédente de la façon suivante :
def probas(jeu):
dico = {}
R = resultats(jeu)
cst = 1 / len(R)
for r in R:
if r in dico:
dico[r] += cst
else:
dico[r] = cst
return dico
dico = probas([4, 2])
dico
{2: 0.125, 3: 0.25, 4: 0.25, 5: 0.25, 6: 0.125}
$\q9$ On trace la courbe des probabilités demandée :
jeu = [6, 6, 6]
P = probas(jeu)
X = P.keys()
Y = P.values()
plt.plot(X, Y, "o")
plt.show()
$\q{10}$
def proba_cumul_inv(jeu):
dico = probas(jeu)
return {cle : sum([dico[p] for p in dico if p>=cle]) for cle in dico}
dico=proba_cumul_inv([4,2])
dico[3]
0.875
$\q{11}$
jeu = [6, 6, 6]
P = proba_cumul_inv(jeu)
X = P.keys()
Y = P.values()
plt.plot(X, Y, "o")
plt.show()
$\q{12}$
types_des = [4, 6, 8, 12, 20]
def rajouter_de(jeux):
return [L + [i] for L in jeux for i in types_des]
rajouter_de([[4, 6], [8, 4]])
[[4, 6, 4], [4, 6, 6], [4, 6, 8], [4, 6, 12], [4, 6, 20], [8, 4, 4], [8, 4, 6], [8, 4, 8], [8, 4, 12], [8, 4, 20]]
def liste_jeux(n):
L = [[]]
for k in range(n):
L = rajouter_de(L)
return L
len(liste_jeux(3))
125