How to resolve the algorithm Amicable pairs step by step in the Cowgol programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Amicable pairs step by step in the Cowgol programming language
Table of Contents
Problem Statement
Two integers
N
{\displaystyle N}
and
M
{\displaystyle M}
are said to be amicable pairs if
N ≠ M
{\displaystyle N\neq M}
and the sum of the proper divisors of
N
{\displaystyle N}
(
s u m
(
p r o p D i v s
( N ) )
{\displaystyle \mathrm {sum} (\mathrm {propDivs} (N))}
)
= M
{\displaystyle =M}
as well as
s u m
(
p r o p D i v s
( M ) )
N
{\displaystyle \mathrm {sum} (\mathrm {propDivs} (M))=N}
.
1184 and 1210 are an amicable pair, with proper divisors:
Calculate and show here the Amicable pairs below 20,000; (there are eight).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Amicable pairs step by step in the Cowgol programming language
Source code in the cowgol programming language
include "cowgol.coh";
const LIMIT := 20000;
# Calculate sums of proper divisors
var divSum: uint16[LIMIT + 1];
var i: @indexof divSum;
var j: @indexof divSum;
i := 2;
while i <= LIMIT loop
divSum[i] := 1;
i := i + 1;
end loop;
i := 2;
while i <= LIMIT/2 loop
j := i * 2;
while j <= LIMIT loop
divSum[j] := divSum[j] + i;
j := j + i;
end loop;
i := i + 1;
end loop;
# Test each pair
i := 2;
while i <= LIMIT loop
j := i + 1;
while j <= LIMIT loop
if divSum[i] == j and divSum[j] == i then
print_i32(i as uint32);
print(", ");
print_i32(j as uint32);
print_nl();
end if;
j := j + 1;
end loop;
i := i + 1;
end loop;
You may also check:How to resolve the algorithm AKS test for primes step by step in the Scheme programming language
You may also check:How to resolve the algorithm ABC problem step by step in the C programming language
You may also check:How to resolve the algorithm Statistics/Normal distribution step by step in the FreeBASIC programming language
You may also check:How to resolve the algorithm Palindrome detection step by step in the Plain English programming language
You may also check:How to resolve the algorithm Ascending primes step by step in the Matlab programming language