In [2]: for i in range (10):
for j in range (i):
print('*',end=' ')
print( )
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
* * * * * * * * *
In [3]: #rectangle
def rectangle(L,C):
for i in range(L):
for j in range(C):
print('*',end=' ')
print()
In [4]: #triangle
def triangle(B,H):
for i in range(B):
for j in range(i):
print('*',end=' ')
print( )
In [5]: #triangle renversé
def triangleren(L):
for i in range(L):
for j in range(L):
if j>=i:
print('*',end=' ')
else:
print(' ',end=' ')
print( )
In [6]: #fonction recursive; exemple du factoriel
def factoriel(n):
if n==0:
return 1
if n<0:
print ('entrez un nombre positif')
if n==1:
return 1
else:
return(n*factoriel(n-1))
In [7]: #inverse d'un entier
def inverse(a,b):
L1 = [a, 1, 0]
L2 = [b, 0, 1]
while L2[0] != 0:
q = L1[0] // L2[0]
L=L2[:]
for i in range (0,3):
L2[i] = L1[i] - q*L2[i]
L1 = L[:]
if L1[0] < 0:
L1 = [-L1[0], -L1[1], -L1[2]]
return L1
In [8]: #la somme
def somme(n):
S = 0
for i in range(n):
S=S+i*2
return S
In [*]: #renverser un nombre
def renverse(n):
inv=0
while n>0:
inv=(n//10)+(inv*10)
n=n//10
return inv
In [*]: #cryptage de cesar
def cryptage_cesar(w,k):
#w est un type string
w=''
#len calcule la longueur d'un string
for i in range(len(w)):
#ord donne ASCII d'un caractère
#1ére fonction calcule les lettres miniscules
if ord(m[i]) >= 65 & ord(m[i]) <= 90:
#fonction de cryptage
x = (ord(m[i]) - 65 + k) % 26
#le string crypté
y = chr(x + 65)
w = w + y
#2éme focntion calcule les letteres majiscules
elif ord(m[i]) >= 97 & ord(m[i]) <= 122:
#fonction de cryptage
x = (ord(m[i]) - 97 + k) % 26
#le string crypté
y = chr(x + 97)
w = w + y
return w
In [*]: #decryptage de cesar
def decryptage_cesar(w,k):
#w est un type string
w=''
#len calcule la longueur d'un string
for i in range(len(w)):
#ord donne ASCII d'un caractère
#1ére fonction calcule les lettres miniscules
if ord(m[i]) >= 65 & ord(m[i]) <= 90:
#fonction de cryptage
x = (ord(m[i]) - 65 - k) % 26
#le string crypté
y = chr(x + 65)
w = w + y
#2éme focntion calcule les letteres majiscules
elif ord(m[i]) >= 97 & ord(m[i]) <= 122:
#fonction de cryptage
x = (ord(m[i]) - 97 - k) % 26
#le string crypté
y = chr(x + 97)
w = w + y
return w
In [*]: #calcule de pgcd
def pgcd(a,b):
while b != 0:
a, b = b, a % b
return a
In [*]: #liste des inverses
def inversible(n):
L=[]
for i in range(1, n):
if pgcd(n,i)==1:
L=[Link](i)
return L
In [*]: #Groupe inversible
def groupe_inversible(n):
L=[]
h=[]
for i in range(1, n):
for j in range(1, n):
if (i*j)%n==1:
x=i
[Link](i)
for i in L:
if i not in h:
[Link](i)
return h
In [*]: #si un nombre est premier ou pas
from math import sqrt
from math import ceil
def premier (n):
if n <= 1:
return False
for i in range (2, ceil(sqrt(n))):
if n % i == 0:
return False
return True
In [*]: #les facteurs d'un nombre
def facteurs(n):
f = []
for i in range (1, n+1):
if n % i == 0:
[Link](i)
return f
In [*]: #les facteurs premiers
def f_premier(n):
f = facteurs(n)
g = []
for i in f:
if premier(i) == True:
[Link](i)
return g
In [*]: #la fonction d'Euler
from math import floor
def phi(n):
T = f_premier(n)
m = 1
for i in T:
m = m * (1 - 1/i)
E = m * n
return E
In [ ]: #Bezout
def bezout(p, q):
L1 = [p, 1, 0]
L2 = [q, 0, 1]
while L2[0] > 0:
q = L1[0] // L2[0]
L = L2[:]
for i in range (0, 3):
L2[i] = L1[i] - q*L2[i]
L1 = L
return L1