25 lines
803 B
Haskell
25 lines
803 B
Haskell
-- The number, 197, is called a circular prime because all rotations of the digits: 197, 971, and 719, are themselves prime.
|
|
--
|
|
-- There are thirteen such primes below 100: 2, 3, 5, 7, 11, 13, 17, 31, 37, 71, 73, 79, and 97.
|
|
--
|
|
-- How many circular primes are there below one million?
|
|
|
|
import Data.List (inits, tails)
|
|
import ProjectEuler (isPrime, primeSieve)
|
|
|
|
isCircularPrime :: Int -> Bool
|
|
isCircularPrime n
|
|
| n == 2 = True
|
|
| any (`elem` ['0', '2' .. '8']) (show n) = False
|
|
| all (isPrime . read) rotations = True
|
|
| otherwise = False
|
|
where
|
|
rotations = zipWith (++) (tails (show n)) (init (inits (show n)))
|
|
|
|
main = do
|
|
let result = length $ filter isCircularPrime (takeWhile (< 1000000) primeSieve)
|
|
putStrLn $
|
|
"Project Euler, Problem 35\n"
|
|
++ "Answer: "
|
|
++ show result
|