Sieve of Eratosthenes

Akshay Bist 26th of March 2012

#!/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
220
Views
0
Comments
0
Downloads
Comments
Only Code Pad members can post comments!

Want to join Code Pad Request An Invite
    No Comments Posted

Suppo - Create, Customize & Host
Your App Support Page at Suppo

Snippet Details

  • Status: Public
  • Description: 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.
  • Saved by: Akshay Bist
  • Type: Python
  • Date: 26th March 2012
  • Characters: 518
  • Views: 220
  • Likes: 0
  • Downloads: 0 (SieveofEratosthenes.txt)
Keyboard Shortcuts

DDownload

TTweet

FFull View

Next Snippet

Previous Snippet