module Problem133 where

import Prime

main :: IO ()
main = print . sum . filter (not . canDividesRepunitPow10) $ getPrimesUpto 100000
  where
    canDividesRepunitPow10 p
        | gcd 10 p /= 1 = False
        | otherwise     = reduce 5 (reduce 2 (a p)) == 1
    reduce _ 1 = 1
    reduce p n = case n `quotRem` p of
        (q, 0) -> reduce p q
        _      -> n
    a :: Int -> Int
    a = go 1
    go v n = case (10 * v + 1) `rem` n of
        0 -> 2
        r -> 1 + go r n
