How to resolve the algorithm Narcissistic decimal number step by step in the Delphi programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Narcissistic decimal number step by step in the Delphi programming language
Table of Contents
Problem Statement
A Narcissistic decimal number is a non-negative integer,
n
{\displaystyle n}
, that is equal to the sum of the
m
{\displaystyle m}
-th powers of each of the digits in the decimal representation of
n
{\displaystyle n}
, where
m
{\displaystyle m}
is the number of digits in the decimal representation of
n
{\displaystyle n}
.
Narcissistic (decimal) numbers are sometimes called Armstrong numbers, named after Michael F. Armstrong. They are also known as Plus Perfect numbers.
Generate and show here the first 25 narcissistic decimal numbers.
Note:
0
1
= 0
{\displaystyle 0^{1}=0}
, the first in the series.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Narcissistic decimal number step by step in the Delphi programming language
Source code in the delphi programming language
function IntPower(N,P: integer): integer;
var I: integer;
begin
Result:=N;
for I:=1 to P-1 do Result:=Result * N;
end;
function IsNarcisNumber(N: integer): boolean;
{Test if this a narcisstic number}
{i.e. the sum of each digit raised to power length = N}
var S: string;
var I,Sum,B,P: integer;
begin
S:=IntToStr(N);
Sum:=0;
P:=Length(S);
for I:=1 to Length(S) do
begin
B:=byte(S[I])-$30;
Sum:=Sum+IntPower(B,P);
end;
Result:=Sum=N;
end;
procedure ShowNarcisNumber(Memo: TMemo);
{Show first 25 narcisstic number}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=0 to High(Integer) do
if IsNarcisNumber(I) then
begin
S:=S+Format('%10d',[I]);
Inc(Cnt);
if (Cnt mod 5)=0 then S:=S+#$0D#$0A;
if Cnt>=25 then break;
end;
Memo.Lines.Add(S);
end;
You may also check:How to resolve the algorithm Abundant odd numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Smarandache prime-digital sequence step by step in the Raku programming language
You may also check:How to resolve the algorithm Roman numerals/Encode step by step in the Oz programming language
You may also check:How to resolve the algorithm Sequence: smallest number with exactly n divisors step by step in the Nim programming language
You may also check:How to resolve the algorithm Read a file line by line step by step in the Tcl programming language