Narcissistic decimal number: Difference between revisions

Content added Content deleted
m (→‎optimized, unrolled: changed the wording/highlighting in the REXX section header comment.)
m (→‎{{header|REXX}}: changed whitespace and comments, changed comments in the output section.)
Line 1,929:
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. */
Line 1,981 ⟶ 1,983:
_=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. */
Line 1,990 ⟶ 1,994:
end /*j*/ /* [↑] this list starts at 0 (zero).*/
/*stick a fork in it, we're all done. */</lang>
'''output''' &nbsp; is the same as 1st1<sup>st</sup> REXX version.
 
===optimized, unrolled===
Line 2,000 ⟶ 2,004:
parse arg N .; if N=='' then N=25 /*obtain the number of narcissistic #'s*/
N=min(N,89) /*there are only 89 narcissistic #s. */
@.=0 do w=1 for 39 /*generate tables:set default for digitsthe ^@ Lstemmed powerarray. */
do i=0 for 10; @.w.i=i**w; end /*build table of ten digits ^ L power. */
end /*w*/ /* [↑] table is a fixed (limited) size*/
#=0 /*number of narcissistic numbers so far*/
do loww=0 for 10;39+1 call tell low; end /*handlegenerate tables: the 1stdigits 1─digit^ dec.L numberspower. */
if w<10 then call tell w /*display the 1st 1─digit dec. numbers.*/
do i=01 for 109; @.w.i=i**w; end /*build table of ten digits ^ L power. */
end /*w*/ /* [↑] 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 call tell j /*does the sum equal to J? Show the #*/
end /*j*/ /* [↑] the J loop list starts at 100*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────TELL subroutine───────────────────────────*/
tell: #=#+1 /*bump the counter for narcissistic #s.*/
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==N then exit /*stick a fork in it, we're all done. */
return /*return to invoker & keep on truckin'.*/</lang>
'''output''' &nbsp; is the same as 1st1<sup>st</sup> REXX version.
 
=={{header|Ruby}}==