RosettaCodeData/Task/Extensible-prime-generator/BQN/extensible-prime-generator-1.bqn
2023-07-01 13:44:08 -04:00

18 lines
713 B
BQN
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Function that returns a new prime generator
PrimeGen {𝕤
i 0 # Counter: index of next prime to be output
primes 0
next 2
Sieve { p 𝕊 in:
E {(((𝕩|-i)+𝕩×))n-i} # Indices of multiples of 𝕩
i + / (1˜n-i) E{0¨(𝕨)𝕩}´ p # Primes in segment [i,n)
}
{𝕤
{ i=primes ? # Extend if required
next ((224)+ ט) oldnext # Sieve at most 16M new entries
primes (primes()next) Sieve oldnext
;@}
(i+1) iprimes
}
}
_w_{𝔽𝔾𝔽_𝕣_𝔾𝔽𝔾𝕩} # Looping utility for the session below