###### Modules #######

import math

####### Algo #########


### Exo 1
def divFacto(s:int) -> int :
    P=1
    n=1
    while P%s != 0 :
        n = n+1
        P = P*n
    return(n)


############

### Exo 2
def Pi(epsilon:float) -> float :
    u = 2
    v = u-math.sin(u)/math.cos(u)
    while abs(u-v)>epsilon :
        u = v
        v = u-math.sin(u)/math.cos(u)
    return(v)

def PiNb(epsilon:float) -> float :
    u=2
    v=u-math.sin(u)/math.cos(u)
    k=0
    while abs(u-v)>epsilon :
        u = v
        v = u-math.sin(u)/math.cos(u)
        k = k+1
    return(v,k)

############

### Exo 3
def parfait(n:int) -> bool :
    S=-n
    k=1
    while k<=math.sqrt(n) :
        if n%k == 0 :
            S = S+k+n//k
        k = k+1
    return(S==n)

############

### Exo 4
def nbMax(tab : list) -> int :
    vmax = tab[0]
    nmax = 1
    for i in range(1, len(tab)) :
        if vmax < tab[i] :
            vmax = tab[i]
            nmax = 1
        elif vmax == tab[i] :
            nmax = nmax + 1
    return nmax

##########

### Exo 5
def tabCroissant(tab : list) -> bool :
    for i in range( len(tab)-1 ) :
        if tab[i] > tab[i+1] :
            return False
    return True

#########

### Exo 6
def rechercheDicho(tab : list, val : int) -> tuple :
    btrouve = False
    ig = 0
    id = len(tab)-1
    while not btrouve and id-ig >= 0 :
        im = (ig + id) // 2
        if val < tab[im] :
            id = im-1
        elif val > tab[im] :
            ig = im+1
        else : 
            btrouve = True
    return btrouve, im

def zeroDicho(f,a,b,epsilon) : 
	"""Algorithme de dichotomie. 
	f -- fonction continue sur [a,b]
	a et b -- réels
	epsilon -- réel>0, critère d'approximation. """
	if f(a)*f(b) > 0 or epsilon <= 0 : 
		print("Erreur sur les hypothèse du principe de dichotomie.")
	c,d = a,b
	while (d-c) > 2*epsilon : 
		m = (c+d)/2 
		if f(c)*f(m) <= 0 : 
			d = m 
		else : 
			c = m
	return((c+d)/2)

#########

###
