Problem 230

230 Fibonacci Words

Problem 230 – Project Euler

規則性を見いだせ,ということなんでしょう.

再帰的な構造なので,それを利用.現在どこのブロックにいて(m),何番目(n)が欲しいのかで再帰.

import Data.List (findIndex)
fib :: [Integer]
fib = 100: 100: zipWith (+) fib (tail fib)
index :: Integer -> Int
index n = let Just m = findIndex (n <=) fib
in fromInteger $ ipart n m
ipart :: Integer -> Int -> Integer
ipart n  = n
ipart n 1 = n + 100
ipart n m | n <= p    = ipart n $ m - 2
| otherwise = ipart (n - p) $ m - 1
where p = fib !! (m - 2)
a, b :: [Char]
a = "1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679"
b = "8214808651328230664709384460955058223172535940812848111745028410270193852110555964462294895493038196"
main :: IO ()
main = print [ (a ++ b) !! ( index $ (127 + 19 * n)*7^n - 1) | n <- [17,16..] ]
More Reading
Older// Problem 231