Algorithme Python Récursive
Fonction fact(X:entier) :entier def fact(X) :
Début F=1
F←1 For i in range(2,X+1) :
Pour i de 2 à X faire F=F*i
F←1 Return F
Fin pour
Retourner F
Fin
Fonction premier (X :entier) :booléen def premier (X) :
Début i=2
i←2 while(X %i !=0) and(i<X//2+1) :
tant que (X mod i ≠0) et (i<X div 2+1) i=i+1
faire return i==X//2+1
i←i+1
Fin tant que
Retourner (i==X div2+1)
Fin
Fonction fact_premier (X :entier) :chaine def fact_premier(X) :
Début i=2
i←2 Ch=""
ch← "" while(X !=1) :
tant que (X ≠1) faire if X%i==0 :
si X mod i=0 alors X=X//i
X←X div i if ch== "" :
si ch←"" alors Ch= str(i)
ch= convch(i) else :
si non Ch=Ch+"*"+ str(i)
ch←ch+"*"+ convch(i)
else :
fin si
i=i+1
si non
return ch
i=i+1
fin si
fin tant que
retourner ch
fin
fonction som_chiffres(X :entier ) :entier def som_chiffres(X) :
Début ch=str(X)
ch ←convch(X) S=0
S ←0 for i in range(0,len(ch)) :
Pour i de 0 à long(ch)-1 faire S=S+int(ch[i])
S ←S+valeur(ch[i]) return S
fin pour
retourner S
fin
fonction PGCD(a :entier ; b ;entier) :entier def PGCD(a,b)) :
Début while (a !=b) :
Tant que (a≠b) faire if a>b :
Si a >b alors a=a-b
a←a-b else :
si non b=b-a
b←b-a
fin si return a
fin tant que
retourner a fin
fonction PPCM(a :entier ; b ;entier) :entier def PPCM(a,b) :
Début m=a
m←a while (m%b !=0) :
Tant que (m mod b≠0) faire m=m+a
m=m+a return m
fin tant que
retourner m
fin
fonction suite_fib(n:entier) :entier def suite_fib(n) :
début U1=1
U1←1 U2=1
U2←2 for i in range(3,n+1) :
Pour i de 3 à n faire U= U1+ U2
U←+U2+U1 U1= U2
U1←U2 U2= U
U2 ←U Return U
Fin pour
Retourner U
Fin
fonction nombre_or(eps:réel) :réel def nombre_or (eps) :
Début V1=1
V1←1 V= suite_fib(3)/ suite_fib(2)
V← suite_fib(3)/ suite_fib(2) i =4
i ←4 while (abs((V-V1)>eps) :
Tant que (abs(V-V1)>eps) faire V1=V
V1←V V=suite_fibo(i+1)/ suite_fibo(i)
V← suite_fibo(i+1)/suite_fibo(i) i=i+1
i ←i+1 Return U
fin tant que
retourner V
fin
fonction Conv10_b(x,b :entier) :chaine def Conv10_b(x,b) :
Début V1=""
ch← "" while (x!=0) :
Tant que (x ≠0) faire if x %b >=10:
Si x mod b>=10 alors c=chr(55+ x% b)
c← chr(55+x mod b) else:
si non c=str(x%b)
c← convch(x mod b) ch=c+ch
fin si x=x //b
ch ←c+ch Return ch
x ← x div b
fin tant que
retourner ch
fin
Fonction distinct(ch :chaine) :booléen : def dist(ch):
Debut while(ch!="")and(ch[1:].find(ch[0])==-1):
Tantque (ch ≠"") et (pos(ch[0],sous(ch,1,len(ch))=- ch=ch[1:]
1)faire return ch==""
Ch←souschaine(ch,1,long(ch))
Fin tant que
Retourner ch=""
fin
Fonction air_rect(a,b :réel ; n :entier) :reél def air_rect(a,b ,n ) :
Debut Som = 0
Som ← 0 h =(b-a)/n
h ←(b-a)/n x=a+h
x←a+h for k in range(1,n+1) :
Pour k de 1 à nFaire Som← Som + f(x)
Som← Som + f(x) x←x+h
x←x+h
Fin Pour return Somme * h
retourner Somme * h
Fin
Fonction air_Trap (a, b : Réel ; n : Entier ) : Réel def air_Trap(a,b ,n ) :
Debut h =(b-a)/n
h ← (b-a)/n Som =(f(a)+f(a+h))/2
Som← (f(a)+f(a+h))/2 x=a+h
x ← a1 for k in range(1,n-1) :
Pour k de 1 à n- Faire x←x+h
x←x+h Som← Som + (f(x)+f(x+h))/2
Som ← Som + (f(x)+f(x+h))/2 return Som
Fin Pour
Retourner Som
fin
Proce/fon traitement (f : fiche) def traitement (f : fiche) :
Début ch= nom_logique.readline()
Lire ligne(f,ch) While ch != "" :
Tant que (ch != "") faire Traitement
Traitement ch = nom_logique.readline()
Lire ligne(f,ch)
Fin tant que
Fin
def traitement (f : fiche)
Fin_fichier = False
while not (Fin_fichier) :
try :
x = load (Nom_logique)
except :
Fin_fichier = True