Arithmetic/Rational/Ada: Difference between revisions
m
Fixed syntax highlighting.
m (prepare for inclusion as template) |
m (Fixed syntax highlighting.) |
||
Line 1:
<noinclude>{{collection|Rational Arithmetic}}</noinclude>
The generic package specification:
<
type Number is range <>;
package Generic_Rational is
Line 60:
Zero : constant Rational := (0, 1);
One : constant Rational := (1, 1);
end Generic_Rational;</
The package can be instantiated with any integer type. It provides rational numbers represented by a numerator and denominator cleaned from the common divisors. Mixed arithmetic of the base integer type and the rational type is supported. Division to zero raises Constraint_Error. The implementation of the specification above is as follows:
<
function GCD (A, B : Number) return Number is
Line 283:
end Denominator;
end Generic_Rational;</
The implementation uses solution of the [[greatest common divisor]] task. Here is the implementation of the test:
<
with Ada.Text_IO; use Ada.Text_IO;
with Generic_Rational;
Line 307:
end;
end loop;
end Test_Rational;</
The perfect numbers are searched by summing of the reciprocal of each of the divisors of a candidate except 1. This sum must be 1 for a perfect number.
{{out}}
|