1
0
Fork 0

Euler question 7

main
Bill Ewanick 2023-09-13 22:57:32 -04:00
parent a0fef78e65
commit 9db5fd08e6
1 changed files with 32 additions and 0 deletions

View File

@ -0,0 +1,32 @@
-- https://projecteuler.net/problem=7
-- Find the 10_001 prime number
primes1 :: [Integer]
primes1 = 2:3:prs
where
1:p:candidates = [6*k+r | k <- [0..], r <- [1,5]]
prs = p : filter isPrime candidates
isPrime n = not (any (divides n) (takeWhile (\p -> p*p <= n) prs))
divides n p = n `mod` p == 0
primes2 = 2:([3..] `minus` composites)
where
composites = union [multiples p | p <- primes2]
multiples n = map (n*) [n..]
(x:xs) `minus` (y:ys) | x < y = x:(xs `minus` (y:ys))
| x == y = xs `minus` ys
| x > y = (x:xs) `minus` ys
union = foldr merge [ ]
where
merge (x:xs) ys = x:merge' xs ys
merge' (x:xs) (y:ys) | x < y = x:merge' xs (y:ys)
| x == y = x:merge' xs ys
| x > y = y:merge' (x:xs) ys
primes3 = sieve [2..]
where
sieve (p : xs) = p : sieve [x | x <- xs, x `mod` p > 0]