How to resolve the algorithm Amicable pairs step by step in the Nim programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Amicable pairs step by step in the Nim 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 Nim programming language
Source code in the nim programming language
from math import sqrt
const N = 524_000_000.int32
proc sumProperDivisors(someNum: int32, chk4less: bool): int32 =
result = 1
let maxPD = sqrt(someNum.float).int32
let offset = someNum mod 2
for divNum in countup(2 + offset, maxPD, 1 + offset):
if someNum mod divNum == 0:
result += divNum + someNum div divNum
if chk4less and result >= someNum:
return 0
for n in countdown(N, 2):
let m = sumProperDivisors(n, true)
if m != 0 and n == sumProperDivisors(m, false):
echo $n, " ", $m
from math import sqrt
const N = 524_000_000.int32
var x = newSeq[int32](N+1)
for i in 2..sqrt(N.float).int32:
var p = i*i
x[p] += i
var j = i + i
while (p += i; p <= N):
j.inc
x[p] += j
for m in 4..N:
let n = x[m] + 1
if n < m and n != 0 and m == x[n] + 1:
echo n, " ", m
You may also check:How to resolve the algorithm Respond to an unknown method call step by step in the C# programming language
You may also check:How to resolve the algorithm Statistics/Basic step by step in the VBA programming language
You may also check:How to resolve the algorithm Hello world/Line printer step by step in the EDSAC order code programming language
You may also check:How to resolve the algorithm Rate counter step by step in the Erlang programming language
You may also check:How to resolve the algorithm Continued fraction/Arithmetic/G(matrix ng, continued fraction n) step by step in the Icon programming language