2012-2013/TD_maple/DS1/correction.py

191 lines
3.5 KiB
Python

#!/usr/bin/env python
#-*- coding:utf8-*-
# ------------------------------
# Imports
# ------------------------------
from math import sqrt, sin
# ------------------------------
# Fonctions
# ------------------------------
# --------------------------
# Exo 1
#
def divProp(n):
""" divProp
Renvoie les diviseurs propres de n
"""
divP = []
for i in range(int(n/2)):
if not (n%(i+1)):
divP += [(i+1)]
return divP
def divPropBis(n):
""" divProp
Renvoie les diviseurs propres de n
a
"""
divP = []
for i in range(int(sqrt(n))):
if not (n%(i+1)):
divP += [(i+1), (int(n/(i+1)))]
return divP
def SumDivProp(n):
""" SumDivProp
Retourne la somme des diviseurs propres
"""
return sum(divProp(n))
def nbrParfait(n):
""" nbrParfait
Retourne les nombres parfaits inferieur à n
"""
nbrParf = []
for i in range(n):
if SumDivProp((i+1)) == (i+1):
nbrParf += [(i+1)]
return nbrParf
def nbrAmis(n):
""" nbrAmis
Retourne les nombres amis inferieurs à n
"""
L = set()
for i in range(n):
I = SumDivProp((i+1))
if ((i+1) == SumDivProp(I)):
L = L.union({(max((i+1),I), min((i+1),I))})
return L
# --------------------------
# Exo 2
#
def calf(x, a):
""" calf
Return value of f at x
"""
val = x**4 + 7*x**3 + 2*x**2 + x - 7/x - a
return val
def cherche0(a, pas, start = 1):
""" cherche0
Cherche le n tel que la fonction x^4 + 7x^3 + 2x^2 + x - 7 - 1/x - a passe de negatif à positif
@param a: le parametre
@param pas: la puissance de 10 pour les pas
@param start: à partir d'ou commencer la recherche
"""
# print("New")
# print("Pas: ", pas)
step = calf(start, a)
i = 0
while step < 0:
i += 1
step = calf((start + 10**pas * i), a)
# print(step)
# print(calf((start + 10**pas * (i-1)), a))
# print('i: ', str(i))
if pas == 0:
return i
else:
return 10**pas * (i-1) + cherche0(a, (pas-1), start + 10**pas * (i-1))
# --------------------------
# Exo 3
#
def evalu(n):
""" evalu
Fonction récurcive qui calcul u_n définie par u_n = sin (u_n{n-1}) et u_0 = 1
"""
if n == 0:
return 1
else:
return sin(evalu(n-1))
def cherche_rang(e):
""" cherche_rang
Determine le n minimal pour que u_n < e
"""
u = 1
i = 0
while (u > e):
u = sin(u)
i += 1
return i
# ------------------------------
# Bloc principal
# ------------------------------
if __name__ == '__main__':
#n = 21236789
#print(divProp(100))
#print(divPropBis(n))
#print(SumDivProp(30))
#print(nbrParfait(10000))
#print(nbrAmis(6000))
#print(cherche0(42, 1, 1))
print(cherche0(42**42, 17, 1))
a = 42**42
# n = cherche0(a, 17, 1)
# print(n)
# print(calf(n-1,a))
# print(calf(n,a))
# for i in range(10):
# print(n+i)
# print(calf(n+i,a))
n = 110692335104026962
print(n)
print(calf(n-5,a))
print(calf(n-1,a))
print(calf(n,a))
print(calf(n+1,a))
#print(evalu(1))
#print(cherche_rang(0.007))
# ------------------------------
# Fin du programme
# ------------------------------
# -----------------------------
# Reglages pour 'vim'
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
# cursor: 16 del