How to resolve the algorithm Amicable pairs step by step in the ReScript programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Amicable pairs step by step in the ReScript 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 ReScript programming language
Source code in the rescript programming language
let isqrt = (v) => {
Belt.Float.toInt(
sqrt(Belt.Int.toFloat(v)))
}
let sum_divs = (n) => {
let sum = ref(1)
for d in 2 to isqrt(n) {
if mod(n, d) == 0 {
sum.contents = sum.contents + (n / d + d)
}
}
sum.contents
}
{
for n in 2 to 20000 {
let m = sum_divs(n)
if (m > n) {
if sum_divs(m) == n {
Printf.printf("%d %d\n", n, m)
}
}
}
}
You may also check:How to resolve the algorithm Distributed programming step by step in the C# programming language
You may also check:How to resolve the algorithm Sorting algorithms/Patience sort step by step in the JavaScript programming language
You may also check:How to resolve the algorithm Perfect numbers step by step in the Fortran programming language
You may also check:How to resolve the algorithm Smith numbers step by step in the M2000 Interpreter programming language
You may also check:How to resolve the algorithm Determine if a string has all the same characters step by step in the Erlang programming language