-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheuler037.hs
More file actions
25 lines (22 loc) · 1.07 KB
/
euler037.hs
File metadata and controls
25 lines (22 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
-- The number 3797 has an interesting property. Being prime itself, it is possible to continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97, and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
--
-- Find the sum of the only eleven primes that are both truncatable from left to right and right to left.
--
-- NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
import Prime
main :: IO ()
main = print answer
-- get the sum of the first 11 trunctable primes
answer :: Integer
answer =
sum $ take 11 $ filter (\n -> n>7 && trunctablePrime 10 n) $ primes
-- Given a power of ten and a prime, if the power of ten is bigger than the prime
-- we got bigger than the prime and thus all bits are prime
-- otherwise, divide the prime by the power of ten
-- and check both the quotient and remainder are prime then raise the power of ten and
-- go back round...
trunctablePrime :: Integer -> Integer -> Bool
trunctablePrime d p =
d > p || (prime q && prime r && (trunctablePrime (10*d) p))
where
(q,r) = p `quotRem` d