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


var a, b, count = integer
rem - return p mod q
dim integer sumf(search_limit)
function mod(p, q = integer) = integer
end = p - q * (p / q)


print "Searching up to"; search_limit; " for amicable pairs:"
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

sum = 1
for a = 1 to search_limit
f1 = 2
while (f1 * f1) <= n do
sumf(a) = 1
next a
begin

if mod(n, f1) = 0 then
for a = 2 to search_limit
b = a + a
while (b > 0) and (b <= search_limit) do
begin
begin
sum = sum + f1
sumf(b) = sumf(b) + a
f2 = n / f1
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
var a, b, count = integer
print "Searching up to"; search_limit; " for amicable pairs:"
count = 0
count = 0
for a = 2 to search_limit do
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>