Arithmetic/Rational: Difference between revisions
Content deleted Content added
Add Seed7 example |
|||
Line 2,417:
}
}</lang>
=={{header|Seed7}}==
The library [http://seed7.sourceforge.net/libraries/rational.htm rational.s7i] defines the type
[http://seed7.sourceforge.net/manual/types.htm#rational rational], which supports the required functionality.
Rational numbers are based on the type [http://seed7.sourceforge.net/manual/types.htm#integer integer].
For rational numbers, which are based on integers with unlimited precision, use
[http://seed7.sourceforge.net/manual/types.htm#bigRational bigRational], which is defined
in the library [http://seed7.sourceforge.net/libraries/bigrat.htm bigrat.s7i].
<lang seed7>$ include "seed7_05.s7i";
include "rational.s7i";
const func boolean: isPerfect (in integer: candidate) is func
result
var boolean: isPerfect is FALSE;
local
var integer: divisor is 0;
var rational: sum is rational.value;
begin
sum := 1 / candidate;
for divisor range 2 to sqrt(candidate) do
if candidate mod divisor = 0 then
sum +:= 1 / divisor + 1 / (candidate div divisor);
end if;
end for;
isPerfect := sum = rat(1);
end func;
const proc: main is func
local
var integer: candidate is 0;
begin
for candidate range 2 to 2 ** 19 - 1 do
if isPerfect(candidate) then
writeln(candidate <& " is perfect");
end if;
end for;
end func;</lang>
{{out}}
<pre>
6 is perfect
28 is perfect
496 is perfect
8128 is perfect
</pre>
=={{header|Slate}}==
|