How to resolve the algorithm Amicable pairs step by step in the Elena programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Amicable pairs step by step in the Elena 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 Elena programming language
Source code in the elena programming language
import extensions;
import system'routines;
const int N = 20000;
extension op
{
ProperDivisors
= Range.new(1,self / 2).filterBy:(n => self.mod:n == 0);
get AmicablePairs()
{
var divsums := Range
.new(0, self + 1)
.selectBy:(i => i.ProperDivisors.summarize(Integer.new()))
.toArray();
^ 1.repeatTill(divsums.Length)
.filterBy:(i)
{
var ii := i;
var sum := divsums[i];
^ (i < sum) && (sum < divsums.Length) && (divsums[sum] == i)
}
.selectBy:(i => new { Item1 = i; Item2 = divsums[i]; })
}
}
public program()
{
N.AmicablePairs.forEach:(pair)
{
console.printLine(pair.Item1, " ", pair.Item2)
}
}
import extensions;
import system'routines'stex;
import system'collections;
const int N = 20000;
extension op : IntNumber
{
Enumerator ProperDivisors
= new Range(1,self / 2).filterBy:(int n => self.mod:n == 0);
get AmicablePairs()
{
auto divsums := new List(
cast Enumerator(
new Range(0, self).selectBy:(int i => i.ProperDivisors.summarize(0))));
^ new Range(0, divsums.Length)
.filterBy:(int i)
{
auto sum := divsums[i];
^ (i < sum) && (sum < divsums.Length) && (divsums[sum] == i)
}
.selectBy:(int i => new Tuple(i,divsums[i]));
}
}
public program()
{
N.AmicablePairs.forEach:(var Tuple pair)
{
console.printLine(pair.Item1, " ", pair.Item2)
}
}
import extensions;
import system'routines'stex;
import system'collections;
const int Limit = 20000;
singleton ProperDivisors
{
Enumerator function(int number)
= Range.new(1, number / 2).filterBy:(int n => number.mod:n == 0);
}
public sealed AmicablePairs
{
int max;
constructor(int max)
{
this max := max
}
yieldable Tuple next()
{
List divsums := Range.new(0, max + 1).selectBy:(int i => ProperDivisors(i).summarize(0));
for (int i := 1, i < divsums.Length, i += 1)
{
int sum := divsums[i];
if(i < sum && sum <= divsums.Length && divsums[sum] == i) {
$yield new Tuple(i, sum);
}
};
^ nil
}
}
public program()
{
auto e := new AmicablePairs(Limit);
for(auto pair := e.next(), pair != nil)
{
console.printLine(pair.Item1, " ", pair.Item2)
}
}
You may also check:How to resolve the algorithm Sockets step by step in the R programming language
You may also check:How to resolve the algorithm Digital root step by step in the DCL programming language
You may also check:How to resolve the algorithm Sum of a series step by step in the Lingo programming language
You may also check:How to resolve the algorithm Guess the number step by step in the LFE programming language
You may also check:How to resolve the algorithm Averages/Arithmetic mean step by step in the Lingo programming language