Il crivello di Eratostene

Questo codice Python di esempio, genera una lista di numeri primi che vanno da 2 fino al numero passato come parametro.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
def eratostene(x):  
    primi = range(3, x + 1, 2)  
    for i in primi:  
        if(pow(i, 2) > x):  
            break  
        for j in primi:  
            if(i != j) and (j % i == 0):  
                primi.remove(j)  
                primi.insert(0, 2)
    return primi  

If you enjoyed this article and want to show your appreciation, consider buying me a coffee using the button below. Your support, even a small gesture, means a lot and will inspire me to continue writing and sharing more articles on my blog ❤️

Buy Me a Coffee at ko-fi.com
source code available on GitHub
Built with Hugo
Theme Stack designed by Jimmy