irreductibles.py

Created by ph-moutou

Created on April 22, 2018

1.17 KB

Ce programme donne, pour le quotient de a par b: sa nature (réductible ou non), son rang (ordre du dénombrement) parmi les fractions de même nature, le pourcentage d’irréductibles jusqu’à cette fraction. Il affiche les fractions irréductibles obtenues dans l’ordre numérique; de même pour les fractions simplifiables.


def pgcd(a,b): 
    reste=a%b
    if reste==0 : return b
    else : return pgcd(b,reste)

def insere(a,b,L):
    rg,val=0,a/b
    for e in L:
        if e[2]>val:break
        else:rg+=1
    L.insert(rg,[a,b,val])

def denombre(n,d):
    som,tot,rang,irr,red=2,0,0,[],[]
    while som<=n+d:
        num,denom=1,som-1
        while num<som:
            PGCD=pgcd(num,denom)
            if PGCD==1:
                rang+=1
                insere(num,denom,irr)
            else:insere(num,denom,red)
            tot+=1
            if num==n and denom==d:return rang,tot,PGCD,irr,red
            num+=1
            denom-=1
        som+=1

a,b=2,4
rng,tot,gcd,Lirr,Lred=denombre(a,b)
if gcd==1:print('{}/{} : fraction irreductible #{}'.format(a,b,rng))
else:print('{}/{} : fraction simplifiable #{}'.format(a,b,tot-rng))
print('Nombre de fractions inferieures ou egales : {}'.format(tot))
print('Ratio irreductibles={}%'.format(round(rng/tot*100,1)))
print("Fractions irreductibles par ordre croissant:")
for f in Lirr:
    print('{}/{}'.format(f[0],f[1]),end=" ")
print("\nFractions simplifiables par ordre croissant:")
for f in Lred:
    print('{}/{}'.format(f[0],f[1]),end=" ")

During your visit to our site, NumWorks needs to install "cookies" or use other technologies to collect data about you in order to:

With the exception of Cookies essential to the operation of the site, NumWorks leaves you the choice: you can accept Cookies for audience measurement by clicking on the "Accept and continue" button, or refuse these Cookies by clicking on the "Continue without accepting" button or by continuing your browsing. You can update your choice at any time by clicking on the link "Manage my cookies" at the bottom of the page. For more information, please consult our cookies policy.