Narcissistic decimal number: Difference between revisions

→‎{{header|REXX}}: changed/added whitespace and comments, improved speed of the 3rd REXX pgm by using PARSE instead of SUBSTR for extracting decimal digits of a number.
m (added a note about Armstrong numbers.)
(→‎{{header|REXX}}: changed/added whitespace and comments, improved speed of the 3rd REXX pgm by using PARSE instead of SUBSTR for extracting decimal digits of a number.)
Line 1,801:
 
=={{header|REXX}}==
===idomaticidiomatic===
<lang rexx>/*REXX programpgm to generategenerates and displaydisplays a number of narcissistic (Armstrong) numbers.*/
numeric digits 39 /*be able to handle the largest Armstrong #*/
parse arg N .; if N=='' then N=25 /*getobtain the number of narcissistic #'s.*/
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 the length of the J (decimal) number.*/
s$=left(j,1)**L /*1st digit in J raised to the L pow.*/
do k=2 for L-1 until s$>j /*perform for each decimal digit in J. */
s$=s$ + substr(j,k,1)**L /*add digit raised to powpower to the sum.*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if s$\==j then iterate /*does the sum equal to J? No, ···skip it*/
#=#+1 /*bump thecount of narcissistic numnumbers. count*/
say right(#,9) ' narcissistic:' j /*display index &and narcissistic #.number*/
end /*j*/ /* [↑] this list starts at 0. (zero).*/
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; when using the default input:
<pre>
Line 1,848:
===optimized===
This REXX version is optimized to pre-compute all the ten (single) digits raised to all possible powers (which is 39).
<lang rexx>/*REXX programpgm to generategenerates and displaydisplays a number of narcissistic (Armstrong) numbers.*/
numeric digits 39 /*be able to handle the largest Armstrong #*/
parse arg N .; if N=='' then N=25 /*getobtain the number of narcissistic #'s.*/
N=min(N,89) /*there are only 89 narcissistic #s. */
do w=1 for 39 /*generate tables: digits ^ L powpower. */
do i=0 for 10; @.w.i=i**w; end /*build table of 10ten digsdigits ^ L powpower. */
end /*w*/ /* [↑] table is of a fixed (limited) size.*/
#=0 /*number of narcissistic #numbers so far*/
do j=0 until #==N; L=length(j) /*get the length of the J decimal number.*/
_=left(j,1) /*select the first decimal digit to sum. */
s$=@.L._ /*sum of the J digsdec. digits ^ L (so far)*/
do k=2 for L-1 until s$>j /*perform for each decimal digit in J. */
_=substr(j,k,1) /*select the next decimal digit to sum. */
s$=s$+@.L._ /*add dec. digit raised to powpower to sum.*/
end /*k*/ /* [↑] calculate the rest of the sum. */
if s$\==j then iterate /*does the sum equal to J? No, ···skip it*/
#=#+1 /*bump thecount of narcissistic numnumbers. count*/
say right(#,9) ' narcissistic:' j /*display index &and narcissistic #.number*/
end /*j*/ /* [↑] this list starts at 0. (zero).*/
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is the same as 1st REXX version.
 
Line 1,873:
This REXX version is optimized by unrolling part of the DO loop that sums the digits.
<br>The unrolling also necessitated the special handling of one- and two-digit narcissistic numbers.
<lang rexx>/*REXX programpgm to generategenerates and displaydisplays a number of narcissistic (Armstrong) numbers.*/
numeric digits 39 /*be able to handle the largest Armstrong #*/
parse arg N .; if N=='' then N=25 /*getobtain the number of narcissistic #'s.*/
N=min(N,89) /*there are only 89 narcissistic #s. */
do w=1 for 39 /*generate tables: digits ^ L powpower. */
do i=0 for 10; @.w.i=i**w; end /*build table of 10ten digsdigits ^ L powpower. */
end /*w*/ /* [↑] table is of a fixed (limited) size.*/
#=0 /*number of narcissistic #numbers so far*/
do low=0 for 10; call tell low; end /*handle the first1st one-digit1─digit dec. numbers. nums*/
/* [↓] skip the 2-digit2─digit dec. numbers. */
do j=100; L=length(j) /*get the length of the J decimal number. */
_1=left(j,1);parse var _2=substr(j, _1 2,1) _2 3 m '' -1 _R /*selectget 1st &, 2nd, digitmiddle, tolast sumdec. digit*/
_R$=right(j,1) @.L._1 + @.L._2 + @.L._R /*selectsum of the rightJ digitdecimal todigs^L sum.(so far).*/
s=@.L._1 + @.L._2 + @.L._R do k=3 for L-3 /*sum ofuntil the$>j J/*perform digsfor ^other Ldecimal digits (soin far)J*/
parse var m _ +1 m do k=3 for L-3 until s>j /*performget fornext eachdec. digitdig in J., start at 3rd.*/
$=$ + @.L._=substr(j,k,1) /*selectadd the nextdec. digit raised to pow to sum. */
end s=s + @.L._/*k*/ /*add digit[↑] raised tocalculate powthe torest of the sum. */
if $==j then call tell j end /*k*/ /*does the sum equal to J? /* [↑] calculateShow the rest of sum#*/
ifend s== /*j*/ then call tell j /*does sum[↑] equal to this J?list starts at Yes0 ···(zero).*/
exit end /*j*/ /*stick [↑] a fork in thisit, list startswe're atall 0done. */
/*──────────────────────────────────TELL subroutine─────────────────────subroutine───────────────────────────*/
exit /*stick a fork in it, we're done.*/
tell: #=#+1 /*bump the counter for narcissistic #s.*/
/*──────────────────────────────────TELL subroutine─────────────────────*/
say right(#,9) ' narcissistic:' y arg(1) /*display index &and narcissistic #.number*/
tell: parse arg y /*get narcissistic # to display. */
if #=#+1=N then exit /*stick a fork in it, we're all /*bump the narcissistic # countdone. */
exit return /*stickreturn ato forkinvoker in& it,keep weon truckin're done.*/</lang>
say right(#,9) ' narcissistic:' y /*display index & narcissistic #.*/
if #==N then exit /*stick a fork in it, we're done.*/
return /*return and keep on truckin'. */</lang>
'''output''' &nbsp; is the same as 1st REXX version.