How to resolve the algorithm Perfect numbers step by step in the Modula-2 programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perfect numbers step by step in the Modula-2 programming language

Table of Contents

Problem Statement

Write a function which says whether a number is perfect.

A perfect number is a positive integer that is the sum of its proper positive divisors excluding the number itself. Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself).

Note:   The faster   Lucas-Lehmer test   is used to find primes of the form   2n-1,   all known perfect numbers can be derived from these primes using the formula   (2n - 1) × 2n - 1. It is not known if there are any odd perfect numbers (any that exist are larger than 102000). The number of   known   perfect numbers is   51   (as of December, 2018),   and the largest known perfect number contains  49,724,095  decimal digits.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect numbers step by step in the Modula-2 programming language

Source code in the modula-2 programming language

MODULE PerfectNumbers;

FROM SWholeIO IMPORT
  WriteCard;
FROM STextIO IMPORT
  WriteLn;
FROM RealMath IMPORT
  sqrt;

VAR
  N: CARDINAL;

PROCEDURE IsPerfect(N: CARDINAL): BOOLEAN;
VAR
  S, I: CARDINAL;
  SqrtN: REAL;
BEGIN
  S := 1;
  SqrtN := sqrt(FLOAT(N));
  IF N REM 2 = 0 THEN
    S := S + 2 + N / 2;
  END;
  I := 3;
  WHILE FLOAT(I) <= SqrtN - 1.0 DO
    IF N REM I = 0 THEN
      S := S + I + N / I;
    END;
    I := I + 1;
  END;
  IF I * I = N THEN
    S := S + I;
  END;
  RETURN (N = S);
END IsPerfect;

BEGIN
  FOR N := 2 TO 10000 BY 2 DO
    IF IsPerfect(N) THEN
      WriteCard(N, 5);
      WriteLn;
    END;
  END;
END PerfectNumbers.


  

You may also check:How to resolve the algorithm Digital root step by step in the Modula-2 programming language
You may also check:How to resolve the algorithm Partition an integer x into n primes step by step in the zkl programming language
You may also check:How to resolve the algorithm Numerical integration/Gauss-Legendre Quadrature step by step in the Scala programming language
You may also check:How to resolve the algorithm SHA-256 step by step in the C# programming language
You may also check:How to resolve the algorithm Goldbach's comet step by step in the Phix programming language