Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
32
Task/Continued-fraction/Haskell/continued-fraction-1.hs
Normal file
32
Task/Continued-fraction/Haskell/continued-fraction-1.hs
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
import Data.List (unfoldr)
|
||||
import Data.Char (intToDigit)
|
||||
|
||||
-- continued fraction represented as a (possibly infinite) list of pairs
|
||||
sqrt2, napier, myPi :: [(Integer, Integer)]
|
||||
sqrt2 = zip (1 : [2,2 ..]) [1,1 ..]
|
||||
|
||||
napier = zip (2 : [1 ..]) (1 : [1 ..])
|
||||
|
||||
myPi = zip (3 : [6,6 ..]) ((^ 2) <$> [1,3 ..])
|
||||
|
||||
-- approximate a continued fraction after certain number of iterations
|
||||
approxCF
|
||||
:: (Integral a, Fractional b)
|
||||
=> Int -> [(a, a)] -> b
|
||||
approxCF t = foldr (\(a, b) z -> fromIntegral a + fromIntegral b / z) 1 . take t
|
||||
|
||||
-- infinite decimal representation of a real number
|
||||
decString
|
||||
:: RealFrac a
|
||||
=> a -> String
|
||||
decString frac = show i ++ '.' : decString_ f
|
||||
where
|
||||
(i, f) = properFraction frac
|
||||
decString_ = map intToDigit . unfoldr (Just . properFraction . (10 *))
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
(putStrLn .
|
||||
take 200 . decString . (approxCF 950 :: [(Integer, Integer)] -> Rational))
|
||||
[sqrt2, napier, myPi]
|
||||
37
Task/Continued-fraction/Haskell/continued-fraction-2.hs
Normal file
37
Task/Continued-fraction/Haskell/continued-fraction-2.hs
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
import Data.Ratio ((%), denominator, numerator)
|
||||
import Data.Bool (bool)
|
||||
|
||||
-- ignoring the task-given pi sequence: sucky convergence
|
||||
-- pie = zip (3:repeat 6) (map (^2) [1,3..])
|
||||
pie = zip (0 : [1,3 ..]) (4 : map (^ 2) [1 ..])
|
||||
|
||||
sqrt2 = zip (1 : repeat 2) (repeat 1)
|
||||
|
||||
napier = zip (2 : [1 ..]) (1 : [1 ..])
|
||||
|
||||
-- truncate after n terms
|
||||
cf2rat n = foldr (\(a, b) f -> (a % 1) + ((b % 1) / f)) (1 % 1) . take n
|
||||
|
||||
-- truncate after error is at most 1/p
|
||||
cf2rat_p p s = f $ map ((\i -> (cf2rat i s, cf2rat (1 + i) s)) . (2 ^)) [0 ..]
|
||||
where
|
||||
f ((x, y):ys)
|
||||
| abs (x - y) < (1 / fromIntegral p) = x
|
||||
| otherwise = f ys
|
||||
|
||||
-- returns a decimal string of n digits after the dot; all digits should
|
||||
-- be correct (doesn't mean it's the best approximation! the decimal
|
||||
-- string is simply truncated to given digits: pi=3.141 instead of 3.142)
|
||||
cf2dec n = ratstr n . cf2rat_p (10 ^ n)
|
||||
where
|
||||
ratstr l a = show t ++ '.' : fracstr l n d
|
||||
where
|
||||
d = denominator a
|
||||
(t, n) = quotRem (numerator a) d
|
||||
fracstr 0 _ _ = []
|
||||
fracstr l n d = show t ++ fracstr (l - 1) n1 d
|
||||
where
|
||||
(t, n1) = quotRem (10 * n) d
|
||||
|
||||
main :: IO ()
|
||||
main = mapM_ putStrLn [cf2dec 200 sqrt2, cf2dec 200 napier, cf2dec 200 pie]
|
||||
Loading…
Add table
Add a link
Reference in a new issue