Arithmetic/Rational: Difference between revisions
m
→{{header|Haskell}}: Applied hlint hindent, specified import, framed it as a finite (rather than non-halting) computation.
(Adding link to Modula-3 webpage) |
m (→{{header|Haskell}}: Applied hlint hindent, specified import, framed it as a finite (rather than non-halting) computation.) |
||
Line 1,622:
=={{header|Haskell}}==
Haskell provides a <code>Rational</code> type, which is really an alias for <code>Ratio Integer</code> (<code>Ratio</code> being a polymorphic type implementing rational numbers for any <code>Integral</code> type of numerators and denominators). The fraction is constructed using the <code>%</code> operator.
<lang haskell>import Data.Ratio ((%))
--
main =
let n = 4
| candidate <- [2 .. 2^19],▼
mapM_ print $
getSum candidate == 1]▼
take
where getSum candidate = 1 % candidate +▼
n
sum [1 % factor + 1 % (candidate `div` factor)▼
[ candidate
| factor <- [2 .. floor(sqrt(fromIntegral(candidate)))],▼
candidate `mod` factor == 0]</lang>▼
where
1 % candidate +
sum
</lang>
For a sample implementation of <code>Ratio</code>, see [http://www.haskell.org/onlinereport/ratio.html the Haskell 98 Report].
|