RosettaCodeData/Task/Prime-decomposition/BQN/prime-decomposition-1.bqn
2023-07-01 13:44:08 -04:00

20 lines
776 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.

Factor { 𝕊n:
# Prime sieve
primes 0
Sieve { p 𝕊 ab:
p()b lb-a
E {(((𝕩|-a)+𝕩×))l} # Indices of multiples of 𝕩
a + / (1˜l) E{0¨(𝕨)𝕩}´ p # Primes in segment [a,b)
}
# Factor by trial division
r 0 # Result list
Try {
m (1+n) 2×𝕩 # Upper bound for factors this round
𝕩<m ? # Stop if no factors
primes np primes Sieve 𝕩m # New primes
{0=𝕩|n? r𝕩 n÷𝕩 𝕊𝕩 ;@}¨ np # Try each one
𝕊 m # Next segment
;@}
Try 2
r 1<n
}