Instead of searching up to n-1
, I would only search up to (int) sqrt(n)
.
Figure out why this is sufficient yourself. ;-)
I do not get why you need altesP
at all. Can't you just increment p
by two?
I wouldn't filter by striking out. I would build a positive list, and add the prime numbers you have found.
Look into fast primeness tests that can rule out a number without having to go through the whole sieve.
So do the following changes to your code, please:
instead of erasing
aZahlen
, build a list ofprimes
.sqrtN = (int)sqrt(n)
as allocation size should be fine, and use a countfoundPrimes
for how many primes you know.Iterate over
p
up to<= sqrtN
without any fuzz. See if any of the known primes is a divisor, otherwise you found a new prime. Output it, and store it in yourfoundPrimes
list.