Amicable pairs: Difference between revisions
Content deleted Content added
imported>Arakov |
Not a robot (talk | contribs) Add ABC |
||
(4 intermediate revisions by 2 users not shown) | |||
Line 439:
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN proper.divisor.sum.table n:
PUT {} IN propdivs
FOR i IN {1..n}: PUT 1 IN propdivs[i]
FOR i IN {2..floor (n/2)}:
PUT i+i IN j
WHILE j<=n:
PUT propdivs[j] + i IN propdivs[j]
RETURN propdivs
PUT 20000 IN maximum
PUT proper.divisor.sum.table maximum IN propdivs
FOR cand IN {1..maximum}:
PUT propdivs[cand] IN other
IF cand<other<maximum AND propdivs[other]=cand:
WRITE cand, other/</syntaxhighlight>
{{out}}
<pre>220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416</pre>
=={{header|Action!}}==
Calculations on a real Atari 8-bit computer take quite long time. It is recommended to use an emulator capable with increasing speed of Atari CPU.
Line 6,025 ⟶ 6,052:
[1, 2, 4, 8, 16, 23, 46, 47, 92, 94, 184, 188, 368, 376, 752, 1081, 2162, 4324, 8648]
[1, 2, 4, 8, 16, 1151, 2302, 4604, 9208]
</pre>
=={{header|Sage}}==
<syntaxhighlight lang="Sage">
def sum_of_proper_divisors(n):
return sum(divisors(n)) - n
# Iterate over the desired range
for x in range(1, 20001):
y = sum_of_proper_divisors(x)
if x == sum_of_proper_divisors(y):
print(f"{x} {y}")
</syntaxhighlight>
{{out}}
<pre>
220 284
1184 1210
2620 2924
5020 5564
6232 6368
10744 10856
12285 14595
17296 18416
</pre>
Line 6,033 ⟶ 6,085:
$constant search_limit = 20000
var a, b, count = integer▼
dim integer sumf(search_limit)
print "Searching up to"; search_limit; " for amicable pairs:"▼
▲rem - return sum of the proper divisors of n
rem - set up the table of proper divisor sums
sum = 1 ▼
for a = 1 to search_limit
next a
for a = 2 to search_limit
while (b > 0) and (b <= search_limit) do
begin
▲ if f2 > f1 then sum = sum + f2
end
next a
▲ f1 = f1 + 1
rem - search for pairs using the table
▲var a, b, count = integer
▲print "Searching up to"; search_limit; " for amicable pairs:"
count = 0
for a = 2 to search_limit
b = sumf(a)
if (b > a) and (b < search_limit) then
if a = sumf(b) then
begin
|