euler-0.1.0.0

Safe HaskellSafe
LanguageHaskell2010

Euler.Util.ContinuedFractions

Synopsis

Documentation

type ContinuedFraction a = (a, [a]) Source #

continuedFractionConvergent Source #

Arguments

:: 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 #

Arguments

:: Integral a 
=> a

n

-> Maybe (ContinuedFraction a)

Continued fraction expansion of sqrt(n) in canonical form.

sqrtConvergents Source #

Arguments

:: 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