module Problem346 where

import qualified Data.HashSet as S

main :: IO ()
-- 1*b^0+..1*b^n = (b^(n+1)-1)(b-1)
-- 1 = 1 in all bases
-- n = 11 in base (n-1)
-- so atleast 3 digit in other bases: 1+b+b^2<=n => b<=sqrt n
main =
    print
        $ sum
        $ S.fromList
        $ (1 :)
        $ concat
        $ [ drop 2 $ takeWhile (<= lim) $ scanl1 (+) $ iterate (* b) 1
          | b <- [2 .. lim']
          ]
  where
    lim :: Int
    lim = 10 ^ 12
    lim' :: Int
    lim' = ceiling $ sqrt $ fromIntegral lim
