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: | Line 1,929: | ||
N=min(N,89) /*there are only 89 narcissistic #s. */ |
N=min(N,89) /*there are only 89 narcissistic #s. */ |
||
#=0 /*number of narcissistic numbers so far*/ |
#=0 /*number of narcissistic numbers so far*/ |
||
do j=0 until #==N; L=length(j) /*get length of the J |
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.*/ |
$=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.*/ |
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.*/ |
$=$ + substr(j,k,1)**L /*add digit raised to power to the sum.*/ |
||
end /*k*/ /* [↑] calculate the rest of the sum. */ |
end /*k*/ /* [↑] calculate the rest of the sum. */ |
||
if $\==j then iterate /*does the sum equal to J? No, skip it*/ |
if $\==j then iterate /*does the sum equal to J? No, skip it*/ |
||
#=#+1 /*bump count of narcissistic numbers. */ |
#=#+1 /*bump count of narcissistic numbers. */ |
||
Line 1,981: | Line 1,983: | ||
_=left(j,1) /*select the first decimal digit to sum*/ |
_=left(j,1) /*select the first decimal digit to sum*/ |
||
$=@.L._ /*sum of the J dec. digits ^ L (so far)*/ |
$=@.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.*/ |
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.*/ |
_=substr(j,k,1) /*select the next decimal digit to sum.*/ |
||
$=$+@.L._ /*add dec. digit raised to power to sum*/ |
$=$+@.L._ /*add dec. digit raised to power to sum*/ |
||
end /*k*/ /* [↑] calculate the rest of the sum. */ |
end /*k*/ /* [↑] calculate the rest of the sum. */ |
||
if $\==j then iterate /*does the sum equal to J? No, skip it*/ |
if $\==j then iterate /*does the sum equal to J? No, skip it*/ |
||
#=#+1 /*bump count of narcissistic numbers. */ |
#=#+1 /*bump count of narcissistic numbers. */ |
||
Line 1,990: | Line 1,994: | ||
end /*j*/ /* [↑] this list starts at 0 (zero).*/ |
end /*j*/ /* [↑] this list starts at 0 (zero).*/ |
||
/*stick a fork in it, we're all done. */</lang> |
/*stick a fork in it, we're all done. */</lang> |
||
'''output''' is the same as |
'''output''' is the same as 1<sup>st</sup> REXX version. |
||
===optimized, unrolled=== |
===optimized, unrolled=== |
||
Line 2,000: | Line 2,004: | ||
parse arg N .; if N=='' then N=25 /*obtain the number of narcissistic #'s*/ |
parse arg N .; if N=='' then N=25 /*obtain the number of narcissistic #'s*/ |
||
N=min(N,89) /*there are only 89 narcissistic #s. */ |
N=min(N,89) /*there are only 89 narcissistic #s. */ |
||
@.=0 /*set default for the @ stemmed array. */ |
|||
⚫ | |||
⚫ | |||
#=0 /*number of narcissistic numbers so far*/ |
#=0 /*number of narcissistic numbers so far*/ |
||
do |
do w=0 for 39+1 /*generate tables: digits ^ L power. */ |
||
if w<10 then call tell w /*display the 1st 1─digit dec. numbers.*/ |
|||
⚫ | |||
⚫ | |||
/* [↓] skip the 2─digit dec. numbers. */ |
/* [↓] skip the 2─digit dec. numbers. */ |
||
do j=100; L=length(j) |
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*/ |
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).*/ |
$=@.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*/ |
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.*/ |
parse var m _ +1 m /*get next dec. dig in J, start at 3rd.*/ |
||
$=$ + @.L._ /*add dec. digit raised to pow to sum. */ |
$=$ + @.L._ /*add dec. digit raised to pow to sum. */ |
||
end /*k*/ /* [↑] calculate the rest of the sum. */ |
end /*k*/ /* [↑] calculate the rest of the sum. */ |
||
if $==j then call tell j /*does the sum equal to J? Show the #*/ |
if $==j then call tell j /*does the sum equal to J? Show the #*/ |
||
end /*j*/ /* [↑] the J loop list starts at 100*/ |
end /*j*/ /* [↑] the J loop list starts at 100*/ |
||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*────────────────────────────────────────────────────────────────────────────*/ |
|||
/*──────────────────────────────────TELL subroutine───────────────────────────*/ |
|||
tell: #=#+1 /*bump the counter for narcissistic #s.*/ |
tell: #=#+1 /*bump the counter for narcissistic #s.*/ |
||
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
||
if #==N then exit /*stick a fork in it, we're all done. */ |
if #==N then exit /*stick a fork in it, we're all done. */ |
||
return /*return to invoker & keep on truckin'.*/</lang> |
return /*return to invoker & keep on truckin'.*/</lang> |
||
'''output''' is the same as |
'''output''' is the same as 1<sup>st</sup> REXX version. |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |