Last active
August 29, 2015 14:24
-
-
Save alogic0/bb1cf41b8c8db8e3865a to your computer and use it in GitHub Desktop.
Fibonacci numbers in chunks of decimal part of division 1 by 9999...899...
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
num48 = 999999999999999999999998999999999999999999999999 | |
strNum24 = replicate 47 '0' ++ show (10 ^ (58*48) `div` num48) | |
chunks _ [] = [] | |
chunks n xs = let (h, t) = splitAt n xs in h : chunks n t | |
result24 = putStr $ unlines $ map (unwords . chunks 24) $ chunks 48 strNum24 | |
-------- | |
fibs :: [Integer] | |
fibs = 0:1: zipWith (+) fibs (tail fibs) | |
threeFibs :: [(Int, Int, Integer)] | |
threeFibs = zip3 [1..] (map (length.show) fibs) fibs | |
-- last fib with length n | |
nLenFib :: Int -> Int | |
nLenFib n = | |
let (k,_,_) = last $ takeWhile (\(_,l,_) -> l < n + 1) threeFibs | |
in k - 1 | |
pad0N :: Int -> String -> String | |
pad0N n xs = replicate (n - length xs) '0' ++ xs | |
formatN n num = chunks n $ replicate (2*n - 1) '0' ++ show num | |
longNumStr :: Int -> String | |
longNumStr n = ('1' :) $ concatMap (pad0N k . show) $ drop 2 $ ts | |
where | |
ts = take n fibs | |
k = length . show $ ts !! (n-1) | |
nineNumPow :: Int -> (Integer, (Int, Int)) | |
nineNumPow width = | |
let lnum = longNumStr (nLenFib width) | |
len = length lnum | |
pow = len + 2 * width - 1 | |
in (10 ^ pow `div` (read lnum), (pow, width)) | |
printNine (num, (pow, w)) = (putStr . unlines) $ formatN w $ 10 ^ pow `div` num | |
-- usage: printNine $ nineNumPow 24 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment