You can first optimize it in two ways: 1) You do not require integer linkedlist. Instead use a simple for loop. 2) Once you are using for loop, you can first remove all the even numbers as they are obviously divisible by 2. And then just traverse through the odd...

performance,haskell,time-complexity,primes,sieve

Your code has an exponential explosion inside it: sieve p (x:xs) | rem x p == 0 = sieve p xs | otherwise = x : (sieve x $ sieve p xs) -- ^^^^^^^ here! -- ^^^^^^^ and here! You intended for the inner sieve call to just continue filtering...

If I understand your question right, you could replace all your Sieve2, Sieve3, ... classes by this: class PrimeSieve : public Sieve { public: PrimeSieve(Sieve& src, int prime) : Sieve() , src_(src) , prime_(prime) { } override int NextNumber () { int i; do { i = _src.NextNumber (); }...

Only update primes when it's actually needed. Keep track of the number of entries you've updated, and only output that number of entries at the end: j := 0; for i := 0 to 999 do begin if candidates[i] <> 0 then begin primes[j] := i; Inc(j); end; end; for...

You cause an infinite recursion by applying all to the entire sieve and not just the values which you sieved so far. I.e. For the second element, isPrime tests all values in sieve instead of just 2. In your Python version, you wrote is_prime(n, ps) which only tests n against...

Those functions, unless declared in the bitarray.h header, are local to the bitarray.cpp. If you want access to them in sieve.h, declare them in the bitarray.h header, or in another file accessible by sieve.cpp/sieve.h. // bitarray.h class BitArray { public: void Set(); // Example. void Unset(); // Example. }; //...