In: Computer Science
IN PYTHON
Use the sieve of Eratosthenes to find all primes less than 10,000.
# all primes less than 10,000
n = 10000
# initialize array of n elements to true
primes = [True for i in range(n)]
# set 0 and 1 as false
primes[0]=primes[1]=False
# starting from first prime
current = 2
while (current**2 <= n):
# if current number is prime
if (primes[current] == True):
# set all multiples of current to false
for i in range(current**2, n, current):
primes[i] = False
# increment current
current += 1
print("Primes less than 10,000 are:")
# print numbers in primes array which are true
for prime in range(n):
if primes[prime]:
print(prime,end=' ')
print()
.
Screenshot:
.
Output:
.