Jump to content

Amicable pairs: Difference between revisions

→‎{{header|S-BASIC}}: followed approach of 2nd PL/I-80 example (runs MUCH faster!)
imported>Arakov
imported>KayproKid
(→‎{{header|S-BASIC}}: followed approach of 2nd PL/I-80 example (runs MUCH faster!))
Line 6,033:
$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 * f1sumf(a) <= n do1
next a
begin
 
if mod(n, f1) = 0 then
for a = 2 to search_limit
sum b = 1a + a
while (b > 0) and (b <= search_limit) do
begin
sum sumf(b) = sumsumf(b) + f1a
f2 b = nb /+ f1a
if f2 > f1 then sum = sum + f2
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
for a = 2 to search_limit do
b = sumf(a)
if (b > a) and (b < search_limit) then
if a = sumf(b) then
begin
Line 6,070 ⟶ 6,068:
 
end
</syntaxhighlight>
{{out}}
<pre>
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.