Legendre prime counting function: Difference between revisions

no edit summary
No edit summary
No edit summary
Line 11:
 
π(n) = φ(x, a) + a - 1, where a = π(√n)
 
The Legendre formula still requires the use of a sieve to enumerate primes; however it's only required to sieve up to the √n, and for counting primes, the Legendre method is much faster than sieving up to n.
 
;Task:
Calculate π(n) for values up to 1 billion. Show π(n) for n = 1, 10, 100, ... 10<sup>9</sup>.
 
For this task, you may refer to a prime number sieve (such as the Sieve of Eratosthenes or the extensible sieve) in an external library to enumerate the primes required by the formula. (note that the formula only requires known primes up the square root of the limit, thus sieves can be smaller.) Also note that it will be necessary to memoize the results of φ(x, a) in order to have reasonable performance, since the recurrence relation would otherwise take exponential time.
 
=={{header|CoffeeScript}}==
Line 117 ⟶ 119:
 
main(_) ->
put(primes, array:from_list(primality:sieve(imathfloor(math:sqrt(?LIMIT))))),
ets:new(memphi, [set, named_table, protected]),
output(0, 9).
357

edits