premiers.py

Created by ph-moutou

Created on April 21, 2018

496 Bytes

La fonction premiers(n) utilise la méthode du crible d’Ératosthène pour renvoyer la liste des nombres premiers jusqu’à n


# methode du crible d'Eratosthene
# pour determiner la liste des nombres premiers inferieurs a n
def premiers(n):
    prem=list(range(2,n+1))
    k=2
    nRacine=n**0.5
    while k<nRacine :
        prem=[p for p in prem if p<=k or p%k!=0]
        k=prem[prem.index(k)+1]   # nouveau nombre premier
    return prem

ListePremiers=premiers(100)
print("Plus grand premier =",ListePremiers[-1])
print("Nombre de premiers =",len(ListePremiers))
print("Liste premiers :",ListePremiers)

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.