Hello,
I'm trying to solve Euler problem 104 with the solution "My Solution" below but it takes quite long time therefore I quite.
Then I turn to haskell wiki for better solution which work well but I can not figure out why it is better than mine.
I'm wondering whether more function call decrease the performance.
Could you please help a little?
Thank you.
-- | My Solution
main = print $ snd $ head $ dropWhile (\(x,y) -> (not . bothNinePandigit "123456789") x) (zip fibs [1..])
bothNinePandigit digits n = isFirstNinePandigit digits n && isLastNinePandigit digits n
isLastNinePandigit digits n = digits == sort (lastDigits 9 n)
isFirstNinePandigit digits n = digits == sort (firstDigits 9 n)
firstDigits k n = take k (show n)
lastDigits k n = show (n `mod` 10^k)
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
-- | From Haskell Wiki
fibs = 1 : 1 : zipWith (+) fibs (tail fibs)
isFibPan n =
let a = n `mod` 1000000000
b = sort (show a)
c = sort $ take 9 $ show n
in b == "123456789" && c == "123456789"
ex_104 = snd $ head $ dropWhile (\(x,y) -> (not . isFibPan) x) (zip fibs [1..])