RosettaCodeData/Task/Amicable-pairs/S-BASIC/amicable-pairs.basic

40 lines
721 B
Text
Raw Permalink Normal View History

2023-07-01 11:58:00 -04:00
$lines
$constant search_limit = 20000
2024-03-06 22:25:12 -08:00
var a, b, count = integer
dim integer sumf(search_limit)
print "Searching up to"; search_limit; " for amicable pairs:"
rem - set up the table of proper divisor sums
for a = 1 to search_limit
sumf(a) = 1
next a
for a = 2 to search_limit
b = a + a
while (b > 0) and (b <= search_limit) do
2023-07-01 11:58:00 -04:00
begin
2024-03-06 22:25:12 -08:00
sumf(b) = sumf(b) + a
b = b + a
2023-07-01 11:58:00 -04:00
end
2024-03-06 22:25:12 -08:00
next a
rem - search for pairs using the table
2023-07-01 11:58:00 -04:00
count = 0
2024-03-06 22:25:12 -08:00
for a = 2 to search_limit
2023-07-01 11:58:00 -04:00
b = sumf(a)
2024-03-06 22:25:12 -08:00
if (b > a) and (b < search_limit) then
2023-07-01 11:58:00 -04:00
if a = sumf(b) then
begin
print using "##### #####"; a; b
count = count + 1
end
next a
print count; " pairs were found"
end