How to resolve the algorithm Amicable pairs step by step in the Oberon-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Amicable pairs step by step in the Oberon-2 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 Oberon-2 programming language

Source code in the oberon-2 programming language

MODULE AmicablePairs;
IMPORT 
  Out;
CONST
  max = 20000;

VAR
  i,j: INTEGER;
  pd: ARRAY max + 1 OF LONGINT;

PROCEDURE ProperDivisorsSum(n: LONGINT): LONGINT;
VAR
   i,sum: LONGINT;
BEGIN
  sum := 0;
  IF n > 1 THEN 
    INC(sum,1);i := 2;
    WHILE (i < n) DO
      IF (n MOD i) = 0 THEN INC(sum,i) END;
      INC(i)
    END
  END;
  RETURN sum
END ProperDivisorsSum;

BEGIN
  FOR i := 0 TO max DO
    pd[i] := ProperDivisorsSum(i)
  END;

  FOR i := 2 TO max DO
    FOR j := i + 1 TO max DO
      IF (pd[i] = j) & (pd[j] = i) THEN
         Out.Char('[');Out.Int(i,0);Out.Char(',');Out.Int(j,0);Out.Char("]");Out.Ln
      END
    END 
  END
END AmicablePairs.

  

You may also check:How to resolve the algorithm Strip a set of characters from a string step by step in the RPL programming language
You may also check:How to resolve the algorithm Averages/Simple moving average step by step in the Swift programming language
You may also check:How to resolve the algorithm Comma quibbling step by step in the jq programming language
You may also check:How to resolve the algorithm MD5 step by step in the Crystal programming language
You may also check:How to resolve the algorithm MD5 step by step in the Haskell programming language