rationnel.py

Created by ph-moutou

Created on April 21, 2018

985 Bytes

Ce programme détermine la nature d’un nombre rationnel (entier, décimal non entier ou rationnel non décimal) et donne, dans le cas d’un nombre non décimal, la séquence périodique et sa longueur. Dans tous les cas, une écriture décimale est donnée.


# Donne le quotient de a par b: partie fixe et sequence periodique

def decimal(a,b):
    quotient=str(a//b)  #la partie entiere du quotient
    reste=a%b
    texte=0
    if reste!=0: 
        suiteRestes=[reste]
        quotient+=","
        while texte==0:
            quotient+=str(reste*10//b)
            reste=(reste*10)%b
            suiteRestes.append(reste)
            if reste==0:
                texte=1
                break
            for i in range(len(suiteRestes)-1) :
                if reste==suiteRestes[i] :
                    long=len(suiteRestes)-i-1
                    sequence=str(quotient[-long:])
                    quotient+=sequence+"..."
                    print("Sequence periodique de longueur "+str(long)+" : "+sequence)
                    texte=2
    return quotient,texte

num,denom=8,17
q,t=decimal(num,denom)
nature=["entier","decimal non entier","rationnel non decimal"]
print(num,"/",denom,"est un nombre "+nature[t],":",q)

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.