Narcissistic decimal number: Difference between revisions
Content added Content deleted
m (→Haskell :: reduced search: Orthography) |
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
||
Line 610: | Line 610: | ||
length 18: |
length 18: |
||
^C |
^C |
||
</pre> |
|||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include <iostream> |
|||
#include <vector> |
|||
using namespace std; |
|||
typedef unsigned int uint; |
|||
class NarcissisticDecs |
|||
{ |
|||
public: |
|||
void makeList( int mx ) |
|||
{ |
|||
uint st = 0, tl; int pwr = 0, len; |
|||
while( narc.size() < mx ) |
|||
{ |
|||
len = getDigs( st ); |
|||
if( pwr != len ) |
|||
{ |
|||
pwr = len; |
|||
fillPower( pwr ); |
|||
} |
|||
tl = 0; |
|||
for( int i = 1; i < 10; i++ ) |
|||
tl += static_cast<uint>( powr[i] * digs[i] ); |
|||
if( tl == st ) narc.push_back( st ); |
|||
st++; |
|||
} |
|||
} |
|||
void display() |
|||
{ |
|||
for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ ) |
|||
cout << *i << " "; |
|||
cout << "\n\n"; |
|||
} |
|||
private: |
|||
int getDigs( uint st ) |
|||
{ |
|||
memset( digs, 0, 10 * sizeof( int ) ); |
|||
int r = 0; |
|||
while( st ) |
|||
{ |
|||
digs[st % 10]++; |
|||
st /= 10; |
|||
r++; |
|||
} |
|||
return r; |
|||
} |
|||
void fillPower( int z ) |
|||
{ |
|||
for( int i = 1; i < 10; i++ ) |
|||
powr[i] = pow( static_cast<float>( i ), z ); |
|||
} |
|||
vector<uint> narc; |
|||
uint powr[10]; |
|||
int digs[10]; |
|||
}; |
|||
int main( int argc, char* argv[] ) |
|||
{ |
|||
NarcissisticDecs n; |
|||
n.makeList( 25 ); |
|||
n.display(); |
|||
return system( "pause" ); |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 |
|||
</pre> |
</pre> |
||
Line 1,080: | Line 1,005: | ||
25 31: 1550475334214501539088894 1553242162893771850669378 3706907995955475988644380 3706907995955475988644381 4422095118095899619457938 |
25 31: 1550475334214501539088894 1553242162893771850669378 3706907995955475988644380 3706907995955475988644381 4422095118095899619457938 |
||
Total elasped: 30.5658944 seconds</pre> |
Total elasped: 30.5658944 seconds</pre> |
||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include <iostream> |
|||
#include <vector> |
|||
using namespace std; |
|||
typedef unsigned int uint; |
|||
class NarcissisticDecs |
|||
{ |
|||
public: |
|||
void makeList( int mx ) |
|||
{ |
|||
uint st = 0, tl; int pwr = 0, len; |
|||
while( narc.size() < mx ) |
|||
{ |
|||
len = getDigs( st ); |
|||
if( pwr != len ) |
|||
{ |
|||
pwr = len; |
|||
fillPower( pwr ); |
|||
} |
|||
tl = 0; |
|||
for( int i = 1; i < 10; i++ ) |
|||
tl += static_cast<uint>( powr[i] * digs[i] ); |
|||
if( tl == st ) narc.push_back( st ); |
|||
st++; |
|||
} |
|||
} |
|||
void display() |
|||
{ |
|||
for( vector<uint>::iterator i = narc.begin(); i != narc.end(); i++ ) |
|||
cout << *i << " "; |
|||
cout << "\n\n"; |
|||
} |
|||
private: |
|||
int getDigs( uint st ) |
|||
{ |
|||
memset( digs, 0, 10 * sizeof( int ) ); |
|||
int r = 0; |
|||
while( st ) |
|||
{ |
|||
digs[st % 10]++; |
|||
st /= 10; |
|||
r++; |
|||
} |
|||
return r; |
|||
} |
|||
void fillPower( int z ) |
|||
{ |
|||
for( int i = 1; i < 10; i++ ) |
|||
powr[i] = pow( static_cast<float>( i ), z ); |
|||
} |
|||
vector<uint> narc; |
|||
uint powr[10]; |
|||
int digs[10]; |
|||
}; |
|||
int main( int argc, char* argv[] ) |
|||
{ |
|||
NarcissisticDecs n; |
|||
n.makeList( 25 ); |
|||
n.display(); |
|||
return system( "pause" ); |
|||
} |
|||
</lang> |
|||
{{out}} |
|||
<pre> |
|||
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 |
|||
</pre> |
|||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Line 1,962: | Line 1,962: | ||
[0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315] |
[0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315] |
||
</pre> |
</pre> |
||
=={{header|GW-BASIC}}== |
=={{header|GW-BASIC}}== |
||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
Line 2,136: | Line 2,137: | ||
<lang j> (#~ isNarc) i.1e7 NB. display Narcissistic numbers |
<lang j> (#~ isNarc) i.1e7 NB. display Narcissistic numbers |
||
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</lang> |
0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</lang> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
Line 2,872: | Line 2,874: | ||
say $i++; |
say $i++; |
||
}</lang> |
}</lang> |
||
=={{header|Perl 6}}== |
|||
Here is a straightforward, naive implementation. It works but takes ages. |
|||
<lang perl6>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars } |
|||
for 0 .. * { |
|||
if .&is-narcissistic { |
|||
.say; |
|||
last if ++state$ >= 25; |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>0 |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
7 |
|||
8 |
|||
9 |
|||
153 |
|||
370 |
|||
371 |
|||
407 |
|||
Ctrl-C</pre> |
|||
Here the program was interrupted but if you're patient enough you'll see all the 25 numbers. |
|||
Here's a faster version that precalculates the values for base 1000 digits: |
|||
<lang perl6>sub kigits($n) { |
|||
my int $i = $n; |
|||
my int $b = 1000; |
|||
gather while $i { |
|||
take $i % $b; |
|||
$i = $i div $b; |
|||
} |
|||
} |
|||
for (1..*) -> $d { |
|||
my @t = 0..9 X** $d; |
|||
my @table = @t X+ @t X+ @t; |
|||
sub is-narcissistic(\n) { n == [+] @table[kigits(n)] }; |
|||
state $l = 2; |
|||
FIRST say "1\t0"; |
|||
say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d; |
|||
last if $l > 25 |
|||
};</lang> |
|||
{{out}} |
|||
<pre>1 0 |
|||
2 1 |
|||
3 2 |
|||
4 3 |
|||
5 4 |
|||
6 5 |
|||
7 6 |
|||
8 7 |
|||
9 8 |
|||
10 9 |
|||
11 153 |
|||
12 370 |
|||
13 371 |
|||
14 407 |
|||
15 1634 |
|||
16 8208 |
|||
17 9474 |
|||
18 54748 |
|||
19 92727 |
|||
20 93084 |
|||
21 548834 |
|||
22 1741725 |
|||
23 4210818 |
|||
24 9800817 |
|||
25 9926315</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 3,624: | Line 3,551: | ||
{{out}} |
{{out}} |
||
<pre>'(0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 93084 92727 548834 1741725 4210818 9800817 9926315)</pre> |
<pre>'(0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 93084 92727 548834 1741725 4210818 9800817 9926315)</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Here is a straightforward, naive implementation. It works but takes ages. |
|||
<lang perl6>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars } |
|||
for 0 .. * { |
|||
if .&is-narcissistic { |
|||
.say; |
|||
last if ++state$ >= 25; |
|||
} |
|||
}</lang> |
|||
{{out}} |
|||
<pre>0 |
|||
1 |
|||
2 |
|||
3 |
|||
4 |
|||
5 |
|||
6 |
|||
7 |
|||
8 |
|||
9 |
|||
153 |
|||
370 |
|||
371 |
|||
407 |
|||
Ctrl-C</pre> |
|||
Here the program was interrupted but if you're patient enough you'll see all the 25 numbers. |
|||
Here's a faster version that precalculates the values for base 1000 digits: |
|||
<lang perl6>sub kigits($n) { |
|||
my int $i = $n; |
|||
my int $b = 1000; |
|||
gather while $i { |
|||
take $i % $b; |
|||
$i = $i div $b; |
|||
} |
|||
} |
|||
for (1..*) -> $d { |
|||
my @t = 0..9 X** $d; |
|||
my @table = @t X+ @t X+ @t; |
|||
sub is-narcissistic(\n) { n == [+] @table[kigits(n)] }; |
|||
state $l = 2; |
|||
FIRST say "1\t0"; |
|||
say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d; |
|||
last if $l > 25 |
|||
};</lang> |
|||
{{out}} |
|||
<pre>1 0 |
|||
2 1 |
|||
3 2 |
|||
4 3 |
|||
5 4 |
|||
6 5 |
|||
7 6 |
|||
8 7 |
|||
9 8 |
|||
10 9 |
|||
11 153 |
|||
12 370 |
|||
13 371 |
|||
14 407 |
|||
15 1634 |
|||
16 8208 |
|||
17 9474 |
|||
18 54748 |
|||
19 92727 |
|||
20 93084 |
|||
21 548834 |
|||
22 1741725 |
|||
23 4210818 |
|||
24 9800817 |
|||
25 9926315</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 4,013: | Line 4,016: | ||
End Sub</lang>{{out}} |
End Sub</lang>{{out}} |
||
<pre>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315</pre> |
<pre>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315</pre> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<lang vb>Function Narcissist(n) |
<lang vb>Function Narcissist(n) |
||
Line 4,057: | Line 4,061: | ||
L(548834,1741725,4210818,9800817,9926315) |
L(548834,1741725,4210818,9800817,9926315) |
||
</pre> |
</pre> |
||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
Array index starts at 1. Only 1 character long variable names are allowed for For-Next loops. 8 Digits or higher numbers are displayed as floating point numbers. Needs about 2 hours (3.5Mhz) |
Array index starts at 1. Only 1 character long variable names are allowed for For-Next loops. 8 Digits or higher numbers are displayed as floating point numbers. Needs about 2 hours (3.5Mhz) |