From f9e5e9639437cd8f3349b3321ccaf49652e5f817 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ramom=20Jos=C3=A9?= Date: Sat, 6 Oct 2018 23:39:21 -0300 Subject: [PATCH] Finder Primes --- FindingPrimes.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 FindingPrimes.py diff --git a/FindingPrimes.py b/FindingPrimes.py new file mode 100644 index 0000000..01f1fa1 --- /dev/null +++ b/FindingPrimes.py @@ -0,0 +1,18 @@ +''' +-The sieve of Eratosthenes is an algorithm used to find prime numbers, less than or equal to a given value. +-Illustration: https://upload.wikimedia.org/wikipedia/commons/b/b9/Sieve_of_Eratosthenes_animation.gif +''' +from math import sqrt +def SOE(n): + check = round(sqrt(n)) #Need not check for multiples past the square root of n + + sieve = [False if i <2 else True for i in range(n+1)] #Set every index to False except for index 0 and 1 + + for i in range(2, check): + if(sieve[i] == True): #If i is a prime + for j in range(i+i, n+1, i): #Step through the list in increments of i(the multiples of the prime) + sieve[j] = False #Sets every multiple of i to False + + for i in range(n+1): + if(sieve[i] == True): + print(i, end=" ") \ No newline at end of file