#!/usr/bin/env python
# Sieve of Eratosthenes - to find all the prime numbers in range 1 to n
# http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
def sieve_of_eratosthenes(n):
primes = []
numbers = []
for i in range(2, n+1):
numbers.append(i)
while numbers:
primes.append(numbers.pop(0))
for e in numbers:
if not e%primes[-1]:
numbers.pop(numbers.index(e))
return primes
def is_prime(n):
prime_list = sieve_of_eratosthenes(n)
if n in prime_list:
return True
return False
A Python implementation of Sieve of Eratosthenes with functions to find all prime numbers between 1 & n, and to check whether input number is a prime or not.
3 Responses
http://goo.gl/v9nmki
Write a comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.