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
$=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''' is the same as
===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 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
if w<10 then call tell w /*display the 1st 1─digit dec. numbers.*/
▲ end /*w*/ /* [↑] table is a fixed (limited) size*/
/* [↓] skip the 2─digit dec. numbers. */
do j=100; L=length(j)
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: #=#+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''' is the same as
=={{header|Ruby}}==
|