Anonymous user
Arithmetic/Rational: Difference between revisions
m
→{{header|Nimrod}}
(Add Nimrod) |
|||
Line 1,922:
<lang nimrod>import math
proc `^`[T](base
var (base, exp) = (base, exp)
result = 1
Line 1,932:
base *= base
proc gcd[T](u, v:
if v != 0:
gcd(v, u mod v)
Line 1,938:
u.abs
proc lcm[T](a, b:
a div gcd(a, b) * b
type Rational* = tuple[num, den:
proc fromInt*(x: TInteger): Rational =
Line 1,994:
x.num.float / x.den.float
proc toInt*(x: Rational):
x.num div x.den
Line 2,010:
result.den = abs x.den
for candidate in 2'i64 .. <((2'i64)^19):
var sum: Rational = (1'i64, candidate)
for factor in 2'i64 .. pow(candidate.float, 0.5).
if candidate mod factor == 0:
sum += (1'i64, factor) + (1'i64, candidate div factor)
if sum.den == 1:
echo "Sum of recipr. factors of ",candidate," = ",sum.num," exactly ",
|