Skip to content

Instantly share code, notes, and snippets.

@phasedchirp
Created August 24, 2016 04:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phasedchirp/5d3fb6957dd397c80e3782d0b4a2f86e to your computer and use it in GitHub Desktop.
Save phasedchirp/5d3fb6957dd397c80e3782d0b4a2f86e to your computer and use it in GitHub Desktop.
import qualified Data.Vector as V (Vector(..),fromList,toList, (//), (!), foldl')
candidates :: Int -> V.Vector Int
candidates n = (V.fromList [2..n]) V.// filters
where filters = concat $ map (\x -> isMult x n) [2..n]
isMult :: Int -> Int -> [(Int,Int)]
isMult x u = [(x*m-2,0) | m <- [2..(u `div` x)]]
rotations :: Int -> [Int]
rotations x = map read $ filter (\x -> head x /= '0') $ take l (iterate step (show x))
where l = length (show x)
step :: String -> String
step (x:xs) = xs ++ [x]
check :: V.Vector Int -> Int -> [(Int,Int)]
check acc x = if and (map (\x -> 1 < acc V.! x) rs) then [] else zip rs (repeat 0)
where rs = map (+ (-2)) $ rotations x
circular :: Int -> V.Vector Int
circular x = circular' cVec cList
where circular' acc xs = acc V.// (concat $ map (check cVec) xs)
cVec = candidates x
cList = V.toList cVec
val = V.foldl' foldStep 0 (circular 1000000)
where foldStep x y = if y > 1 then x + 1 else x
main :: IO ()
main = print val
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment