You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If I understand correctly what the sieve.pas benchmarks tests are supposed to do (count primes up to a certain number, 8192 in the tests) - the algorithm used to calculate primes:
fillchar(flags, sizeof(flags), true);
count := 0;
for i:=0to size doif flags[i] thenbegin
prime := i*2 + 3;
k := prime + i;
while (k <= size) dobegin
flags[k] := false;
inc(k, prime);
end;
inc(count);
end;
fillchar(flags, sizeof(flags), true);
flags[0] := false;
flags[1] := false;
count := 0;
for i:=2to size doif flags[i] then{ mark all multiplications of i as non-primes }begin
k := i + i;
while (k <= size) dobegin
flags[k] := false;
inc(k,i);
end;
inc(count);
end;
as the inner part has to mark all multiplications of the current prime.
The other algorithm just does not mark many non-primes (it seems like an optimization to faster find the next prime - but it does not seem to be used correctly here...).
Also, in consequence, this cheats a bit on the result of the benchmark (by more than a 100 'ticks')...
The text was updated successfully, but these errors were encountered:
OK. I see. That algorithm does indeed something a bit different than it seemed at first glance - it does not store information about primes it finds (what could be easily added, eg in a second array), it just counts them, using the array in a more packed way.
If I understand correctly what the
sieve.pas
benchmarks tests are supposed to do (count primes up to a certain number, 8192 in the tests) - the algorithm used to calculate primes:is not correct - it counts 1899 primes while up to 8192 (what is calculated), there are only 1028.
The corrected algorithm would be:
as the inner part has to mark all multiplications of the current prime.
The other algorithm just does not mark many non-primes (it seems like an optimization to faster find the next prime - but it does not seem to be used correctly here...).
Also, in consequence, this cheats a bit on the result of the benchmark (by more than a 100 'ticks')...
The text was updated successfully, but these errors were encountered: