How to resolve the algorithm Narcissistic decimal number step by step in the REXX programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Narcissistic decimal number step by step in the REXX 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 REXX programming language
Source code in the rexx programming language
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
#=0 /*number of narcissistic numbers so far*/
do j=0 until #==N; L=length(j) /*get length of the J decimal number.*/
$=left(j, 1) **L /*1st digit in J raised to the L pow.*/
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
$=$ + substr(j, k, 1) ** L /*add digit raised to power to the sum.*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if $\==j then iterate /*does the sum equal to J? No, skip it*/
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /*stick a fork in it, we're all done. */
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
do p=1 for 39 /*generate tables: digits ^ P power. */
do i=0 for 10; @.p.i= i**p /*build table of ten digits ^ P power. */
end /*i*/
end /*w*/ /* [↑] table is a fixed (limited) size*/
#=0 /*number of narcissistic numbers so far*/
do j=0 until #==N; L=length(j) /*get length of the J decimal number.*/
_=left(j, 1) /*select the first decimal digit to sum*/
$=@.L._ /*sum of the J dec. digits ^ L (so far)*/
do k=2 for L-1 until $>j /*perform for each decimal digit in J.*/
_=substr(j, k, 1) /*select the next decimal digit to sum.*/
$=$ + @.L._ /*add dec. digit raised to power to sum*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if $\==j then iterate /*does the sum equal to J? No, skip it*/
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /*stick a fork in it, we're all done. */
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
@.=0 /*set default for the @ stemmed array. */
#=0 /*number of narcissistic numbers so far*/
do p=0 for 39+1; if p<10 then call tell p /*display the 1st 1─digit dec. numbers.*/
do i=1 for 9; @.p.i= i**p /*build table of ten digits ^ P power. */
end /*i*/
end /*p*/ /* [↑] table is a fixed (limited) size*/
/* [↓] skip the 2─digit dec. numbers. */
do j=100; L=length(j) /*get length of the J decimal number.*/
parse var j _1 2 _2 3 m '' -1 _R /*get 1st, 2nd, middle, last dec. digit*/
$=@.L._1 + @.L._2 + @.L._R /*sum of the J decimal digs^L (so far).*/
do k=3 for L-3 until $>j /*perform for other decimal digits in J*/
parse var m _ +1 m /*get next dec. dig in J, start at 3rd.*/
$=$ + @.L._ /*add dec. digit raised to pow to sum. */
end /*k*/ /* [↑] calculate the rest of the sum. */
if $==j then do; call tell j /*does the sum equal to J? Show the #*/
if #==n then leave /*does the sum equal to J? Show the #*/
end
end /*j*/ /* [↑] the J loop list starts at 100*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: #=# + 1 /*bump the counter for narcissistic #s.*/
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/
/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
if N=='' | N=="," then N=25 /*Not specified? Then use the default.*/
N=min(N, 89) /*there are only 89 narcissistic #s. */
@.=0 /*set default for the @ stemmed array. */
#=0 /*number of narcissistic numbers so far*/
do p=0 for 39+1; if p<10 then call tell p /*display the 1st 1─digit dec. numbers.*/
do i=1 for 9; @.p.i= i**p /*build table of ten digits ^ P power. */
zzj= '00'j; @.p.zzj= @.p.j /*assign value for a 3-dig number (LZ),*/
end /*i*/
do j=10 to 99; parse var j t 2 u /*obtain 2 decimal digits of J: T U */
@.p.j = @.p.t + @.p.u /*assign value for a 2─dig number. */
zj= '0'j; @.p.zj = @.p.j /* " " " " 3─dig " (LZ),*/
end /*j*/ /* [↑] T≡ tens digit; U≡ units digit.*/
do k=100 to 999; parse var k h 2 t 3 u /*obtain 3 decimal digits of J: H T U */
@.p.k= @.p.h + @.p.t + @.p.u /*assign value for a three-digit number*/
end /*k*/ /* [↑] H≡ hundreds digit; T≡ tens ···*/
end /*p*/ /* [↑] table is a fixed (limited) size*/
/* [↓] skip the 2─digit dec. numbers. */
do j=100; L=length(j) /*get length of the J decimal number.*/
parse var j _ +3 m /*get 1st three decimal digits of J. */
$=@.L._ /*sum of the J decimal digs^L (so far).*/
do while m\=='' /*do the rest of the dec. digs in J. */
parse var m _ +3 m /*get the next 3 decimal digits in M. */
$=$ + @.L._ /*add dec. digit raised to pow to sum. */
end /*while*/ /* [↑] calculate the rest of the sum. */
if $==j then do; call tell j /*does the sum equal to J? Show the #*/
if #==n then leave /*does the sum equal to J? Show the #*/
end
end /*j*/ /* [↑] the J loop list starts at 100*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
tell: #=# + 1 /*bump the counter for narcissistic #s.*/
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/
You may also check:How to resolve the algorithm Man or boy test step by step in the Visual Prolog programming language
You may also check:How to resolve the algorithm Hello world/Graphical step by step in the Visual Basic .NET programming language
You may also check:How to resolve the algorithm Vector products step by step in the Nemerle programming language
You may also check:How to resolve the algorithm Ackermann function step by step in the Objeck programming language
You may also check:How to resolve the algorithm Horner's rule for polynomial evaluation step by step in the Icon and Unicon programming language