Jump to content

Anti-primes: Difference between revisions

no edit summary
(Added Rust)
No edit summary
Line 520:
The first 20 anti-primes are:
1 2 4 6 12 24 36 48 60 120 180 240 360 720 840 1260 1680 2520 5040 7560
</pre>
 
=={{header|Haskell}}==
<lang haskell>import Data.List
import Data.Maybe
 
firstPrimeFactor n = head $ filter (\x -> n `mod` x == 0 ) [2..n]
 
allPrimeFactors 1 = []
allPrimeFactors n = first:(allPrimeFactors(n `div` first))
where first = firstPrimeFactor n
 
factorCount 1 = 1
factorCount n = foldr1 (*) (map (+1) $ map length (group $ allPrimeFactors n))
 
divisorCount n = (n, factorCount n)
 
hcnNext (num, factors) = fromJust (find (\x -> snd x > factors) (map divisorCount [num..]))
 
hcnSequence = map (fst) $ iterate hcnNext (1, 1)
 
main = putStrLn $ show $ take 20 hcnSequence</lang>
{{output}}
<pre>
[1,2,4,6,12,24,36,48,60,120,180,240,360,720,840,1260,1680,2520,5040,7560]
</pre>
 
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.