<jupyter_start><jupyter_text>Factorielle<jupyter_code>def factorielleIt(n):
    r = 1
    for i in range(1,n+1):
        r *=i
    return r
factorielleIt(8)
def factorielleRec(n):
    if n == 0:
        return 1
    return n * factorielleRec(n-1)
factorielleRec(3)
def factorielleAffichage(n):
    print("Début appel " + str(n))
    if n == 0:
        print ("Fin appel 0")
        return 1
    p = factorielleAffichage(n-1)
    print("Valeurs prec : " + str(p))
    print("Fin appel " + str(n))
    return n * p
factorielleAffichage(3)<jupyter_output>Début appel 3
Début appel 2
Début appel 1
Début appel 0
Fin appel 0
Valeurs prec : 1
Fin appel 1
Valeurs prec : 1
Fin appel 2
Valeurs prec : 2
Fin appel 3<jupyter_text>Syracuse<jupyter_code>def syr(n):
    print(n)
    if n == 1:
        return
    if n%2==0:
        syr(n//2)
    else:
        syr(3*n+1)
syr(5)<jupyter_output>5
16
8
4
2
1<jupyter_text>Hanoi<jupyter_code>def print_hanoi_actions(n, t1, t2, t3):
    if n == 0:
        return
    print_hanoi_actions(n-1,t1,t3,t2)
    print("Move from " + t1 + " to " + t2)
    print_hanoi_actions(n-1,t3,t2,t1)
print_hanoi_actions(8,"Tower 1","Tower 2","Tower 3")
class Towers:
    
    def __init__(self,n):
        self.towers = (list(range(n,0,-1)),[],[])
        self.size = n
    
    def move(self,i,j):
        t1,t2 = self.towers[i], self.towers[j]
        if len(t1) == 0 or (len(t2) != 0 and t1[-1]>t2[-1]):
            raise ValueError("Invalid action")
        print("Move from Tower " + str(i) + " to Tower " + str(j))
        t2.append(t1.pop())
    
    def pretty_print(self):
        res = ""
        n = self.size
        for i in range(n):
            line = ""
            for t in self.towers:
                if len(t) > i:
                    v = t[i]
                else:
                    v = 0
                stars = v*"*"
                spaces = (n-v)*" "
                line+= spaces + stars + "|" + stars + spaces + " "
            line+="\n"
            res = line + res
        return res

        
    
    
T = Towers(3)
print(T.pretty_print())
T.move(0,1)
print(T.pretty_print())
T.move(0,1)
print(T.pretty_print())
def hanoi(n, T, t1, t2, t3):
    print("Debut appel ",n,t1,t2)
    if n == 0:
        print("Fin appel 0")
        return
    hanoi(n-1,T,t1,t3,t2)
    T.move(t1,t2)
    print(T.pretty_print())
    hanoi(n-1,T,t3,t2,t1)
    print("Fin appel ",n,t1,t2)
n = 3
T = Towers(n)
print(T.pretty_print())
hanoi(n,T,0,1,2)
T = Towers(4)
print T.pretty_print()
hanoi(4,T,0,1,2)<jupyter_output><empty_output><jupyter_text>TD  Exercice 1<jupyter_code>def affiche(n,i):
    if n == i:
        print(n)
        print("*")
    else:
        print(i)
        affiche(n,i+1)

affiche(5,1)<jupyter_output>1
2
3
4
5
*<jupyter_text>Exercice 2<jupyter_code>def f1(n):
    if n==0:
        return 1
    return f1(n+1)
f1(-6)
def f2(n):
    if n==0:
        return 0
    return f2(n-1) + n
f2(5)
def f3(n):
    if n==0:
        return 0
    return f3(n-1) - n
f3(5)
def f4(n):
    if n==0:
        return 0
    if n<0:
        return n + f4(-n)
    return n + f4(-n+1)
f4(-5)
def f5(n):
    if n<=1:
        return 0
    return 1 + f5(n-2)
f5(12)
sum(range(101))
def fiboRec(n):
    if n == 0 or n == 1:
        return 1
    return fiboRec(n-1) + fiboRec(n-2)
fiboRec(40)
def fiboIt(n):
    u0 = 1
    u1 = 1
    for i in range(2,n+1):
        u1, u0 = u0 + u1, u1
    return u1
fiboIt(12000)
def pgcd(a,b):
    if a < b:
        return pgcd(b,a)
    if a%b == 0:
        return b
    return pgcd(b,a%b)
pgcd(2145,630)
pgcd(7,15)
def dicho(T, deb, fin, v):
    if fin - deb <= 0:
        return -1
    m = (deb+fin)//2
    if T[m] == v:
        return m
    if v < T[m]:
        return dicho(T,deb,m,v)
    return dicho(T,m+1,fin,v)
    
dicho([2,4,6,9,12],0,5,5)
def fiboC(n):
    if n == 0 or n == 1:
        return 1,1
    f0,c0 = fiboC(n-1)
    f1,c1 = fiboC(n-2)
    return  f0+f1, c0+c1+1
fiboC(7)
def test(n):
    if n == 0:
        return 1
    return 1 + test(n//2) + test(n//2)
test(8)
def puiss1(a,b):
    if b == 0:
        return 1
    return a*puiss1(a,b-1)
puiss1(3,4)
3**4
def puiss2(a,b):
    if b == 0:
        return 1
    if b%2 == 1:
        return a * puiss2(a,b-1)
    c = puiss2(a, b//2)
    return  c*c
puiss2(3,4)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup