30 lines
923 B
Text
30 lines
923 B
Text
% Generate proper divisors from 1 to max
|
|
proper_divisors = proc (max: int) returns (array[int])
|
|
divs: array[int] := array[int]$fill(1, max, 0)
|
|
for i: int in int$from_to(1, max/2) do
|
|
for j: int in int$from_to_by(i*2, max, i) do
|
|
divs[j] := divs[j] + i
|
|
end
|
|
end
|
|
return(divs)
|
|
end proper_divisors
|
|
|
|
% Are A and B and amicable pair, given the proper divisors?
|
|
amicable = proc (divs: array[int], a, b: int) returns (bool)
|
|
return(divs[a] = b & divs[b] = a)
|
|
end amicable
|
|
|
|
% Find all amicable pairs up to 20 000
|
|
start_up = proc ()
|
|
max = 20000
|
|
po: stream := stream$primary_output()
|
|
divs: array[int] := proper_divisors(max)
|
|
|
|
for a: int in int$from_to(1, max) do
|
|
for b: int in int$from_to(a+1, max) do
|
|
if amicable(divs, a, b) then
|
|
stream$putl(po, int$unparse(a) || ", " || int$unparse(b))
|
|
end
|
|
end
|
|
end
|
|
end start_up
|