How to resolve the algorithm Perfect totient numbers step by step in the Delphi programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Perfect totient numbers step by step in the Delphi programming language

Table of Contents

Problem Statement

Generate and show here, the first twenty Perfect totient numbers.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Perfect totient numbers step by step in the Delphi programming language

Source code in the delphi programming language

program Perfect_totient_numbers;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

function totient(n: Integer): Integer;
begin
  var tot := n;
  var i := 2;
  while i * i <= n do
  begin
    if (n mod i) = 0 then
    begin
      while (n mod i) = 0 do
        n := n div i;
      dec(tot, tot div i);
    end;
    if i = 2 then
      i := 1;
    inc(i, 2);
  end;
  if n > 1 then
    dec(tot, tot div n);
  Result := tot;
end;

begin
  var perfect: TArray;
  var n := 1;
  while length(perfect) < 20 do
  begin
    var tot := n;
    var sum := 0;
    while tot <> 1 do
    begin
      tot := totient(tot);
      inc(sum, tot);
    end;
    if sum = n then
    begin
      SetLength(perfect, Length(perfect) + 1);
      perfect[High(perfect)] := n;
    end;
    inc(n, 2);
  end;
  writeln('The first 20 perfect totient numbers are:');
  write('[');
  for var e in perfect do
    write(e, ' ');
  writeln(']');
  readln;
end.


  

You may also check:How to resolve the algorithm Catamorphism step by step in the Déjà Vu programming language
You may also check:How to resolve the algorithm Include a file step by step in the PureBasic programming language
You may also check:How to resolve the algorithm Factors of an integer step by step in the NetRexx programming language
You may also check:How to resolve the algorithm Hailstone sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Law of cosines - triples step by step in the FreeBASIC programming language