Question
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?
Haskell
import Data.List (union)
pairwise :: (a -> a -> a) -> [a] -> [a]
:ys:t) = f xs ys : pairwise f t
pairwise f (xs= t
pairwise _ t
primes :: [Integer]
= 2 : _Y ((3 :) . gaps 5 . _U . map (\p-> [p*p, p*p+2*p..]))
primes where
= g (_Y g) -- recursion, Y combinator
_Y g :xs):t) = x : (union xs . _U . pairwise union) t -- ~= nub.sort.concat
_U ((x@(x:xs)
gaps k s| k < x = k : gaps (k+2) s -- ~= [k,k+2..]\\s, when
| otherwise = gaps (k+2) xs -- k <= head s && null(s\\[k,k+2..])
rotate :: [a] -> [a]
= []
rotate [] :xs) = xs ++ [x]
rotate (x
rotations :: [a] -> [[a]]
= take n (iterate rotate x)
rotations x where n = length x
intRotations :: Integer -> [Integer]
= map read . rotations . show
intRotations
prime :: Integer -> Bool
= n > 1 && foldr (\p r -> p*p > n || ((n `rem` p) /= 0 && r)) True primes
prime n
circular :: Integer -> Bool
= all prime . intRotations
circular
candidate :: Integer -> Bool
| n < 10 = True
candidate n | otherwise = all (`elem` "1379") (show n)
main :: IO ()
= print $ length . filter circular $ filter candidate $ takeWhile (< 1000000) primes main
$ ghc -O2 -o circular-primes circular-primes.hs
$ time ./circular-primes
real 0m0.115s
user 0m0.107s
sys 0m0.008s
Ruby
#!/usr/bin/env ruby
require 'mathn'
puts (1..1000000).select { |i|
1..i.to_s.length).all? { |j|
(.to_s.split('').rotate(j).join('').to_i.prime?
i}
}.count
$ time ruby circular-primes.rb
real 0m8.053s
user 0m8.052s
sys 0m0.000s