Amicable pairs: Difference between revisions
Content added Content deleted
imported>Arakov |
imported>KayproKid (→{{header|S-BASIC}}: followed approach of 2nd PL/I-80 example (runs MUCH faster!)) |
||
Line 6,033: | Line 6,033: | ||
$constant search_limit = 20000 |
$constant search_limit = 20000 |
||
⚫ | |||
rem - return p mod q |
|||
dim integer sumf(search_limit) |
|||
function mod(p, q = integer) = integer |
|||
end = p - q * (p / q) |
|||
⚫ | |||
rem - return sum of the proper divisors of n |
|||
function sumf(n = integer) = integer |
|||
rem - set up the table of proper divisor sums |
|||
var f1, f2, sum = integer |
|||
⚫ | |||
for a = 1 to search_limit |
|||
f1 = 2 |
|||
sumf(a) = 1 |
|||
next a |
|||
begin |
|||
if mod(n, f1) = 0 then |
|||
for a = 2 to search_limit |
|||
⚫ | |||
while (b > 0) and (b <= search_limit) do |
|||
begin |
begin |
||
sumf(b) = sumf(b) + a |
|||
b = b + a |
|||
if f2 > f1 then sum = sum + f2 |
|||
end |
end |
||
next a |
|||
f1 = f1 + 1 |
|||
end |
|||
rem - search for pairs using the table |
|||
end = sum |
|||
rem - main program begins here |
|||
⚫ | |||
⚫ | |||
count = 0 |
count = 0 |
||
for a = 2 to search_limit |
for a = 2 to search_limit |
||
b = sumf(a) |
b = sumf(a) |
||
if b > a then |
if (b > a) and (b < search_limit) then |
||
if a = sumf(b) then |
if a = sumf(b) then |
||
begin |
begin |
||
Line 6,070: | Line 6,068: | ||
end |
end |
||
</syntaxhighlight> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |