Skip to content

Instantly share code, notes, and snippets.

@rense
Last active August 29, 2015 13:59

Revisions

  1. rense revised this gist Apr 16, 2014. 1 changed file with 2 additions and 3 deletions.
    5 changes: 2 additions & 3 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,4 @@


    def sieve(limit):
    limit += 1
    primes = []
    @@ -9,7 +8,7 @@ def sieve(limit):
    if not prime:
    continue
    primes.append(i)
    for n in range(i * i, limit, i):
    for n in range(i * 2, limit, i):
    a[n] = False
    print primes

    @@ -22,7 +21,7 @@ def sieve_two(limit):
    if i in multiples:
    continue
    primes.append(i)
    for j in range(i*i, limit, i):
    for j in range(i * 2, limit, i):
    multiples.append(j)
    print primes

  2. rense created this gist Apr 16, 2014.
    43 changes: 43 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@


    def sieve(limit):
    limit += 1
    primes = []
    a = [True] * limit
    a[0] = a[1] = False
    for (i, prime) in enumerate(a):
    if not prime:
    continue
    primes.append(i)
    for n in range(i * i, limit, i):
    a[n] = False
    print primes


    def sieve_two(limit):
    limit += 1
    primes = []
    multiples = []
    for i in range(2, limit):
    if i in multiples:
    continue
    primes.append(i)
    for j in range(i*i, limit, i):
    multiples.append(j)
    print primes


    import math


    def sieve_three(n):
    result = range(2, n)
    i = 0
    while i < len(result) and result[i] < math.sqrt(n):
    x = result[i] * 2
    while x < n:
    if x in result:
    result.remove(x)
    x += result[i]
    i += 1
    print result