*Main> last $ takeWhile (\(a,b) -> b < 20000) $ map (\x -> (x , x*x)) [1..]
(141,19881)
*Main>
ともう一桁詳しい√2が求まります。
これを関数化しますと
*Main> let f k = last $ takeWhile (\(a,b) -> b < 2*100^k) $ map (\x -> (x , x*x)) [1..]
*Main> f 3
(1414,1999396)
*Main> f 4
(14142,199996164)
*Main> f 5
(141421,19999899241)
*Main>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
h 1 = 1 h n = h (n-1) + 1 + h (n-1)
円盤の枚数が(n-1)のときの手順数がわかれば、円盤の枚数がnのときの手順数はいつでも計算できる、ということから再帰的に書くことができます。
計算してみます。
$ ghci
GHCi, version 8.0.1: http://www.haskell.org/ghc/ :? for help
Prelude> :l h.hs
[1 of 1] Compiling Main ( h.hs, interpreted )
Ok, modules loaded: Main.
*Main> h 1
1
*Main> h 2
3
*Main> h 3
7
*Main>