-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path037-truncatable-primes.hs
More file actions
executable file
·29 lines (21 loc) · 971 Bytes
/
037-truncatable-primes.hs
File metadata and controls
executable file
·29 lines (21 loc) · 971 Bytes
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
26
27
28
29
#!/usr/bin/env runhaskell
{- https://projecteuler.net/problem=37
Problem 37
Truncatable primes
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.
-}
-- Very similar to 035-circular-primes.hs
import qualified Data.Numbers.Primes as DNP (isPrime, primes)
import qualified Data.List as DL (inits, tails)
truncations :: [a] -> [[a]]
truncations xs = (init $ DL.tails xs) ++ (tail $ DL.inits xs)
isTruncatablePrime :: Int -> Bool
isTruncatablePrime p = all (DNP.isPrime . read) . truncations . show $ p
main :: IO ()
main = print . sum . take 11 . filter isTruncatablePrime . filter (> 7) $ DNP.primes