From f09f056c87ef9cdfcbab60836a473fb0eb3d5e02 Mon Sep 17 00:00:00 2001 From: Daniele Fucini Date: Sat, 23 Nov 2024 17:46:46 +0100 Subject: [PATCH] Add Haskell solution for Problem 35 --- Haskell/p035.hs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 Haskell/p035.hs diff --git a/Haskell/p035.hs b/Haskell/p035.hs new file mode 100644 index 0000000..7dc5fce --- /dev/null +++ b/Haskell/p035.hs @@ -0,0 +1,23 @@ +-- 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))) +--isCircularPrime n = let rotations = zipWith (++) (tails (show n)) (init (inits (show n))) +-- in all (isPrime . read) rotations + +main = do + let result = length $ filter isCircularPrime (takeWhile (<1000000) primeSieve) + putStrLn $ "Project Euler, Problem 35\n" + ++ "Answer: " ++ show result