Continued fraction/Arithmetic/Construct from rational number: Difference between revisions

Content added Content deleted
(Added Kotlin)
m (→‎{{header|Haskell}}: hlint, hindent, added output section)
Line 475: Line 475:
real2cf :: (RealFrac a, Integral b) => a -> [b]
real2cf :: (RealFrac a, Integral b) => a -> [b]
real2cf x =
real2cf x =
let (i, f) = properFraction x
i : if f == 0 then [] else real2cf (1/f)
in i :
where (i, f) = properFraction x
if f == 0
then []
else real2cf (1 / f)


main :: IO ()
main :: IO ()
main = do
main =
mapM_
print $ real2cf (13 % 11) -- => [1,5,2]
print
print $ take 20 $ real2cf (sqrt 2) -- => [1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]</lang>
[ real2cf (13 % 11)
, take 20 $ real2cf (sqrt 2)
]</lang>
{{Out}}
<pre>[1,5,2]
[1,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2]</pre>


=={{header|J}}==
=={{header|J}}==