Safe Haskell | Safe |
---|---|
Language | Haskell2010 |
Synopsis
- type ContinuedFraction a = (a, [a])
- continuedFractionConvergent :: Integral a => ContinuedFraction a -> Ratio a
- sqrtContinuedFraction :: Integral a => a -> Maybe (ContinuedFraction a)
- sqrtConvergents :: Integral a => a -> Maybe [Ratio a]
Documentation
type ContinuedFraction a = (a, [a]) Source #
continuedFractionConvergent Source #
:: Integral a | |
=> ContinuedFraction a | Must be finite |
-> Ratio a | The approximation represented by the continued fraction |
>>>
fmap (take 6) <$> sqrtContinuedFraction 2
Just (1,[2,2,2,2,2,2])
>>>
fmap (take 14) <$> sqrtContinuedFraction 114
Just (10,[1,2,10,2,1,20,1,2,10,2,1,20,1,2])
>>>
sqrtContinuedFraction 16
Nothing
sqrtContinuedFraction Source #
:: Integral a | |
=> a | n |
-> Maybe (ContinuedFraction a) | Continued fraction expansion of sqrt(n) in canonical form. |
:: Integral a | |
=> a | n |
-> Maybe [Ratio a] | The convergents of the continued fractions representation of sqrt(n). |
>>>
:{
showRatio :: (Integral a, Show a) => Ratio a -> String showRatio x = intercalate "/" $ show . ($ x) <$> [ numerator, denominator ] :}
>>>
(intercalate ", ") . (fmap showRatio) . (take 5) <$> sqrtConvergents 2
Just "1/1, 3/2, 7/5, 17/12, 41/29"
>>>
(intercalate ", ") . (fmap showRatio) . (take 5) <$> sqrtConvergents 3
Just "1/1, 2/1, 5/3, 7/4, 19/11"
>>>
sqrtConvergents 16
Nothing