Amicable pairs: Difference between revisions
Content deleted Content added
Thundergnat (talk | contribs) m Regularize header markup to recommended on category page |
No edit summary |
||
Line 4,164: | Line 4,164: | ||
17296 18416 found after 2.240 seconds |
17296 18416 found after 2.240 seconds |
||
2.250 seconds total search time</pre> |
2.250 seconds total search time</pre> |
||
==={{header|PL/I-80}}=== |
|||
Rather than populating an array with the sums of the proper divisors preparatory to a search, the approach here performs an immediate direct test, saving memory, and at no observable penalty in execution speed, even though about 25% more calls are made to sumf() than would be required simply to initialize the array. |
|||
<lang PL/I> |
|||
amicable: procedure options (main); |
|||
%replace |
|||
search_limit by 20000; |
|||
dcl (a, b, found) fixed bin; |
|||
put skip list ('Searching for amicable pairs up to '); |
|||
put edit (search_limit) (f(5)); |
|||
found = 0; |
|||
do a = 2 to search_limit; |
|||
b = sumf(a); |
|||
if (b > a) then |
|||
do; |
|||
if (sumf(b) = a) then |
|||
do; |
|||
found = found + 1; |
|||
put skip edit (a,b) (f(7)); |
|||
end; |
|||
end; |
|||
end; |
|||
put skip list (found, ' pairs were found'); |
|||
stop; |
|||
/* return sum of the proper divisors of n */ |
|||
sumf: procedure(n) returns (fixed bin); |
|||
dcl (n, sum, f1, f2) fixed bin; |
|||
sum = 1; /* 1 is a proper divisor of every number */ |
|||
f1 = 2; |
|||
do while ((f1 * f1) < n); |
|||
if mod(n, f1) = 0 then |
|||
do; |
|||
sum = sum + f1; |
|||
f2 = n / f1; |
|||
/* don't double count identical co-factors! */ |
|||
if f2 > f1 then sum = sum + f2; |
|||
end; |
|||
f1 = f1 + 1; |
|||
end; |
|||
return (sum); |
|||
end sumf; |
|||
end amicable; |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
Searching for amicable pairs up to 20000 |
|||
220 284 |
|||
1184 1210 |
|||
2620 2924 |
|||
5020 5564 |
|||
6232 6368 |
|||
10744 10856 |
|||
12285 14595 |
|||
17296 18416 |
|||
8 pairs were found |
|||
</pre> |
|||
=={{header|PL/M}}== |
=={{header|PL/M}}== |