Optimize Sieve of Eratosthenes Further

a slightly different approach: use a bitarray to represent the odd numbers 3,5,7,… saving some space compared to a list of booleans. this may save some space only and not help speedup… from bitarray import bitarray def index_to_number(i): return 2*i+3 def number_to_index(n): return (n-3)//2 LIMIT_NUMBER = 50 LIMIT_INDEX = number_to_index(LIMIT_NUMBER)+1 odd_primes = bitarray(LIMIT_INDEX) # index … Read more

How do I implement the Sieve Of Eratosthenes using multithreaded C#?

Edited: My answer to the question is: Yes, you can definitely use the Task Parallel Library (TPL) to find the primes to one billion faster. The given code(s) in the question is slow because it isn’t efficiently using memory or multiprocessing, and final output also isn’t efficient. So other than just multiprocessing, there are a … Read more

To find first N prime numbers in python

using a regexp 🙂 #!/usr/bin/python import re, sys def isPrime(n): # see http://www.noulakaz.net/weblog/2007/03/18/a-regular-expression-to-check-for-prime-numbers/ return re.match(r’^1?$|^(11+?)\1+$’, ‘1’ * n) == None N = int(sys.argv[1]) # number of primes wanted (from command-line) M = 100 # upper-bound of search space l = list() # result list while len(l) < N: l += filter(isPrime, range(M – 100, M)) … Read more

Fastest primality test

The only deterministic, polynomial-time algorithm for primality testing I know of is the AKS primality test (http://en.wikipedia.org/wiki/AKS_primality_test). However, there are a lot of very good randomized primality tests that are fast and have extremely good probability of success. They usually work by finding whether the number is composite with exponentially good probability, so they’ll either … Read more

double stream feed to prevent unneeded memoization?

Normally, definition of primes stream in Richard Bird’s formulation of the sieve of Eratosthenes is self-referential: import Data.List.Ordered (minus, union, unionAll) ps = 2 : minus [3..] — `:` is “cons” (foldr (\p r -> p*p : union [p*p+p, p*p+2*p..] r) [] ps) The primes ps produced by this definition are used as the input … Read more