Skip to content

Instantly share code, notes, and snippets.

@akisute
Created May 8, 2009 12:04
Show Gist options
  • Save akisute/108758 to your computer and use it in GitHub Desktop.
Save akisute/108758 to your computer and use it in GitHub Desktop.
Damn fuckin super-duper n00bish Sieve of Eratosthenes.
# http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes
def getPrimes(num):
numbers = range(2, num)
primes = [1]
while(max(primes)*max(primes) <= max(numbers)):
p = numbers.pop(0)
primes.append(p)
for n in numbers:
if (n%p == 0): numbers.remove(n)
del primes[0]
return primes + numbers
# get a list of natural numbers that are product of 2 prime numbers, which is 1 <= n < 100.
primes1 = getPrimes(99)
primes2 = getPrimes(99)
result1 = []
for a in primes1:
for b in primes2:
result1.append(a*b)
print result1
# get a list of natural numbers that are 1 <= n < 100 and are product of 2 prime numbers.
primes1 = getPrimes(49) # because 2 * 50 = 100
primes2 = getPrimes(49) # because 2 * 50 = 100
result2 = []
for a in primes1:
for b in primes2:
product = a*b
if product < 100 and product not in result2: result2.append(a*b)
result2.sort()
print result2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment