Narcissistic decimal number: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(47 intermediate revisions by 26 users not shown)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F narcissists(m)
[Int] result
L(digits) 0..
Line 49:
print(n, end' ‘ ’)
I (L.index + 1) % 5 == 0
print()</langsyntaxhighlight>
 
{{out}}
Line 62:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
 
procedure Narcissistic is
Line 93:
Current := Current + 1;
end loop;
end Narcissistic;</langsyntaxhighlight>
 
{{out}}
Line 100:
=={{header|Agena}}==
Tested with Agena 2.9.5 Win32
<langsyntaxhighlight lang="agena">scope
# print the first 25 narcissistic numbers
 
Line 155:
io.writeline()
 
epocs</langsyntaxhighlight>
{{out}}
<pre>
Line 162:
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68"># find some narcissistic decimal numbers #
 
# returns TRUE if n is narcissitic, FALSE otherwise; n should be >= 0 #
Line 194:
FI
OD;
print( ( newline ) )</langsyntaxhighlight>
{{out}}
<pre>
Line 202:
=={{header|ALGOL W}}==
{{Trans|Agena}}
<langsyntaxhighlight lang="algolw">begin
% print the first 25 narcissistic numbers %
 
Line 267:
write()
 
end.</langsyntaxhighlight>
{{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|APL}}==
<syntaxhighlight lang="apl">
∇r ← digitsOf n;digitList
digitList ← ⍬
loop:→((⌊n)=0)/done
digitList ← digitList,(⌊n|⍨10)
n ← n÷10
→loop
done: r ← ⊖digitList
 
∇r ← getASN n;idx;list
idx ← 0
list ← 0⍴0
loop:
→(n=⍴list)/done
→(isArmstrongNumber idx)/add
→next
add:
list ← list,idx
next:
idx ← idx+1
→loop
done:
r ← list
 
∇r ← isArmstrongNumber n;digits;nd
digits ← digitsOf n ⍝⍝ (⍎¨⍕n) is equivalent, but about 45% slower!!
nd ← ≢ digits
r ← n = +/digits * nd
 
getASN 25
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
</syntaxhighlight>
 
=={{header|AppleScript}}==
Line 285 ⟶ 322:
For an imperative hand-optimisation, and a contrasting view, see the variant approach below :-)
 
<langsyntaxhighlight AppleScriptlang="applescript">------------------------- NARCISSI -----------------------
 
-- isDaffodil :: Int -> Int -> Bool
Line 456 ⟶ 493:
end script
end if
end mReturn</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight AppleScriptlang="applescript">{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}</langsyntaxhighlight>
----
 
Line 465 ⟶ 502:
When corrected actually to return the 25 numbers required by the task, the JavaScript/Haskell translation above takes seven minutes fifty-three seconds on my current machine. By contrast, the code here was written from scratch in AppleScript, takes the number of results required as its parameter rather than the numbers of digits in them, and returns the 25 numbers in just under a sixth of a second. The first 41 numbers take just under four-and-a-half seconds, the first 42 twenty-seven, and the first 44 a minute thirty-seven-and-a-half. The 43rd and 44th numbers are both displayed in Script Editor's result pane as 4.33828176939137E+15, but appear to be the correct values when tested. The JavaScript/Haskell translation's problems are certainly ''not'' due to AppleScript being "a little out of its depth here", but the narcissistic decimal numbers beyond the 44th are admittedly beyond the resolution of AppleScript's number classes.
 
<langsyntaxhighlight lang="applescript">(*
Return the first q narcissistic decimal numbers
(or as many of the q as can be represented by AppleScript number values).
Line 543 ⟶ 580:
end narcissisticDecimalNumbers
 
return narcissisticDecimalNumbers(25)</langsyntaxhighlight>
 
{{output}}
<langsyntaxhighlight lang="applescript">{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}</langsyntaxhighlight>
 
=={{header|Arturo}}==
{{trans|REXX}}
<syntaxhighlight lang="rebol">powers: map 0..9 'x [
map 0..9 'y [
x ^ y
]
]
 
getPair: function [p,sz].memoize[
if not? numeric? last p -> return powers\[to :integer to :string first p]\[sz]
return powers\[to :integer to :string first p]\[sz] + powers\[to :integer to :string last p]\[sz]
]
 
narcissistic?: function [n][
digs: digits n
sdigs: size digs
n = sum map split.every:2 to :string n 'p -> getPair p sdigs
]
 
i: new 0
counter: new 0
while [counter < 25][
if narcissistic? i [
print i
inc 'counter
]
inc 'i
]</syntaxhighlight>
 
{{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|AutoHotkey}}==
<syntaxhighlight lang="autohotkey">
<lang AutoHotkey>
#NoEnv ; Do not try to use environment variables
SetBatchLines, -1 ; Execute as quickly as you can
Line 592 ⟶ 686:
}
}
</syntaxhighlight>
</lang>
 
{{out}}
Line 609 ⟶ 703:
 
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f NARCISSISTIC_DECIMAL_NUMBER.AWK
BEGIN {
Line 626 ⟶ 720:
exit(0)
}
</syntaxhighlight>
</lang>
<p>output:</p>
<pre>
Line 632 ⟶ 726:
</pre>
 
=={{header|BefungeBASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
<syntaxhighlight lang="bbcbasic"> WHILE N% < 25
L%=LENSTR$I%
M%=0
J%=I%
WHILE J%
M%+=(J% MOD 10) ^ L%
J%/=10
ENDWHILE
IF I% == M% N%+=1 : PRINT N%, I%
I%+=1
ENDWHILE</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{trans|Go}}
Differently from the original version, no data structure for the result is used - why should it be?
<syntaxhighlight lang="basic">
100 rem Narcissistic decimal number
110 n = 25
120 dim power(9)
130 for i = 0 to 9
140 power(i) = i
150 next i
160 limit = 10
170 cnt = 0 : x = 0
180 while cnt < n
190 if x >= limit then
200 for i = 0 to 9
210 power(i) = power(i)*i
220 next i
230 limit = limit*10
240 endif
250 sum = 0 : xx = x
260 while xx > 0
270 sum = sum+power(xx mod 10)
280 xx = int(xx/10)
290 wend
300 if sum = x then print x; : cnt = cnt+1
310 x = x+1
320 wend
330 print
340 end
</syntaxhighlight>
{{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|Craft Basic}}===
<syntaxhighlight lang="basic">dim p[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
 
let l = 10
let n = 25
 
do
 
if c < n then
 
if x >= l then
 
for i = 0 to 9
 
let p[i] = p[i] * i
 
next i
 
let l = l * 10
 
endif
 
let s = 0
let y = x
 
do
 
if y > 0 then
 
let t = y % 10
let s = s + p[t]
let y = int(y / 10)
 
endif
 
wait
 
loop y > 0
 
if s = x then
 
print x
let c = c + 1
 
endif
 
let x = x + 1
 
endif
 
loop c < n
 
end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
====Simple Version====
<syntaxhighlight lang="freebasic">' normal version: 14-03-2017
' compile with: fbc -s console
' can go up to 18 digits (ulongint is 64bit), above 18 overflow will occur
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, a, b
Dim As Integer d()
Dim As ULongInt d2pow(0 To 9) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim As ULongInt x
Dim As String str_x
For n = 1 To 7
For n9 = n To 0 Step -1
For n8 = n-n9 To 0 Step -1
For n7 = n-n9-n8 To 0 Step -1
For n6 = n-n9-n8-n7 To 0 Step -1
For n5 = n-n9-n8-n7-n6 To 0 Step -1
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
x = n1 + n2*d2pow(2) + n3*d2pow(3) + n4*d2pow(4) + n5*d2pow(5)_
+ n6*d2pow(6) + n7*d2pow(7) + n8*d2pow(8) + n9*d2pow(9)
str_x = Str(x)
If Len(str_x) = n Then
ReDim d(10)
For a = 0 To n-1
d(Str_x[a]- Asc("0")) += 1
Next a
If n0 = d(0) AndAlso n1 = d(1) AndAlso n2 = d(2) AndAlso n3 = d(3)_
AndAlso n4 = d(4) AndAlso n5 = d(5) AndAlso n6 = d(6)_
AndAlso n7 = d(7) AndAlso n8 = d(8) AndAlso n9 = d(9) Then
Print x
End If
End If
Next n1
Next n2
Next n3
Next n4
Next n5
Next n6
Next n7
Next n8
Next n9
For a As Integer = 2 To 9
d2pow(a) = d2pow(a) * a
Next a
Next n
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>9
8
7
6
5
4
3
2
1
0
407
371
370
153
9474
8208
1634
93084
92727
54748
548834
9926315
9800817
4210818
1741725</pre>
 
====GMP Version====
<pre>It takes about 35 min. to find all 88 numbers (39 digits).
To go all the way it takes about 2 hours.</pre>
<syntaxhighlight lang="freebasic">' gmp version: 17-06-2015
' uses gmp
' compile with: fbc -s console
 
#Include Once "gmp.bi"
' change the number after max for the maximum n-digits you want (2 to 61)
#Define max 61
 
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
Dim As Integer i, j
Dim As UInteger d()
Dim As ZString Ptr gmp_str
gmp_str = Allocate(100)
 
' create gmp integer array,
Dim d2pow(9, max) As Mpz_ptr
' initialize array and set start value,
For i = 0 To 9
For j = 0 To max
d2pow(i, j) = Allocate(Len(__mpz_struct)) : Mpz_init(d2pow(i, j))
Next j
Next i
 
' gmp integers for to hold intermediate result
Dim As Mpz_ptr x1 = Allocate(Len(__mpz_struct)) : Mpz_init(x1)
Dim As Mpz_ptr x2 = Allocate(Len(__mpz_struct)) : Mpz_init(x2)
Dim As Mpz_ptr x3 = Allocate(Len(__mpz_struct)) : Mpz_init(x3)
Dim As Mpz_ptr x4 = Allocate(Len(__mpz_struct)) : Mpz_init(x4)
Dim As Mpz_ptr x5 = Allocate(Len(__mpz_struct)) : Mpz_init(x5)
Dim As Mpz_ptr x6 = Allocate(Len(__mpz_struct)) : Mpz_init(x6)
Dim As Mpz_ptr x7 = Allocate(Len(__mpz_struct)) : Mpz_init(x7)
Dim As Mpz_ptr x8 = Allocate(Len(__mpz_struct)) : Mpz_init(x8)
 
For n = 1 To max
 
For i = 1 To 9
'Mpz_set_ui(d2pow(i,0), 0)
Mpz_ui_pow_ui(d2pow(i,1), i, n)
For j = 2 To n
Mpz_mul_ui(d2pow(i, j), d2pow(i, 1), j)
Next j
Next i
 
For n9 = n To 0 Step -1
For n8 = n-n9 To 0 Step -1
Mpz_add(x8, d2pow(9, n9), d2pow(8, n8))
For n7 = n-n9-n8 To 0 Step -1
Mpz_add(x7, x8, d2pow(7, n7))
For n6 = n-n9-n8-n7 To 0 Step -1
Mpz_add(x6, x7, d2pow(6, n6))
For n5 = n-n9-n8-n7-n6 To 0 Step -1
Mpz_add(x5, x6, d2pow(5, n5))
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
Mpz_add(x4, x5, d2pow(4, n4))
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
Mpz_add(x3, x4, d2pow(3, n3))
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
Mpz_add(x2, x3, d2pow(2, n2))
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
Mpz_add_ui(x1, x2, n1)
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
 
Mpz_get_str(gmp_str, 10, x1)
 
If Len(*gmp_str) = n Then
ReDim d(10)
 
For i = 0 To n-1
d(gmp_str[i] - Asc("0")) += 1
Next i
 
If n9 = d(9) AndAlso n8 = d(8) AndAlso n7 = d(7) AndAlso n6 = d(6)_
AndAlso n5 = d(5) AndAlso n4 = d(4) AndAlso n3 = d(3)_
AndAlso n2 = d(2) AndAlso n1 = d(1) AndAlso n0 = d(0) Then
Print *gmp_str
End If
ElseIf Len(*gmp_str) < n Then
' all for next loops have a negative step value
' if len(str_x) becomes smaller then n it's time to try the next n value
' GoTo label1 ' old school BASIC
' prefered FreeBASIC style
Exit For, For, For, For, For, For, For, For, For
' leave n1, n2, n3, n4, n5, n6, n7, n8, n9 loop
' and continue's after next n9
End If
 
Next n1
Next n2
Next n3
Next n4
Next n5
Next n6
Next n7
Next n8
Next n9
' label1:
Next n
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
Left side: program output, right side: sorted on length, value
<pre style="height:35ex;overflow:scroll">9 0
8 1
7 2
6 3
5 4
4 5
3 6
2 7
1 8
0 9
407 153
371 370
370 371
153 407
9474 1634
8208 8208
1634 9474
93084 54748
92727 92727
54748 93084
548834 548834
9926315 1741725
9800817 4210818
4210818 9800817
1741725 9926315
88593477 24678050
24678051 24678051
24678050 88593477
912985153 146511208
534494836 472335975
472335975 534494836
146511208 912985153
4679307774 4679307774
94204591914 32164049650
82693916578 32164049651
49388550606 40028394225
44708635679 42678290603
42678290603 44708635679
40028394225 49388550606
32164049651 82693916578
32164049650 94204591914
28116440335967 28116440335967
4338281769391371 4338281769391370
4338281769391370 4338281769391371
35875699062250035 21897142587612075
35641594208964132 35641594208964132
21897142587612075 35875699062250035
4929273885928088826 1517841543307505039
4498128791164624869 3289582984443187032
3289582984443187032 4498128791164624869
1517841543307505039 4929273885928088826
63105425988599693916 63105425988599693916
449177399146038697307 128468643043731391252
128468643043731391252 449177399146038697307
35452590104031691935943 21887696841122916288858
28361281321319229463398 27879694893054074471405
27907865009977052567814 27907865009977052567814
27879694893054074471405 28361281321319229463398
21887696841122916288858 35452590104031691935943
239313664430041569350093 174088005938065293023722
188451485447897896036875 188451485447897896036875
174088005938065293023722 239313664430041569350093
4422095118095899619457938 1550475334214501539088894
3706907995955475988644381 1553242162893771850669378
3706907995955475988644380 3706907995955475988644380
1553242162893771850669378 3706907995955475988644381
1550475334214501539088894 4422095118095899619457938
177265453171792792366489765 121204998563613372405438066
174650464499531377631639254 121270696006801314328439376
128851796696487777842012787 128851796696487777842012787
121270696006801314328439376 174650464499531377631639254
121204998563613372405438066 177265453171792792366489765
23866716435523975980390369295 14607640612971980372614873089
19008174136254279995012734741 19008174136254279995012734740
19008174136254279995012734740 19008174136254279995012734741
14607640612971980372614873089 23866716435523975980390369295
2309092682616190307509695338915 1145037275765491025924292050346
1927890457142960697580636236639 1927890457142960697580636236639
1145037275765491025924292050346 2309092682616190307509695338915
17333509997782249308725103962772 17333509997782249308725103962772
186709961001538790100634132976991 186709961001538790100634132976990
186709961001538790100634132976990 186709961001538790100634132976991
1122763285329372541592822900204593 1122763285329372541592822900204593
12679937780272278566303885594196922 12639369517103790328947807201478392
12639369517103790328947807201478392 12679937780272278566303885594196922
1219167219625434121569735803609966019 1219167219625434121569735803609966019
12815792078366059955099770545296129367 12815792078366059955099770545296129367
115132219018763992565095597973971522401 115132219018763992565095597973971522400
115132219018763992565095597973971522400 115132219018763992565095597973971522401</pre>
 
==={{header|GW-BASIC}}===
{{trans|FreeBASIC}}
Maximum for N (double) is14 digits, there are no 15 digits numbers
<syntaxhighlight lang="qbasic">1 DEFINT A-W : DEFDBL X-Z : DIM D(9) : DIM X2(9) : KEY OFF : CLS
2 FOR A = 0 TO 9 : X2(A) = A : NEXT A
3 FOR N = 1 TO 7
4 FOR N9 = N TO 0 STEP -1
5 FOR N8 = N-N9 TO 0 STEP -1
6 FOR N7 = N-N9-N8 TO 0 STEP -1
7 FOR N6 = N-N9-N8-N7 TO 0 STEP -1
8 FOR N5 = N-N9-N8-N7-N6 TO 0 STEP -1
9 FOR N4 = N-N9-N8-N7-N6-N5 TO 0 STEP -1
10 FOR N3 = N-N9-N8-N7-N6-N5-N4 TO 0 STEP -1
11 FOR N2 = N-N9-N8-N7-N6-N5-N4-N3 TO 0 STEP -1
12 FOR N1 = N-N9-N8-N7-N6-N5-N4-N3-N2 TO 0 STEP -1
13 N0 = N-N9-N8-N7-N6-N5-N4-N3-N2-N1
14 X = N1 + N2*X2(2) + N3*X2(3) + N4*X2(4) + N5*X2(5) + N6*X2(6) + N7*X2(7) + N8*X2(8) + N9*X2(9)
15 S$ = MID$(STR$(X),2)
16 IF LEN(S$) < N THEN GOTO 25
17 IF LEN(S$) <> N THEN GOTO 24
18 FOR A = 0 TO 9 : D(A) = 0 : NEXT A
19 FOR A = 0 TO N-1
20 B = ASC(MID$(S$,A+1,1))-48
21 D(B) = D(B) + 1
22 NEXT A
23 IF N0 = D(0) AND N1 = D(1) AND N2 = D(2) AND N3 = D(3) AND N4 = D(4) AND N5 = D(5) AND N6 = D(6) AND N7 = D(7) AND N8 = D(8) AND N9 = D(9) THEN PRINT X,
24 NEXT N1 : NEXT N2 : NEXT N3 : NEXT N4 : NEXT N5 : NEXT N6 : NEXT N7 : NEXT N8 : NEXT N9
25 FOR A = 2 TO 9
26 X2(A) = X2(A) * A
27 NEXT A
28 NEXT N
29 PRINT
30 PRINT "done"
31 END</syntaxhighlight>
{{out}}
<pre> 9 8 7 6 5
4 3 2 1 0
407 371 370 153 9474
8208 1634 93084 92727 54748
548834 9926315 9800817 4210818 1741725</pre>
 
==={{header|VBA}}===
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function narcissistic(n As Long) As Boolean
Dim d As String: d = CStr(n)
Dim l As Integer: l = Len(d)
Dim sumn As Long: sumn = 0
For i = 1 To l
sumn = sumn + (Mid(d, i, 1) - "0") ^ l
Next i
narcissistic = sumn = n
End Function
 
Public Sub main()
Dim s(24) As String
Dim n As Long: n = 0
Dim found As Integer: found = 0
Do While found < 25
If narcissistic(n) Then
s(found) = CStr(n)
found = found + 1
End If
n = n + 1
Loop
Debug.Print Join(s, ", ")
End Sub</syntaxhighlight>{{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|VBScript}}===
<syntaxhighlight lang="vb">Function Narcissist(n)
i = 0
j = 0
Do Until j = n
sum = 0
For k = 1 To Len(i)
sum = sum + CInt(Mid(i,k,1)) ^ Len(i)
Next
If i = sum Then
Narcissist = Narcissist & i & ", "
j = j + 1
End If
i = i + 1
Loop
End Function
 
WScript.StdOut.Write Narcissist(25)</syntaxhighlight>
{{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|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)
<syntaxhighlight lang="zxbasic"> 1 DIM K(10): DIM M(10)
2 FOR Y=0 TO 9: LET M(Y+1)=Y: NEXT Y
3 FOR N=1 TO 7
4 FOR J=N TO 0 STEP -1
5 FOR I=N-J TO 0 STEP -1
6 FOR H=N-J-I TO 0 STEP -1
7 FOR G=N-J-I-H TO 0 STEP -1
8 FOR F=N-J-I-H-G TO 0 STEP -1
9 FOR E=N-J-I-H-G-F TO 0 STEP -1
10 FOR D=N-J-I-H-G-F-E TO 0 STEP -1
11 FOR C=N-J-I-H-G-F-E-D TO 0 STEP -1
12 FOR B=N-J-I-H-G-F-E-D-C TO 0 STEP -1
13 LET A=N-J-I-H-G-F-E-D-C-B
14 LET X=B+C*M(3)+D*M(4)+E*M(5)+F*M(6)+G*M(7)+H*M(8)+I*M(9)+J*M(10)
15 LET S$=STR$ (X)
16 IF LEN (S$)<N THEN GO TO 34
17 IF LEN (S$)<>N THEN GO TO 33
18 FOR Y=1 TO 10: LET K(Y)=0: NEXT Y
19 FOR Y=1 TO N
20 LET Z= CODE (S$(Y))-47
21 LET K(Z)=K(Z)+1
22 NEXT Y
23 IF A<>K(1) THEN GO TO 33
24 IF B<>K(2) THEN GO TO 33
25 IF C<>K(3) THEN GO TO 33
26 IF D<>K(4) THEN GO TO 33
27 IF E<>K(5) THEN GO TO 33
28 IF F<>K(6) THEN GO TO 33
29 IF G<>K(7) THEN GO TO 33
30 IF H<>K(8) THEN GO TO 33
31 IF I<>K(9) THEN GO TO 33
32 IF J=K(10) THEN PRINT X,
33 NEXT B: NEXT C: NEXT D: NEXT E: NEXT F: NEXT G: NEXT H: NEXT I: NEXT J
34 FOR Y=2 TO 9
35 LET M(Y+1)=M(Y+1)*Y
36 NEXT Y
37 NEXT N
38 PRINT
39 PRINT "DONE"</syntaxhighlight>
{{out}}
<pre>9 8
7 6
5 4
3 2
1 0
9 8
7 6
5 4
3 2
1 0
407 371
370 153
9474 8208
1634 93084
92727 54748
548834 9926315
9800817 4210818
1741725</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
 
let pow(x,y) = valof
$( let r = 1
for i = 1 to y do
r := r * x
resultis r
$)
 
let narcissist(n) = valof
$( let digits = vec 10
let number = n
let len = 0
let i = ? and powsum = 0
while n > 0 do
$( digits!len := n rem 10
n := n / 10
len := len + 1
$)
i := len
while i > 0 do
$( i := i - 1
powsum := powsum + pow(digits!i, len)
$)
resultis powsum = number
$)
 
let start() be
$( let n = 0
for i = 1 to 25
$( until narcissist(n) do n := n+1
writef("%I9*N", n)
n := n+1
$)
$)</syntaxhighlight>
{{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|Befunge}}==
This can take several minutes to complete in most interpreters, so it's probably best to use a compiler if you want to see the full sequence.
 
<langsyntaxhighlight lang="befunge">p55*\>:>:>:55+%\55+/00gvv_@
>1>+>^v\_^#!:<p01p00:+1<>\>
>#-_>\>20p110g>\20g*\v>1-v|
^!p00:-1g00+$_^#!:<-1<^\.:<</langsyntaxhighlight>
 
{{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|BQN}}==
 
<code>B10</code> is a BQNcrate idiom to get the digits of a number.
 
<syntaxhighlight lang="bqn">B10 ← 10{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)}
IsNarc ← {𝕩=+´⋆⟜≠B10 𝕩}
 
/IsNarc¨ ↕1e7</syntaxhighlight><syntaxhighlight lang="bqn">⟨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⟩</syntaxhighlight>
 
A much faster method is to generate a list of digit sums as addition tables (<code>+⌜</code>). A different list of digit sums is generated for each digit count, 0 to 7. To avoid leading 0s, 0 is removed from the first digit list with <code>(0=↕)↓¨</code>. Then all that needs to be done is to join the lists and return locations where the index (number) and value (digit power sum) are equal.
 
<syntaxhighlight lang="bqn">/ ↕∘≠⊸= ∾ (⥊0+⌜´(0=↕)↓¨(<↕10)⋆⊢)¨↕8</syntaxhighlight>
 
=={{header|C}}==
Line 649 ⟶ 1,359:
 
The following prints the first 25 numbers, though not in order...
<langsyntaxhighlight lang="c">#include <stdio.h>
#include <gmp.h>
 
Line 711 ⟶ 1,421:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 736 ⟶ 1,446:
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">
using System;
 
Line 776 ⟶ 1,486:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 786 ⟶ 1,496:
</pre>
===or===
<langsyntaxhighlight lang="csharp">
//Narcissistic numbers: Nigel Galloway: February 17th., 2015
using System;
Line 815 ⟶ 1,525:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 848 ⟶ 1,558:
{{libheader|System.Numerics}}
{{trans|FreeBASIC}} (FreeBASIC, GMP version)<br/>Why stop at 25? Even using '''ulong''' instead of '''int''' only gets one to the 44th item. The 89th (last) item has 39 digits, which '''BigInteger''' easily handles. Of course, the BigInteger implementation is slower than native data types. But one can compensate a bit by calculating in parallel. Not bad, it can get all 89 items in under 7 1/2 minutes on a core i7. The calculation to the 25th item takes a fraction of a second. The calculation for all items up to 25 digits long (67th item) takes about half a minute with sequential processing and less than a quarter of a minute using parallel processing. Note that parallel execution involves some overhead, and isn't a time improvement unless computing around 15 digits or more. This program can test all numbers up to 61 digits in under half an hour, of course the highest item found has only 39 digits.
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 965 ⟶ 1,675:
}
}
</syntaxhighlight>
</lang>
{{out}}(with command line parameter = "39")
<pre style="height:30ex;overflow:scroll">Calculations in parallel... 7 6 5 4 3 2 1 11 10 9 8 15 14 13 12 19 18 17 16 23 22 20 21 26 27 25 24 30 31 29 34 28 35 38 33 39 32 37 36
Line 1,130 ⟶ 1,840:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <vector>
Line 1,198 ⟶ 1,908:
return system( "pause" );
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,206 ⟶ 1,916:
=={{header|Clojure}}==
Find N first Narcissistic numbers.
<syntaxhighlight lang="clojure">
<lang Clojure>
(ns narcissistic.core
(:require [clojure.math.numeric-tower :as math]))
Line 1,220 ⟶ 1,930:
(defn firstNnarc [n] ;;list of the first "n" Narcissistic numbers.
(take n (filter narcissistic? (range))))
</syntaxhighlight>
</lang>
{{out}}
by Average-user
Line 1,230 ⟶ 1,940:
 
=={{header|COBOL}}==
<syntaxhighlight lang="cobol">
<lang COBOL>
PROGRAM-ID. NARCISSIST-NUMS.
DATA DIVISION.
Line 1,289 ⟶ 1,999:
END PROGRAM NARCISSIST-NUMS.
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,318 ⟶ 2,028:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(defun integer-to-list (n)
(map 'list #'digit-char-p (prin1-to-string n)))
Line 1,333 ⟶ 2,043:
counting (narcissisticp c) into narcissistic
do (if (narcissisticp c) (print c))))
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,365 ⟶ 2,075:
NIL
</pre>
 
=={{header|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub pow(n: uint32, p: uint8): (r: uint32) is
r := 1;
while p>0 loop
r := r * n;
p := p - 1;
end loop;
end sub;
 
sub narcissist(n: uint32): (r: uint8) is
var digits: uint8[10];
var len: uint8 := 0;
var number := n;
 
while n>0 loop
digits[len] := (n % 10) as uint8;
n := n / 10;
len := len + 1;
end loop;
 
var i := len;
var powsum: uint32 := 0;
while i>0 loop
i := i - 1;
powsum := powsum + pow(digits[i] as uint32, len);
end loop;
 
r := 0;
if powsum == number then
r := 1;
end if;
end sub;
 
var seen: uint8 := 0;
var n: uint32 := 0;
while seen < 25 loop
if narcissist(n) != 0 then
print_i32(n);
print_nl();
seen := seen + 1;
end if;
n := n + 1;
end loop;</syntaxhighlight>
{{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|D}}==
===Simple Version===
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.conv, std.range;
 
Line 1,375 ⟶ 2,157:
writefln("%(%(%d %)\n%)",
uint.max.iota.filter!isNarcissistic.take(25).chunks(5));
}</langsyntaxhighlight>
{{out}}
<pre>0 1 2 3 4
Line 1,385 ⟶ 2,167:
===Fast Version===
{{trans|Python}}
<langsyntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.array;
 
uint[] narcissists(in uint m) pure nothrow @safe {
Line 1,413 ⟶ 2,195:
void main() {
writefln("%(%(%d %)\n%)", 25.narcissists.chunks(5));
}</langsyntaxhighlight>
With LDC2 compiler prints the same output in less than 0.3 seconds.
 
===Faster Version===
{{trans|C}}
<langsyntaxhighlight lang="d">import std.stdio, std.bigint, std.conv;
 
struct Narcissistics(TNum, uint maxLen) {
Line 1,471 ⟶ 2,253:
foreach (immutable i; 1 .. maxLength + 1)
narc.show(i);
}</langsyntaxhighlight>
{{out}}
<pre>length 1: 9 8 7 6 5 4 3 2 1 0
Line 1,490 ⟶ 2,272:
length 16: 4338281769391371 4338281769391370</pre>
With LDC2 compiler and maxLength=16 the run-time is about 0.64 seconds.
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
 
function IntPower(N,P: integer): integer;
var I: integer;
begin
Result:=N;
for I:=1 to P-1 do Result:=Result * N;
end;
 
function IsNarcisNumber(N: integer): boolean;
{Test if this a narcisstic number}
{i.e. the sum of each digit raised to power length = N}
var S: string;
var I,Sum,B,P: integer;
begin
S:=IntToStr(N);
Sum:=0;
P:=Length(S);
for I:=1 to Length(S) do
begin
B:=byte(S[I])-$30;
Sum:=Sum+IntPower(B,P);
end;
Result:=Sum=N;
end;
 
 
procedure ShowNarcisNumber(Memo: TMemo);
{Show first 25 narcisstic number}
var I,Cnt: integer;
var S: string;
begin
Cnt:=0;
S:='';
for I:=0 to High(Integer) do
if IsNarcisNumber(I) then
begin
S:=S+Format('%10d',[I]);
Inc(Cnt);
if (Cnt mod 5)=0 then S:=S+#$0D#$0A;
if Cnt>=25 then break;
end;
Memo.Lines.Add(S);
end;
 
 
</syntaxhighlight>
{{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|Draco}}==
<syntaxhighlight lang="draco">proc nonrec pow(byte n, p) ulong:
ulong r;
r := 0L1;
while p > 0 do
r := r * make(n, ulong);
p := p - 1
od;
r
corp
 
proc nonrec narcissist(ulong n) bool:
[10]byte digits;
byte len, i;
ulong number, powsum;
number := n;
len := 0;
while n>0 do
digits[len] := n % 10;
n := n / 10;
len := len+1
od;
i := len;
powsum := 0;
while i>0 do
i := i-1;
powsum := powsum + pow(digits[i], len)
od;
powsum = number
corp
 
proc nonrec main() void:
byte i;
ulong n;
n := 0L0;
for i from 1 upto 25 do
while not narcissist(n) do n := n+1 od;
writeln(n);
n := n+1
od
corp</syntaxhighlight>
{{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|EasyLang}}==
<syntaxhighlight lang="easylang">
while cnt < 25
s$ = n
ln = len s$
s = 0
for i to ln
s += pow number substr s$ i 1 ln
.
if s = n
print s
cnt += 1
.
n += 1
.
</syntaxhighlight>
 
=={{header|Elixir}}==
{{trans|D}}
<langsyntaxhighlight lang="elixir">defmodule RC do
def narcissistic(m) do
Enum.reduce(1..10, [0], fn digits,acc ->
Line 1,524 ⟶ 2,452:
catch
x -> IO.inspect x
end</langsyntaxhighlight>
 
{{out}}
Line 1,530 ⟶ 2,458:
[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|EMal}}==
<syntaxhighlight lang="emal">
fun narcissistic = void by int count
for int i, n, sum = 0; i < count; ++n, sum = 0
text nText = text!n
for each text c in nText
sum += (int!c) ** nText.length
end
if sum == n
if (i % 5 == 0) do writeLine() end
write((text!n).padStart(8, " "))
++i
end
end
writeLine()
end
narcissistic(25)
</syntaxhighlight>
{{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|ERRE}}==
<langsyntaxhighlight ERRElang="erre">PROGRAM NARCISISTIC
 
!$DOUBLE
Line 1,554 ⟶ 2,509:
N=N+1
END LOOP
END PROGRAM</langsyntaxhighlight>
Output
<pre>
Line 1,562 ⟶ 2,517:
 
=={{header|F Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
//Naïve solution of Narcissitic number: Nigel Galloway - Febryary 18th., 2015
open System
Line 1,570 ⟶ 2,525:
let d = _Digits (n, [])
d |> List.fold (fun a l -> a + int ((float l) ** (float (List.length d)))) 0 = n) |> Seq.take(25) |> Seq.iter (printfn "%A")
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,601 ⟶ 2,556:
 
=={{header|Factor}}==
<syntaxhighlight lang="text">USING: io kernel lists lists.lazy math math.functions
math.text.utils prettyprint sequences ;
IN: rosetta-code.narcissistic-decimal-number
Line 1,615 ⟶ 2,570:
: main ( -- ) first25 [ pprint bl ] each ;
 
MAIN: main</langsyntaxhighlight>
{{out}}
<pre>
Line 1,623 ⟶ 2,578:
=={{header|Forth}}==
{{works with|GNU Forth|0.7.0}}
<langsyntaxhighlight lang="forth">
: dig.num \ returns input number and the number of its digits ( n -- n n1 )
dup
Line 1,703 ⟶ 2,658:
25 narc.num
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,733 ⟶ 2,688:
ok
</pre>
 
=={{header|FreeBASIC}}==
===Simple Version===
<lang FreeBASIC>' normal version: 14-03-2017
' compile with: fbc -s console
' can go up to 18 digits (ulongint is 64bit), above 18 overflow will occur
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9, a, b
Dim As Integer d()
Dim As ULongInt d2pow(0 To 9) = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
Dim As ULongInt x
Dim As String str_x
For n = 1 To 7
For n9 = n To 0 Step -1
For n8 = n-n9 To 0 Step -1
For n7 = n-n9-n8 To 0 Step -1
For n6 = n-n9-n8-n7 To 0 Step -1
For n5 = n-n9-n8-n7-n6 To 0 Step -1
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
x = n1 + n2*d2pow(2) + n3*d2pow(3) + n4*d2pow(4) + n5*d2pow(5)_
+ n6*d2pow(6) + n7*d2pow(7) + n8*d2pow(8) + n9*d2pow(9)
str_x = Str(x)
If Len(str_x) = n Then
ReDim d(10)
For a = 0 To n-1
d(Str_x[a]- Asc("0")) += 1
Next a
If n0 = d(0) AndAlso n1 = d(1) AndAlso n2 = d(2) AndAlso n3 = d(3)_
AndAlso n4 = d(4) AndAlso n5 = d(5) AndAlso n6 = d(6)_
AndAlso n7 = d(7) AndAlso n8 = d(8) AndAlso n9 = d(9) Then
Print x
End If
End If
Next n1
Next n2
Next n3
Next n4
Next n5
Next n6
Next n7
Next n8
Next n9
For a As Integer = 2 To 9
d2pow(a) = d2pow(a) * a
Next a
Next n
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
<pre>9
8
7
6
5
4
3
2
1
0
407
371
370
153
9474
8208
1634
93084
92727
54748
548834
9926315
9800817
4210818
1741725</pre>
 
===GMP Version===
<pre>It takes about 35 min. to find all 88 numbers (39 digits).
To go all the way it takes about 2 hours.</pre>
<lang FreeBASIC>' gmp version: 17-06-2015
' uses gmp
' compile with: fbc -s console
 
#Include Once "gmp.bi"
' change the number after max for the maximum n-digits you want (2 to 61)
#Define max 61
 
Dim As Integer n, n0, n1, n2, n3, n4, n5, n6, n7, n8, n9
Dim As Integer i, j
Dim As UInteger d()
Dim As ZString Ptr gmp_str
gmp_str = Allocate(100)
 
' create gmp integer array,
Dim d2pow(9, max) As Mpz_ptr
' initialize array and set start value,
For i = 0 To 9
For j = 0 To max
d2pow(i, j) = Allocate(Len(__mpz_struct)) : Mpz_init(d2pow(i, j))
Next j
Next i
 
' gmp integers for to hold intermediate result
Dim As Mpz_ptr x1 = Allocate(Len(__mpz_struct)) : Mpz_init(x1)
Dim As Mpz_ptr x2 = Allocate(Len(__mpz_struct)) : Mpz_init(x2)
Dim As Mpz_ptr x3 = Allocate(Len(__mpz_struct)) : Mpz_init(x3)
Dim As Mpz_ptr x4 = Allocate(Len(__mpz_struct)) : Mpz_init(x4)
Dim As Mpz_ptr x5 = Allocate(Len(__mpz_struct)) : Mpz_init(x5)
Dim As Mpz_ptr x6 = Allocate(Len(__mpz_struct)) : Mpz_init(x6)
Dim As Mpz_ptr x7 = Allocate(Len(__mpz_struct)) : Mpz_init(x7)
Dim As Mpz_ptr x8 = Allocate(Len(__mpz_struct)) : Mpz_init(x8)
 
For n = 1 To max
 
For i = 1 To 9
'Mpz_set_ui(d2pow(i,0), 0)
Mpz_ui_pow_ui(d2pow(i,1), i, n)
For j = 2 To n
Mpz_mul_ui(d2pow(i, j), d2pow(i, 1), j)
Next j
Next i
 
For n9 = n To 0 Step -1
For n8 = n-n9 To 0 Step -1
Mpz_add(x8, d2pow(9, n9), d2pow(8, n8))
For n7 = n-n9-n8 To 0 Step -1
Mpz_add(x7, x8, d2pow(7, n7))
For n6 = n-n9-n8-n7 To 0 Step -1
Mpz_add(x6, x7, d2pow(6, n6))
For n5 = n-n9-n8-n7-n6 To 0 Step -1
Mpz_add(x5, x6, d2pow(5, n5))
For n4 = n-n9-n8-n7-n6-n5 To 0 Step -1
Mpz_add(x4, x5, d2pow(4, n4))
For n3 = n-n9-n8-n7-n6-n5-n4 To 0 Step -1
Mpz_add(x3, x4, d2pow(3, n3))
For n2 = n-n9-n8-n7-n6-n5-n4-n3 To 0 Step -1
Mpz_add(x2, x3, d2pow(2, n2))
For n1 = n-n9-n8-n7-n6-n5-n4-n3-n2 To 0 Step -1
Mpz_add_ui(x1, x2, n1)
n0 = n-n9-n8-n7-n6-n5-n4-n3-n2-n1
 
Mpz_get_str(gmp_str, 10, x1)
 
If Len(*gmp_str) = n Then
ReDim d(10)
 
For i = 0 To n-1
d(gmp_str[i] - Asc("0")) += 1
Next i
 
If n9 = d(9) AndAlso n8 = d(8) AndAlso n7 = d(7) AndAlso n6 = d(6)_
AndAlso n5 = d(5) AndAlso n4 = d(4) AndAlso n3 = d(3)_
AndAlso n2 = d(2) AndAlso n1 = d(1) AndAlso n0 = d(0) Then
Print *gmp_str
End If
ElseIf Len(*gmp_str) < n Then
' all for next loops have a negative step value
' if len(str_x) becomes smaller then n it's time to try the next n value
' GoTo label1 ' old school BASIC
' prefered FreeBASIC style
Exit For, For, For, For, For, For, For, For, For
' leave n1, n2, n3, n4, n5, n6, n7, n8, n9 loop
' and continue's after next n9
End If
 
Next n1
Next n2
Next n3
Next n4
Next n5
Next n6
Next n7
Next n8
Next n9
' label1:
Next n
 
' empty keyboard buffer
While InKey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</lang>
{{out}}
Left side: program output, right side: sorted on length, value
<pre style="height:35ex;overflow:scroll">9 0
8 1
7 2
6 3
5 4
4 5
3 6
2 7
1 8
0 9
407 153
371 370
370 371
153 407
9474 1634
8208 8208
1634 9474
93084 54748
92727 92727
54748 93084
548834 548834
9926315 1741725
9800817 4210818
4210818 9800817
1741725 9926315
88593477 24678050
24678051 24678051
24678050 88593477
912985153 146511208
534494836 472335975
472335975 534494836
146511208 912985153
4679307774 4679307774
94204591914 32164049650
82693916578 32164049651
49388550606 40028394225
44708635679 42678290603
42678290603 44708635679
40028394225 49388550606
32164049651 82693916578
32164049650 94204591914
28116440335967 28116440335967
4338281769391371 4338281769391370
4338281769391370 4338281769391371
35875699062250035 21897142587612075
35641594208964132 35641594208964132
21897142587612075 35875699062250035
4929273885928088826 1517841543307505039
4498128791164624869 3289582984443187032
3289582984443187032 4498128791164624869
1517841543307505039 4929273885928088826
63105425988599693916 63105425988599693916
449177399146038697307 128468643043731391252
128468643043731391252 449177399146038697307
35452590104031691935943 21887696841122916288858
28361281321319229463398 27879694893054074471405
27907865009977052567814 27907865009977052567814
27879694893054074471405 28361281321319229463398
21887696841122916288858 35452590104031691935943
239313664430041569350093 174088005938065293023722
188451485447897896036875 188451485447897896036875
174088005938065293023722 239313664430041569350093
4422095118095899619457938 1550475334214501539088894
3706907995955475988644381 1553242162893771850669378
3706907995955475988644380 3706907995955475988644380
1553242162893771850669378 3706907995955475988644381
1550475334214501539088894 4422095118095899619457938
177265453171792792366489765 121204998563613372405438066
174650464499531377631639254 121270696006801314328439376
128851796696487777842012787 128851796696487777842012787
121270696006801314328439376 174650464499531377631639254
121204998563613372405438066 177265453171792792366489765
23866716435523975980390369295 14607640612971980372614873089
19008174136254279995012734741 19008174136254279995012734740
19008174136254279995012734740 19008174136254279995012734741
14607640612971980372614873089 23866716435523975980390369295
2309092682616190307509695338915 1145037275765491025924292050346
1927890457142960697580636236639 1927890457142960697580636236639
1145037275765491025924292050346 2309092682616190307509695338915
17333509997782249308725103962772 17333509997782249308725103962772
186709961001538790100634132976991 186709961001538790100634132976990
186709961001538790100634132976990 186709961001538790100634132976991
1122763285329372541592822900204593 1122763285329372541592822900204593
12679937780272278566303885594196922 12639369517103790328947807201478392
12639369517103790328947807201478392 12679937780272278566303885594196922
1219167219625434121569735803609966019 1219167219625434121569735803609966019
12815792078366059955099770545296129367 12815792078366059955099770545296129367
115132219018763992565095597973971522401 115132219018763992565095597973971522400
115132219018763992565095597973971522400 115132219018763992565095597973971522401</pre>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">def narcissistic( start ) =
power = 1
powers = array( 0..9 )
Line 2,042 ⟶ 2,709:
narc( start )
 
println( narcissistic(0).take(25) )</langsyntaxhighlight>
 
{{out}}
Line 2,049 ⟶ 2,716:
[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|Fōrmulæ}}==
 
{{FormulaeEntry|page=https://formulae.org/?script=examples/Narcissistic_decimal_number}}
 
'''Solution'''
 
The following functions retrieves whether a given number in a given base is narcissistic or not:
 
[[File:Fōrmulæ - Narcissistic number 01.png]]
 
'''Test case'''
 
[[File:Fōrmulæ - Narcissistic number 02.png]]
 
[[File:Fōrmulæ - Narcissistic number 03.png]]
 
'''Generating the first 25 narcissistic decimal numbers'''
 
[[File:Fōrmulæ - Narcissistic number 04.png]]
 
[[File:Fōrmulæ - Narcissistic number 05.png]]
 
[[File:Fōrmulæ - Narcissistic number 06.png]]
 
=={{header|Go}}==
Nothing fancy as it runs in a fraction of a second as-is.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 2,080 ⟶ 2,771:
func main() {
fmt.Println(narc(25))
}</langsyntaxhighlight>
{{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|GW-BASIC}}==
{{trans|FreeBASIC}}
Maximum for N (double) is14 digits, there are no 15 digits numbers
<lang qbasic>1 DEFINT A-W : DEFDBL X-Z : DIM D(9) : DIM X2(9) : KEY OFF : CLS
2 FOR A = 0 TO 9 : X2(A) = A : NEXT A
3 FOR N = 1 TO 7
4 FOR N9 = N TO 0 STEP -1
5 FOR N8 = N-N9 TO 0 STEP -1
6 FOR N7 = N-N9-N8 TO 0 STEP -1
7 FOR N6 = N-N9-N8-N7 TO 0 STEP -1
8 FOR N5 = N-N9-N8-N7-N6 TO 0 STEP -1
9 FOR N4 = N-N9-N8-N7-N6-N5 TO 0 STEP -1
10 FOR N3 = N-N9-N8-N7-N6-N5-N4 TO 0 STEP -1
11 FOR N2 = N-N9-N8-N7-N6-N5-N4-N3 TO 0 STEP -1
12 FOR N1 = N-N9-N8-N7-N6-N5-N4-N3-N2 TO 0 STEP -1
13 N0 = N-N9-N8-N7-N6-N5-N4-N3-N2-N1
14 X = N1 + N2*X2(2) + N3*X2(3) + N4*X2(4) + N5*X2(5) + N6*X2(6) + N7*X2(7) + N8*X2(8) + N9*X2(9)
15 S$ = MID$(STR$(X),2)
16 IF LEN(S$) < N THEN GOTO 25
17 IF LEN(S$) <> N THEN GOTO 24
18 FOR A = 0 TO 9 : D(A) = 0 : NEXT A
19 FOR A = 0 TO N-1
20 B = ASC(MID$(S$,A+1,1))-48
21 D(B) = D(B) + 1
22 NEXT A
23 IF N0 = D(0) AND N1 = D(1) AND N2 = D(2) AND N3 = D(3) AND N4 = D(4) AND N5 = D(5) AND N6 = D(6) AND N7 = D(7) AND N8 = D(8) AND N9 = D(9) THEN PRINT X,
24 NEXT N1 : NEXT N2 : NEXT N3 : NEXT N4 : NEXT N5 : NEXT N6 : NEXT N7 : NEXT N8 : NEXT N9
25 FOR A = 2 TO 9
26 X2(A) = X2(A) * A
27 NEXT A
28 NEXT N
29 PRINT
30 PRINT "done"
31 END</lang>
{{out}}
<pre> 9 8 7 6 5
4 3 2 1 0
407 371 370 153 9474
8208 1634 93084 92727 54748
548834 9926315 9800817 4210818 1741725</pre>
 
=={{header|Haskell}}==
===Exhaustive search (integer series)===
<langsyntaxhighlight Haskelllang="haskell">import Data.Char (digitToInt)
 
isNarcissistic :: Int -> Bool
Line 2,138 ⟶ 2,788:
 
main :: IO ()
main = mapM_ print $ take 25 (filter isNarcissistic [0 ..])</langsyntaxhighlight>
 
===Reduced search (unordered digit combinations)===
Line 2,145 ⟶ 2,795:
In this way we can find the 25th narcissistic number after '''length $ concatMap digitPowerSums [1 .. 7] == 19447''' tests – an improvement on the exhaustive trawl through '''9926315''' integers.
 
<langsyntaxhighlight lang="haskell">import Data.Bifunctor (second)
 
narcissiOfLength :: Int -> [Int]
Line 2,191 ⟶ 2,841:
w = maximum (length . xShow <$> xs)
in unlines $
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</langsyntaxhighlight>
{{Out}}
<pre>Narcissistic decimal numbers of length 1-7:
Line 2,206 ⟶ 2,856:
 
The following is a quick, dirty, and slow solution that works in both languages:
<langsyntaxhighlight lang="unicon">procedure main(A)
limit := integer(A[1]) | 25
every write(isNarcissitic(seq(0))\limit)
Line 2,216 ⟶ 2,866:
every (sum := 0) +:= (!sn)^m
return sum = n
end</langsyntaxhighlight>
 
Sample run:
Line 2,250 ⟶ 2,900:
 
=={{header|J}}==
<langsyntaxhighlight lang="j">getDigits=: "."0@": NB. get digits from number
isNarc=: (= +/@(] ^ #)@getDigits)"0 NB. test numbers for Narcissism</langsyntaxhighlight>
'''Example Usage'''
<langsyntaxhighlight 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</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">public class Narc{
public static boolean isNarc(long x){
if(x < 0) return false;
Line 2,280 ⟶ 2,930:
}
}
}</langsyntaxhighlight>
{{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>
Line 2,286 ⟶ 2,936:
{{works with|Java|1.8}}
The statics and the System.exit(0) stem from having first developed a version that is not limited by the amount of narcisstic numbers that are to be calculated. I then read that this is a criterion and thus the implementation is an afterthought and looks awkwardish... but still... works!
<langsyntaxhighlight lang="java5">
import java.util.stream.IntStream;
public class NarcissisticNumbers {
Line 2,312 ⟶ 2,962:
});
}
}</langsyntaxhighlight>
{{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>
Line 2,319 ⟶ 2,969:
===ES5===
{{trans|Java}}
<langsyntaxhighlight lang="javascript">function isNarc(x) {
var str = x.toString(),
i,
Line 2,342 ⟶ 2,992:
}
return n.join(' ');
}</langsyntaxhighlight>
{{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>
Line 2,348 ⟶ 2,998:
===ES6===
====Exhaustive search (integer series)====
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
Line 2,387 ⟶ 3,037:
)
.narc
})();</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight JavaScriptlang="javascript">[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]</langsyntaxhighlight>
 
 
Line 2,399 ⟶ 3,049:
 
(Generating the unordered digit combinations directly as power sums allows faster testing later, and needs less space)
<langsyntaxhighlight JavaScriptlang="javascript">(() => {
'use strict';
 
Line 2,542 ⟶ 3,192:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Narcissistic decimal numbers of lengths [1..7]:
Line 2,559 ⟶ 3,209:
 
A function for checking whether a given non-negative integer is narcissistic could be implemented in jq as follows:
<langsyntaxhighlight lang="jq">def is_narcissistic:
def digits: tostring | explode[] | [.] | implode | tonumber;
def pow(n): . as $x | reduce range(0;n) as $i (1; . * $x);
Line 2,565 ⟶ 3,215:
(tostring | length) as $len
| . == reduce digits as $d (0; . + ($d | pow($len)) )
end;</langsyntaxhighlight>
 
In the following, this definition is modified to avoid recomputing (d ^ i). This is accomplished introducing the array [i, [0^i, 1^i, ..., 9^i]].
To update this array for increasing values of i, the function powers(j) is defined as follows:
<langsyntaxhighlight lang="jq"># Input: [i, [0^i, 1^i, 2^i, ..., 9^i]]
# Output: [j, [0^j, 1^j, 2^j, ..., 9^j]]
# provided j is i or (i+1)
Line 2,576 ⟶ 3,226:
else .[0] += 1
| reduce range(0;10) as $k (.; .[1][$k] *= $k)
end;</langsyntaxhighlight>
 
The function is_narcisstic can now be modified to use powers(j) as follows:
<langsyntaxhighlight lang="jq"># Input: [n, [i, [0^i, 1^i, 2^i,...]]] where i is the number of digits in n.
def is_narcissistic:
def digits: tostring | explode[] | [.] | implode | tonumber;
Line 2,586 ⟶ 3,236:
| if . < 0 then false
else . == reduce digits as $d (0; . + $powers[$d] )
end;</langsyntaxhighlight>
'''The task'''
<langsyntaxhighlight lang="jq"># If your jq has "while", then feel free to omit the following definition:
def while(cond; update):
def _while: if cond then ., (update | _while) else empty end;
Line 2,610 ⟶ 3,260:
| "\(.[2]): \(.[0])" ;
 
narcissistic(25)</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">jq -r -n -f Narcissitic_decimal_number.jq
1: 0
2: 1
Line 2,637 ⟶ 3,287:
23: 4210818
24: 9800817
25: 9926315</langsyntaxhighlight>
 
=={{header|Julia}}==
This easy to implement brute force technique is plenty fast enough to find the first few Narcissistic decimal numbers.
<langsyntaxhighlight Julialang="julia">using Printf # for Julia version 1.0+
 
function isnarcissist(n, b=10)
Line 2,664 ⟶ 3,314:
findnarcissist()
@time findnarcissist(true)
</langsyntaxhighlight>{{out}}
<pre>
Finding the first 25 Narcissistic numbers:
Line 2,696 ⟶ 3,346:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.0
 
fun isNarcissistic(n: Int): Boolean {
Line 2,724 ⟶ 3,374:
}
while (count < 25)
}</langsyntaxhighlight>
 
{{out}}
Line 2,731 ⟶ 3,381:
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|Ksh}}==
<syntaxhighlight lang="ksh">
#!/bin/ksh
 
# Narcissistic decimal number
 
# # Variables:
#
 
# # Functions:
#
 
# # Function _isnarcissist(n) - return 1 if n is a narcissistic decimal number
#
function _isnarcissist {
typeset _n ; integer _n=$1
 
(( ${_n} == $(_sumpowdigits ${_n}) )) && return 1
return 0
}
 
# # Function _sumpowdigits(n) - return sum of the digits raised to #digit power
#
function _sumpowdigits {
typeset _n ; integer _n=$1
typeset _i ; typeset -si _i
typeset _sum ; integer _sum=0
 
for ((_i=0; _i<${#_n}; _i++)); do
(( _sum+=(${_n:_i:1}**${#_n}) ))
done
echo ${_sum}
}
 
######
# main #
######
 
integer i cnt=0
for ((i=0; cnt<25; i++)); do
_isnarcissist ${i} ; (( $? )) && printf "%3d. %d\n" $(( ++cnt )) ${i}
done
</syntaxhighlight>
{{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|Lua}}==
This is a simple/naive/slow method but it still spits out the requisite 25 in less than a minute using LuaJIT on a 2.5 GHz machine.
<langsyntaxhighlight Lualang="lua">function isNarc (n)
local m, sum, digit = string.len(n), 0
for pos = 1, m do
Line 2,750 ⟶ 3,470:
end
n = n + 1
until count == 25</langsyntaxhighlight>
{{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|MAD}}==
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
DIMENSION DIGIT(15)
 
INTERNAL FUNCTION(A,B)
ENTRY TO POWER.
R=1
BB=B
STEP WHENEVER BB.E.0, FUNCTION RETURN R
R=R*A
BB=BB-1
TRANSFER TO STEP
END OF FUNCTION
 
INTERNAL FUNCTION(NUM)
ENTRY TO NARCIS.
N=NUM
L=0
GETDGT WHENEVER N.G.0
NN=N/10
DIGIT(L)=N-NN*10
N=NN
L=L+1
TRANSFER TO GETDGT
END OF CONDITIONAL
I=L
SUM=0
POWSUM WHENEVER I.G.0
I=I-1
D=DIGIT(I)
SUM=SUM+POWER.(D,L)
TRANSFER TO POWSUM
END OF CONDITIONAL
FUNCTION RETURN SUM.E.NUM
END OF FUNCTION
 
CAND=0
THROUGH SEARCH, FOR SEEN=0,1,SEEN.GE.25
NEXT THROUGH NEXT, FOR CAND=CAND,1,NARCIS.(CAND)
PRINT FORMAT FMT,CAND
SEARCH CAND=CAND+1
 
VECTOR VALUES FMT=$I10*$
END OF PROGRAM</syntaxhighlight>
{{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|Maple}}==
<syntaxhighlight lang="maple">
<lang Maple>
 
Narc:=proc(i)
Line 2,781 ⟶ 3,572:
end do:
NDN;
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">narc[1] = 0;
narc[n_] := narc[n] = NestWhile[# + 1 &, narc[n - 1] + 1, Plus @@ (IntegerDigits[#]^IntegerLength[#]) != # &];
narc[n_] :=
narc /@ Range[25]</syntaxhighlight>
narc[n] =
NestWhile[# + 1 &, narc[n - 1] + 1,
Plus @@ (IntegerDigits[#]^IntegerLength[#]) != # &];
narc /@ Range[25]</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|MATLAB}}==
<langsyntaxhighlight MATLABlang="matlab">function testNarcissism
x = 0;
c = 0;
Line 2,810 ⟶ 3,598:
dig = sprintf('%d', n) - '0';
tf = n == sum(dig.^length(dig));
end</langsyntaxhighlight>
{{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|Nanoquery}}==
<langsyntaxhighlight Nanoquerylang="nanoquery">def is_narcissist(num)
digits = {}
for digit in str(num)
Line 2,847 ⟶ 3,635:
print num + " "
end
println</langsyntaxhighlight>
{{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>
Line 2,853 ⟶ 3,641:
=={{header|Nim}}==
A simple solution which runs in about one second.
<langsyntaxhighlight Nimlang="nim">import sequtils, strutils
 
func digits(n: Natural): seq[int] =
Line 2,879 ⟶ 3,667:
m *= 10
 
echo findNarcissistic(25).join(" ")</langsyntaxhighlight>
 
{{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|OCaml}}==
;Exhaustive search (integer series)
<syntaxhighlight lang="ocaml">let narcissistic =
let rec next n l p () =
let rec digit_pow_sum a n =
if n < 10 then a + p.(n) else digit_pow_sum (a + p.(n mod 10)) (n / 10)
in
if n = l then next n (l * 10) (Array.mapi ( * ) p) ()
else if n = digit_pow_sum 0 n then Seq.Cons (n, next (succ n) l p)
else next (succ n) l p ()
in
next 0 10 (Array.init 10 Fun.id)
 
let () =
narcissistic |> Seq.take 25 |> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
{{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|Oforth}}==
 
<langsyntaxhighlight Oforthlang="oforth">: isNarcissistic(n)
| i m |
n 0 while( n ) [ n 10 /mod ->n swap 1 + ] ->m
Line 2,895 ⟶ 3,701:
ListBuffer new dup ->l
0 while(l size n <>) [ dup isNarcissistic ifTrue: [ dup l add ] 1 + ] drop ;
</syntaxhighlight>
</lang>
 
{{out}}
Line 2,906 ⟶ 3,712:
=={{header|PARI/GP}}==
Naive code, could be improved by splitting the digits in half and meeting in the middle.
<langsyntaxhighlight lang="parigp">isNarcissistic(n)=my(v=digits(n)); sum(i=1, #v, v[i]^#v)==n
v=List();for(n=1,1e9,if(isNarcissistic(n),listput(v,n);if(#v>24, return(Vec(v)))))</langsyntaxhighlight>
{{out}}
<pre>%1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315, 24678050]</pre>
 
=={{header|Pascal}}==
==={{works withheader|Free Pascal}}===
A recursive version starting at the highest digit and recurses to digit 0. Bad runtime. One more digit-> 10x runtime
runtime ~ 10^(count of Digits).
<langsyntaxhighlight lang="pascal">
program NdN;
//Narcissistic decimal number
Line 2,996 ⟶ 3,802:
NextPowDig;
end;
end.</langsyntaxhighlight>
;output:
<pre>
Line 3,011 ⟶ 3,817:
 
real 0m1.000s</pre>
====alternative====
recursive solution.Just counting the different combination of digits<BR>
See [[Combinations_with_repetitions]]<BR>
<syntaxhighlight lang="pascal">program PowerOwnDigits;
{$IFDEF FPC}
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$COPERATORS ON}
{$ELSE}{$APPTYPE CONSOLE}{$ENDIF}
uses
SysUtils;
 
const
MAXBASE = 10;
MaxDgtVal = MAXBASE - 1;
MaxDgtCount = 19;
type
tDgtCnt = 0..MaxDgtCount;
tValues = 0..MaxDgtVal;
tUsedDigits = array[0..23] of Int8;
tpUsedDigits = ^tUsedDigits;
tPower = array[tValues] of Uint64;
var
PowerDgt: array[tDgtCnt] of tPower;
Min10Pot : array[tDgtCnt] of Uint64;
gblUD : tUsedDigits;
CombIdx: array of Int8;
Numbers : array of Uint64;
rec_cnt : NativeInt;
 
procedure OutUD(const UD:tUsedDigits);
var
i : integer;
begin
For i in tValues do
write(UD[i]:3);
writeln;
For i := 0 to MaxDgtCount do
write(CombIdx[i]:3);
writeln;
end;
 
function InitCombIdx(ElemCount: Byte): pbyte;
begin
setlength(CombIdx, ElemCount + 1);
Fillchar(CombIdx[0], sizeOf(CombIdx[0]) * (ElemCount + 1), #0);
Result := @CombIdx[0];
Fillchar(gblUD[0], sizeOf(gblUD[0]) * (ElemCount + 1), #0);
gblUD[0]:= 1;
end;
 
function Init(ElemCount:byte):pByte;
var
pP1,Pp2 : pUint64;
i, j: Int32;
begin
Min10Pot[0]:= 0;
Min10Pot[1]:= 1;
for i := 2 to High(tDgtCnt) do
Min10Pot[i]:=Min10Pot[i-1]*MAXBASE;
 
pP1 := @PowerDgt[low(tDgtCnt)];
for i in tValues do
pP1[i] := 1;
pP1[0] := 0;
for j := low(tDgtCnt) + 1 to High(tDgtCnt) do
Begin
pP2 := @PowerDgt[j];
for i in tValues do
pP2[i] := pP1[i]*i;
pP1 := pP2;
end;
result := InitCombIdx(ElemCount);
gblUD[0]:= 1;
end;
 
function GetPowerSum(minpot:nativeInt;digits:pbyte;var UD :tUsedDigits):NativeInt;
var
pPower : pUint64;
res,r : Uint64;
dgt :Int32;
begin
r := Min10Pot[minpot];
dgt := minpot;
res := 0;
pPower := @PowerDgt[minpot,0];
repeat
dgt -=1;
res += pPower[digits[dgt]];
until dgt=0;
//check if res within bounds of digitCnt
result := 0;
if (res<r) or (res>r*MAXBASE) then EXIT;
 
//convert res into digits
repeat
r := res DIV MAXBASE;
result+=1;
UD[res-r*MAXBASE]-= 1;
res := r;
until r = 0;
end;
 
procedure calcNum(minPot:Int32;digits:pbyte);
var
UD :tUsedDigits;
res: Uint64;
i: nativeInt;
begin
UD := gblUD;
If GetPowerSum(minpot,digits,UD) <>0 then
Begin
//don't check 0
i := 1;
repeat
If UD[i] <> 0 then
Break;
i +=1;
until i > MaxDgtVal;
 
if i > MaxDgtVal then
begin
res := 0;
for i := minpot-1 downto 0 do
res += PowerDgt[minpot,digits[i]];
setlength(Numbers, Length(Numbers) + 1);
Numbers[high(Numbers)] := res;
end;
end;
end;
 
function NextCombWithRep(pComb: pByte;pUD :tpUsedDigits;MaxVal, ElemCount: UInt32): boolean;
var
i,dgt: NativeInt;
begin
i := -1;
repeat
i += 1;
dgt := pComb[i];
if dgt < MaxVal then
break;
dec(pUD^[dgt]);
until i >= ElemCount;
Result := i >= ElemCount;
 
if i = 0 then
begin
dec(pUD^[dgt]);
dgt +=1;
pComb[i] := dgt;
inc(pUD^[dgt]);
end
else
begin
//decrements digit 0 too.This is false, but not checked.
dec(pUD^[dgt]);
dgt +=1;
pUD^[dgt]:=i+1;
repeat
pComb[i] := dgt;
i -= 1;
until i < 0;
end;
end;
 
var
digits : pByte;
T0 : Int64;
tmp: Uint64;
i, j : Int32;
 
begin
digits := Init(MaxDgtCount);
T0 := GetTickCount64;
rec_cnt := 0;
// i > 0
For i := 2 to MaxDgtCount do
Begin
digits := InitCombIdx(MaxDgtCount);
repeat
calcnum(i,digits);
inc(rec_cnt);
until NextCombWithRep(digits,@gblUD,MaxDgtVal,i);
writeln(i:3,' digits with ',Length(Numbers):3,' solutions in ',GetTickCount64-T0:5,' ms');
end;
T0 := GetTickCount64-T0;
writeln(rec_cnt,' recursions');
 
//sort
for i := 0 to High(Numbers) - 1 do
for j := i + 1 to High(Numbers) do
if Numbers[j] < Numbers[i] then
begin
tmp := Numbers[i];
Numbers[i] := Numbers[j];
Numbers[j] := tmp;
end;
 
setlength(Numbers, j + 1);
for i := 0 to High(Numbers) do
writeln(i+1:3,Numbers[i]:20);
setlength(Numbers, 0);
setlength(CombIdx,0);
{$IFDEF WINDOWS}
readln;
{$ENDIF}
end.
</syntaxhighlight>
{{out|@TIO.RUN}}
<pre style="height:180px">
2 digits with 0 solutions in 0 ms
3 digits with 4 solutions in 0 ms
4 digits with 7 solutions in 0 ms
5 digits with 10 solutions in 0 ms
6 digits with 11 solutions in 0 ms
7 digits with 15 solutions in 0 ms
8 digits with 18 solutions in 1 ms
9 digits with 22 solutions in 3 ms
10 digits with 23 solutions in 6 ms
11 digits with 31 solutions in 13 ms
12 digits with 31 solutions in 25 ms
13 digits with 31 solutions in 46 ms
14 digits with 32 solutions in 82 ms
15 digits with 32 solutions in 141 ms
16 digits with 34 solutions in 238 ms
17 digits with 37 solutions in 395 ms
18 digits with 37 solutions in 644 ms
19 digits with 41 solutions in 1028 ms
20029999 recursions
1 153
2 370
3 371
4 407
5 1634
6 8208
7 9474
8 54748
9 92727
10 93084
11 548834
12 1741725
13 4210818
14 9800817
15 9926315
16 24678050
17 24678051
18 88593477
19 146511208
20 472335975
21 534494836
22 912985153
23 4679307774
24 32164049650
25 32164049651
26 40028394225
27 42678290603
28 44708635679
29 49388550606
30 82693916578
31 94204591914
32 28116440335967
33 4338281769391370
34 4338281769391371
35 21897142587612075
36 35641594208964132
37 35875699062250035
38 1517841543307505039
39 3289582984443187032
40 4498128791164624869
41 4929273885928088826</pre>
 
=={{header|Perl}}==
Simple version using a naive predicate. About 15 seconds.
<syntaxhighlight lang="perl">use v5.36;
<lang perl>sub is_narcissistic {
 
my $n = shift;
sub is_narcissistic ($n) {
my($k,$sum) = (length($n),0);
$sum += $_**my($k, for$sum) = split(//,length $n, 0);
$nsum =+= $sum_**$k for split '', $n;
$n == $sum
}
my $i = 0;
for (1..25) {
$i++ while !is_narcissistic($i);
say $i++;
}</lang>
 
my ($i,@N) = 0;
=={{header|Phix}}==
while (@N < 25) {
<lang Phix>function narcissistic(integer n)
$i++ while not is_narcissistic $i;
string d = sprintf("%d",n)
integerpush l@N, = length(d)$i++
}
integer sumn = 0
for i=1 to l do
sumn += power(d[i]-'0',l)
end for
return sumn=n
end function
 
say join ' ', @N;</syntaxhighlight>
sequence s = {}
{{out}}
integer n = 0
<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>
while length(s)<25 do
 
if narcissistic(n) then s &= n end if
=={{header|Phix}}==
n += 1
<!--<syntaxhighlight lang="phix">(phixonline)-->
end while
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
?s</lang>
<span style="color: #008080;">function</span> <span style="color: #000000;">narcissistic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">string</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">l</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">sumn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">l</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">sumn</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]-</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span><span style="color: #000000;">l</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">sumn</span><span style="color: #0000FF;">=</span><span style="color: #000000;">n</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">n</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">while</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)<</span><span style="color: #000000;">25</span> <span style="color: #008080;">do</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">narcissistic</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">n</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #000000;">n</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,052 ⟶ 4,135:
At least 100 times faster, gets the first 47 (the native precision limit) before the above gets the first 25.<br>
I tried a gmp version, but it was 20-odd times slower, presumably because it uses that mighty sledgehammer for many small int cases.
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>-- Begin with zero, which is narcissistic by definition and is never the only digit used in other numbers.
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
sequence output = {`0`}
<span style="color: #000080;font-style:italic;">-- Begin with zero, which is narcissistic by definition and is never the only digit used in other numbers.</span>
bool done = false
<span style="color: #004080;">sequence</span> <span style="color: #000000;">output</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #008000;">`0`</span><span style="color: #0000FF;">}</span>
integer m = 0
<span style="color: #004080;">bool</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
integer q
<span style="color: #004080;">integer</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
atom t0 = time()
<span style="color: #004080;">integer</span> <span style="color: #000000;">q</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
procedure recurse(sequence digits, atom powsum, integer rem)
-- Recursive subhandler. Builds lists containing m digit values while summing the digits' mth powers.
<span style="color: #008080;">procedure</span> <span style="color: #000000;">recurse</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">powsum</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">)</span>
-- If m digits have been obtained, compare the sum of powers's digits with the values in the list.
<span style="color: #000080;font-style:italic;">-- Recursive subhandler. Builds lists containing m digit values while summing the digits' mth powers.
-- Otherwise continue branching the recursion to derive longer lists.
-- If m digits have been obtained, compare the sum of powers's digits with the values in the list.
if rem=0 then
-- Otherwise continue branching the recursion to derive longer lists.</span>
atom temp = powsum
<span style="color: #008080;">if</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
integer unmatched = m
<span style="color: #004080;">atom</span> <span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">powsum</span>
while temp do
<span style="color: #004080;">integer</span> <span style="color: #000000;">unmatched</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">m</span>
integer d = find(remainder(temp,10),digits)
<span style="color: #008080;">while</span> <span style="color: #000000;">temp</span> <span style="color: #008080;">do</span>
if d=0 then exit end if
<span style="color: #004080;">integer</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #7060A8;">remainder</span><span style="color: #0000FF;">(</span><span style="color: #000000;">temp</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span>
digits[d] = -1
<span style="color: #008080;">if</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
unmatched -= 1
<span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">d</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">' '</span>
temp = floor(temp/10)
<span style="color: #000000;">unmatched</span> <span style="color: #0000FF;">-=</span> <span style="color: #000000;">1</span>
end while
<span style="color: #000000;">temp</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">floor</span><span style="color: #0000FF;">(</span><span style="color: #000000;">temp</span><span style="color: #0000FF;">/</span><span style="color: #000000;">10</span><span style="color: #0000FF;">)</span>
-- If all the digits have been matched, the sum of powers is narcissistic.
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
if unmatched=0 then
<span style="color: #000080;font-style:italic;">-- If all the digits have been matched, the sum of powers is narcissistic.</span>
output = append(output,sprintf("%d",powsum))
<span style="color: #008080;">if</span> <span style="color: #000000;">unmatched</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">then</span>
if length(output)=q then done = true end if
<span style="color: #000000;">output</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">output</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">powsum</span><span style="color: #0000FF;">))</span>
end if
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">output</span><span style="color: #0000FF;">)=</span><span style="color: #000000;">q</span> <span style="color: #008080;">then</span> <span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
else
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
-- If fewer than m digits at this level, derive longer lists from the current one.
<span style="color: #008080;">else</span>
-- Adding only values that are less than or equal to the last one makes each
<span style="color: #000080;font-style:italic;">-- If fewer than m digits at this level, derive longer lists from the current one.
-- collection unique and turns up the narcissistic numbers in numerical order.
-- Adding only values that are less than or equal to the last one makes each
--
-- collection unique and turns up the narcissistic numbers in numerical order.
-- ie/eg if sum(sq_power({9,7,4,4},4))==9474, and as shown sort(digits)==list,
--
-- then 9474 is the one and only permutation that is narcissistic, obviously,
-- ie/eg if sum(sq_power({9,7,4,4},4))==9474, and as shown sort(digits)==list,
-- and there is no point looking at any other permutation of that list, ever.
-- Alsothen 1000,1100,1110,11119474 areis the one and only 4permutation liststhat beginningis 1narcissistic, as opposed to obviously,
-- and there is no point looking at any other permutation of that list, ever.
-- the 999 four-digit numbers beginning 1 that might otherwise be checked,
-- andAlso likewise1000,1100,1110,1111 9000..9999are isthe actuallyonly just4 220lists ratherbeginning than1, theas opposed fullto 999.
-- (Ithe can999 seefour-digit thatnumbers exploringbeginning smaller1 partial sums firstthat willmight tendotherwise tobe issuechecked,
-- and results inlikewise numeric9000..9999 order,is butactually cannotjust see220 anrather absolutethan certaintythe offull that)999.
-- (I can see that exploring smaller partial sums first will tend to issue
--
-- results in numeric order, but cannot see an absolute certainty of that)
for d=0 to digits[$] do
--</span>
recurse(digits & d, powsum + power(d,m), rem-1)
<span style="color: #008080;">for</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span> <span style="color: #008080;">to</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[$]-</span><span style="color: #008000;">'0'</span> <span style="color: #008080;">do</span>
if done then exit end if
<span style="color: #000000;">recurse</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span> <span style="color: #0000FF;">&</span> <span style="color: #000000;">d</span><span style="color: #0000FF;">+</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">powsum</span> <span style="color: #0000FF;">+</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">rem</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">done</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end procedure
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">procedure</span>
function narcissisticDecimalNumbers(integer qp)
atom t1 = time()+1
<span style="color: #008080;">function</span> <span style="color: #000000;">narcissisticDecimalNumbers</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">qp</span><span style="color: #0000FF;">)</span>
q = qp
<span style="color: #004080;">atom</span> <span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
-- Initiate the recursive building and testing of collections of increasing numbers of digit values.
<span style="color: #000000;">q</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">qp</span>
while not done do
<span style="color: #000080;font-style:italic;">-- Initiate the recursive building and testing of collections of increasing numbers of digit values.</span>
m += 1
<span style="color: #008080;">while</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">do</span>
if m > iff(machine_bits()=32?16:17) then
<span style="color: #000000;">m</span> <span style="color: #0000FF;">+=</span> <span style="color: #000000;">1</span>
output = append(output,"Remaining numbers beyond number precision")
<span style="color: #008080;">if</span> <span style="color: #000000;">m</span> <span style="color: #0000FF;">></span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">16</span><span style="color: #0000FF;">:</span><span style="color: #000000;">17</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
done = true
<span style="color: #000000;">output</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">output</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Remaining numbers beyond number precision"</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">done</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
for digit=1 to 9 do
<span style="color: #008080;">else</span>
recurse({digit}, power(digit,m), m-1)
<span style="color: #008080;">for</span> <span style="color: #000000;">digit</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #000000;">9</span> <span style="color: #008080;">do</span>
if done then exit end if
<span style="color: #000000;">recurse</span><span style="color: #0000FF;">(</span><span style="color: #008000;">""</span><span style="color: #0000FF;">&</span><span style="color: #008000;">'0'</span><span style="color: #0000FF;">+</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">,</span> <span style="color: #7060A8;">power</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digit</span><span style="color: #0000FF;">,</span><span style="color: #000000;">m</span><span style="color: #0000FF;">),</span> <span style="color: #000000;">m</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">if</span> <span style="color: #000000;">done</span> <span style="color: #008080;">then</span> <span style="color: #008080;">exit</span> <span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
if not done and time()>t1 then
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
printf(1,"searching... %d found, length %d, %s\n",
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">done</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()></span><span style="color: #000000;">t1</span> <span style="color: #008080;">and</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
{length(output),m,elapsed(time()-t0)})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"searching... %d found, length %d, %s\n"</span><span style="color: #0000FF;">,</span>
t1 = time()+1
<span style="color: #0000FF;">{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">output</span><span style="color: #0000FF;">),</span><span style="color: #000000;">m</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
end if
<span style="color: #000000;">t1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()+</span><span style="color: #000000;">1</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end while
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return output
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">output</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence r = narcissisticDecimalNumbers(iff(machine_bits()=32?44:47))
pp(r)
<span style="color: #004080;">sequence</span> <span style="color: #000000;">r</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">narcissisticDecimalNumbers</span><span style="color: #0000FF;">(</span><span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">44</span><span style="color: #0000FF;">:</span><span style="color: #000000;">47</span><span style="color: #0000FF;">))</span>
printf(1,"found %d in %s\n",{length(r),elapsed(time()-t0)})</lang>
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"found %d in %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">r</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 3,143 ⟶ 4,229:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(let (C 25 N 0 L 1)
(loop
(when
Line 3,155 ⟶ 4,241:
(T (=0 C) 'done) ) )
(bye)</langsyntaxhighlight>
 
=={{header|PL/I}}==
===version 1===
{{trans|REXX}}
<langsyntaxhighlight lang="pli"> narn: Proc Options(main);
Dcl (j,k,l,nn,n,sum) Dec Fixed(15)init(0);
Dcl s Char(15) Var;
Line 3,211 ⟶ 4,297:
Return(result);
End
End;</langsyntaxhighlight>
{{out}}
<pre> 1 narcissistic: 0 0 16:10:17.586
Line 3,246 ⟶ 4,332:
===version 2===
Precompiled powers
<syntaxhighlight lang="text">*process source xref attributes or(!);
narn3: Proc Options(main);
Dcl (i,j,k,l,nn,n,sum) Dec Fixed(15)init(0);
Line 3,302 ⟶ 4,388:
Return(result);
End;
End;</langsyntaxhighlight>
{{out}}
<pre> 1 narcissistic: 0 0 00:41:43.632
Line 3,334 ⟶ 4,420:
29 narcissistic: 146511208 199768 00:50:22.777
30 narcissistic: 472335975 1221384 01:10:44.161 </pre>
 
=={{header|PL/M}}==
PL/M-80 only supports 16-bit integers, so this prints only the first 18 narcissistic decimal numbers.
 
<syntaxhighlight lang="plm">100H:
BDOS: PROCEDURE (FN,AR); DECLARE FN BYTE, AR ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PR$CHAR: PROCEDURE (CR); DECLARE CR BYTE; CALL BDOS(2,CR); END PR$CHAR;
PR$STR: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PR$STR;
 
DIGITS: PROCEDURE (N,BUF) BYTE;
DECLARE (N, BUF) ADDRESS;
DECLARE (DIGIT BASED BUF, TEMP, I, LEN) BYTE;
I = 5;
STEP:
DIGIT(I := I-1) = N MOD 10;
IF (N := N/10) > 0 THEN GO TO STEP;
LEN = 0;
DO WHILE I<5;
DIGIT(LEN) = DIGIT(I);
LEN = LEN+1;
I = I+1;
END;
RETURN LEN;
END DIGITS;
 
PR$NUM: PROCEDURE (N);
DECLARE N ADDRESS, DS (5) BYTE, I BYTE;
DO I = 0 TO DIGITS(N,.DS) - 1;
CALL PR$CHAR('0' + DS(I));
END;
CALL PR$STR(.(13,10,'$'));
END PR$NUM;
 
POWER: PROCEDURE (N,P) ADDRESS;
DECLARE (N, P, R) ADDRESS;
R = 1;
DO WHILE P > 0;
R = R * N;
P = P - 1;
END;
RETURN R;
END POWER;
 
NARCISSIST: PROCEDURE (N) ADDRESS;
DECLARE (LEN, I) BYTE, DS (5) BYTE;
DECLARE (N, POWSUM) ADDRESS;
LEN = DIGITS(N, .DS);
POWSUM = 0;
DO I = 0 TO LEN-1;
POWSUM = POWSUM + POWER(DS(I), LEN);
END;
RETURN POWSUM = N;
END NARCISSIST;
 
DECLARE CAND ADDRESS;
DO CAND = 0 TO 65534;
IF NARCISSIST(CAND) THEN CALL PR$NUM(CAND);
END;
 
CALL EXIT;
EOF</syntaxhighlight>
{{out}}
<pre>0
1
2
3
4
5
6
7
8
9
153
370
371
407
1634
8208
9474
54748</pre>
 
=={{header|PowerShell}}==
<syntaxhighlight lang="powershell">
<lang PowerShell>
function Test-Narcissistic ([int]$Number)
{
Line 3,367 ⟶ 4,534:
 
$narcissisticNumbers | Format-Wide {"{0,7}" -f $_} -Column 5 -Force
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 3,375 ⟶ 4,542:
8208 9474 54748 92727 93084
548834 1741725 4210818 9800817 9926315
</pre>
 
=={{header|Prolog}}==
works with swi-prolog
<syntaxhighlight lang="prolog">
digits(0, []):-!.
digits(N, [D|DList]):-
divmod(N, 10, N1, D),
digits(N1, DList).
 
combi(0, _, []).
combi(N, [X|T], [X|Comb]):-
N > 0,
N1 is N - 1,
combi(N1, [X|T], Comb).
combi(N, [_|T], Comb):-
N > 0,
combi(N, T, Comb).
 
powSum([], _, Sum, Sum).
powSum([D|DList], Pow, Acc, Sum):-
Acc1 is Acc + D^Pow,
powSum(DList, Pow, Acc1, Sum).
 
armstrong(Exp, PSum):-
numlist(0, 9, DigList),
(Exp > 1 ->
Min is 10^(Exp - 1)
; Min is 0
),
Max is 10^Exp - 1,
combi(Exp, DigList, Comb),
powSum(Comb, Exp, 0, PSum),
between(Min, Max, PSum),
digits(PSum, DList),
sort(0, @=<, DList, DSort), % hold equal digits
( DSort = Comb;
PSum =:= 0, % special case because
Comb = [0] % DList in digits(0, DList) is [] and not [0]
).
do:-between(1, 7, Exp),
findall(ArmNum, armstrong(Exp, ArmNum), ATemp),
sort(ATemp, AList),
writef('%d -> %w\n', [Exp, AList]),
fail.
do.
</syntaxhighlight>
{{out}}
<pre>
?- time(do).
1 -> [0,1,2,3,4,5,6,7,8,9]
2 -> []
3 -> [153,370,371,407]
4 -> [1634,8208,9474]
5 -> [54748,92727,93084]
6 -> [548834]
7 -> [1741725,4210818,9800817,9926315]
% 666,266 inferences, 0.120 CPU in 0.120 seconds (100% CPU, 5557841 Lips)
true.
</pre>
 
Line 3,381 ⟶ 4,608:
This solution pre-computes the powers once.
 
<langsyntaxhighlight lang="python">from __future__ import print_function
from itertools import count, islice
 
Line 3,398 ⟶ 4,625:
print(n, end=' ')
if i % 5 == 0: print()
print()</langsyntaxhighlight>
 
{{out}}
Line 3,410 ⟶ 4,637:
 
{{trans|D}}
<langsyntaxhighlight lang="python">try:
import psyco
psyco.full()
Line 3,467 ⟶ 4,694:
narc.show(i)
 
main()</langsyntaxhighlight>
{{out}}
<pre>length 1: 9 8 7 6 5 4 3 2 1 0
Line 3,487 ⟶ 4,714:
{{Trans|Haskell}}{{Trans|JavaScript}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Narcissistic decimal numbers'''
 
from itertools import chain
Line 3,606 ⟶ 4,833:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Narcissistic numbers of digit lengths 1 to 7:
Line 3,617 ⟶ 4,844:
6 -> [548834]
7 -> [1741725, 4210818, 9800817, 9926315]</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="quackery"> [ [] swap
[ 10 /mod
rot join swap
dup 0 = until ]
drop ] is digits ( n --> [ )
 
[ dup digits
0 over size rot
witheach
[ over ** rot + swap ]
drop = ] is narcissistic ( n --> b )
 
[] 0
[ dup narcissistic if
[ tuck join swap ]
1+ over size 25 = until ]
drop echo</syntaxhighlight>
 
{{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|R}}==
===For loop solution===
This is a slow method and it needed above 5 minutes on a i3 machine.
<langsyntaxhighlight Rlang="rsplus">for (u in 1:10000000) {
j <- nchar(u)
set2 <- c()
Line 3,632 ⟶ 4,883:
}
if (sum(control) == u) print(u)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,667 ⟶ 4,918:
*As we are using format anyway, we take the chance to make the output look nicer.
 
<langsyntaxhighlight Rlang="rsplus">generateArmstrong <- function(howMany)
{
resultCount <- i <- 0
while(resultCount < howMany)
{
#The next line looks terrible, but I know of no better way to convert a large integer in to its digits in R.
digits <- as.integer(unlist(strsplit(format(i, scientific = FALSE), "")))
if(i == sum(digits^(length(digits)))) cat("Armstrong number ", resultCount <- resultCount + 1, ": ", format(i, big.mark = ","), "\n", sep = "")
{i <- i + 1
cat("Armstrong number ",resultCount<-resultCount+1,": ",format(i, big.mark = ","),"\n",sep="")
}
i<-i+1
}
}
generateArmstrong(25)</langsyntaxhighlight>
{{out}}
<pre>Armstrong number 1: 0
Line 3,710 ⟶ 4,958:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">;; OEIS: A005188 defines these as positive numbers, so I will follow that definition in the function
;; definitions.
;;
Line 3,773 ⟶ 5,021:
(n (sequence-filter narcissitic? (in-naturals 1)))) n)
'(1 2 3 4 5 6 7 8 9 153 370 371))
(check-equal? (next-narcissitics 0 12) '(1 2 3 4 5 6 7 8 9 153 370 371)))</langsyntaxhighlight>
 
{{out}}
Line 3,781 ⟶ 5,029:
===Faster Version===
This version uses lists of digits, rather than numbers themselves.
<langsyntaxhighlight lang="racket">#lang racket
(define (non-decrementing-digital-sequences L)
(define (inr d l)
Line 3,839 ⟶ 5,087:
(check-equal? (narcissitic-numbers-of-length<= 1) '(0 1 2 3 4 5 6 7 8 9))
(check-equal? (narcissitic-numbers-of-length<= 3) '(0 1 2 3 4 5 6 7 8 9 153 370 371 407)))</langsyntaxhighlight>
 
{{out}}
Line 3,846 ⟶ 5,094:
=={{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 }
 
===Simple, with concurrency===
for 0 .. * {
Simple implementation is not exactly speedy, but concurrency helps move things along.
if .&is-narcissistic {
<syntaxhighlight lang="raku" line>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars }
.say;
my @N = lazy (0..∞).hyper.grep: *.&is-narcissistic;
last if ++state$ >= 25;
@N[^25].join(' ').say;</syntaxhighlight>
}
}</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
Ctrl-C</pre>
 
Here the program was interrupted but if you're patient enough you'll see all the 25 numbers.
 
===Single-threaded, with precalculations===
Here's a faster version that precalculates the values for base 1000 digits:
This version that precalculates the values for base 1000 digits, but despite the extra work ends up taking more wall-clock time than the simpler version.
<lang perl6>sub kigits($n) {
<syntaxhighlight lang="raku" line>sub kigits($n) {
my int $i = $n;
my int $b = 1000;
Line 3,892 ⟶ 5,122:
say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d;
last if $l > 25
};</langsyntaxhighlight>
{{out}}
<pre>1 0
Line 3,922 ⟶ 5,152:
=={{header|REXX}}==
===idiomatic===
<langsyntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
Line 3,938 ⟶ 5,168:
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default input:}}
<pre>
Line 3,972 ⟶ 5,202:
 
It is about &nbsp; '''77%''' &nbsp; faster then 1<sup>st</sup> REXX version.
<langsyntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
Line 3,994 ⟶ 5,224:
#=# + 1 /*bump count of narcissistic numbers. */
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/
end /*j*/ /*stick a fork in it, we're all done. */</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
Line 4,004 ⟶ 5,234:
It is about &nbsp; &nbsp; '''44%''' &nbsp; faster then 2<sup>nd</sup> REXX version, &nbsp; and
<br>it is about &nbsp; '''154%''' &nbsp; faster then 1<sup>st</sup> REXX version.
<langsyntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
Line 4,034 ⟶ 5,264:
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
Line 4,043 ⟶ 5,273:
<br>it is about &nbsp; '''136%''' &nbsp; faster then 2<sup>nd</sup> REXX version, &nbsp; and
<br>it is about &nbsp; '''317%''' &nbsp; faster then 1<sup>st</sup> REXX version.
<langsyntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */
numeric digits 39 /*be able to handle largest Armstrong #*/
parse arg N . /*obtain optional argument from the CL.*/
Line 4,082 ⟶ 5,312:
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/
return /*return to invoker & keep on truckin'.*/</langsyntaxhighlight>
{{out|output|text=&nbsp; is identical to the 1<sup>st</sup> REXX version.}}
 
Line 4,089 ⟶ 5,319:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
n = 0
count = 0
Line 4,110 ⟶ 5,340:
nr = (sum = n)
return nr
</syntaxhighlight>
</lang>
 
=={{header|RPL}}==
We started the challenge on a genuine HP-28S, powered by a 4-bit CPU running at 2 MHz.
≪ DUP XPON 1 + → n m
≪ 0 n '''WHILE''' DUP '''REPEAT'''
10 MOD LAST / IP SWAP m ^ ROT + SWAP '''END'''
DROP n ==
≫ ≫ '<span style="color:blue">NAR6?</span>' STO
≪ { 0 } 1 999 '''FOR''' n IF n <span style="color:blue">NAR6?</span> '''THEN''' n + '''END'''
≫ EVAL
It took 4 minutes and 20 seconds to get the first 14 numbers.
{{out}}
<pre>
1: { 1 2 3 4 5 6 7 8 9 153 370 371 407 }
</pre>
Then we switched to the emulator, using 3-digit addition tables.
{{works with|Halcyon Calc|4.2.7}}
≪ → m
≪ { 999 } 0 CON
0 9 '''FOR''' h 0 9 '''FOR''' t 0 9 '''FOR''' u
'''IF''' h t u + + '''THEN''' h 100 * t 10 * u + + h m ^ t m ^ u m ^ + + PUT '''END'''
'''NEXT NEXT NEXT'''
'<span style="color:green">POWM</span>' STO
≫ ≫ '<span style="color:blue">INIT</span>' STO
≪ DUP XPON 1 + → n m
≪ 0 n
'''WHILE''' DUP '''REPEAT'''
1000 MOD LAST / IP
'''IF''' SWAP '''THEN''' LAST <span style="color:green">POWM</span> SWAP GET ROT + SWAP '''END'''
'''END''' DROP n ==
≫ ≫ '<span style="color:blue">NAR6?</span>' STO
≪ DUP <span style="color:blue">INIT</span> DUP ALOG SWAP 1 - ALOG
'''WHILE''' DUP2 > '''REPEAT'''
'''IF''' DUP <span style="color:blue">NAR6?</span> '''THEN''' ROT OVER + ROT ROT '''END'''
1 +
'''END''' DROP2
≫ '<span style="color:blue">RTASK</span>' STO
{{in}}
<pre>
{ 0 } 1 RTASK 2 RTASK 3 RTASK 4 RTASK 5 RTASK 6 RTASK
</pre>
Emulator's watchdog timer has limited the quest to the first 19 Armstrong numbers.
{{out}}
<pre>
1: { 0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 }
</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Integer
def narcissistic?
return false if negative?
digs = self.digits
m = digs.size
digs.mapsum{|d| d**m}.sum == self
end
end
 
puts 0.step.lazy.select(&:narcissistic?).first(25)</langsyntaxhighlight>
{{out}}
<pre>
Line 4,152 ⟶ 5,431:
</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">
fn is_narcissistic(x: u32) -> bool {
let digits: Vec<u32> = x
.to_string()
.chars()
.map(|c| c.to_digit(10).unwrap())
.collect();
 
digits
.iter()
.map(|d| d.pow(digits.len() as u32))
.sum::<u32>()
== x
}
 
fn main() {
let mut counter = 0;
let mut i = 0;
while counter < 25 {
if is_narcissistic(i) {
println!("{}", i);
counter += 1;
}
i += 1;
}
}
</syntaxhighlight>
{{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|Scala}}==
{{works with|Scala|2.9.x}}
 
<langsyntaxhighlight Scalalang="scala">object NDN extends App {
val narc: Int => Int = n => (n.toString map (_.asDigit) map (math.pow(_, n.toString.size)) sum) toInt
Line 4,162 ⟶ 5,497:
println((Iterator from 0 filter isNarc take 25 toList) mkString(" "))
 
}</langsyntaxhighlight>
 
Output:
<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|SETL}}==
<syntaxhighlight lang="setl">program narcissists;
n := 0;
loop until seen = 25 do
if narcissist n then
print(n);
seen +:= 1;
end if;
n +:= 1;
end loop;
 
op narcissist(n);
k := n;
digits := [[k mod 10, k div:= 10](1) : until k=0];
return n = +/[d ** #digits : d in digits];
end op;
end program;</syntaxhighlight>
{{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|Sidef}}==
<langsyntaxhighlight lang="ruby">func is_narcissistic(n) {
n.digits »**» n.len -> sum == n
}
Line 4,178 ⟶ 5,557:
break if (count == 25)
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,210 ⟶ 5,589:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">extension BinaryInteger {
@inlinable
public var isNarcissistic: Bool {
Line 4,232 ⟶ 5,611:
let narcs = Array((0...).lazy.filter({ $0.isNarcissistic }).prefix(25))
 
print("First 25 narcissistic numbers are \(narcs)")</langsyntaxhighlight>
 
{{out}}
Line 4,239 ⟶ 5,618:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc isNarcissistic {n} {
set m [string length $n]
for {set t 0; set N $n} {$N} {set N [expr {$N / 10}]} {
Line 4,257 ⟶ 5,636:
}
 
puts [join [firstNarcissists 25] ","]</langsyntaxhighlight>
{{out}}
<pre>
Line 4,265 ⟶ 5,644:
=={{header|UNIX Shell}}==
{{works with|ksh93}}
<langsyntaxhighlight lang="bash">function narcissistic {
integer n=$1 len=${#n} sum=0 i
for ((i=0; i<len; i++)); do
Line 4,278 ⟶ 5,657:
done
echo "${nums[*]}"
echo "elapsed: $SECONDS"</langsyntaxhighlight>
 
{{output}}
<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
elapsed: 436.639</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<lang vb>Private Function narcissistic(n As Long) As Boolean
Dim d As String: d = CStr(n)
Dim l As Integer: l = Len(d)
Dim sumn As Long: sumn = 0
For i = 1 To l
sumn = sumn + (Mid(d, i, 1) - "0") ^ l
Next i
narcissistic = sumn = n
End Function
 
Public Sub main()
Dim s(24) As String
Dim n As Long: n = 0
Dim found As Integer: found = 0
Do While found < 25
If narcissistic(n) Then
s(found) = CStr(n)
found = found + 1
End If
n = n + 1
Loop
Debug.Print Join(s, ", ")
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>
 
=={{header|VBScript}}==
<lang vb>Function Narcissist(n)
i = 0
j = 0
Do Until j = n
sum = 0
For k = 1 To Len(i)
sum = sum + CInt(Mid(i,k,1)) ^ Len(i)
Next
If i = sum Then
Narcissist = Narcissist & i & ", "
j = j + 1
End If
i = i + 1
Loop
End Function
 
WScript.StdOut.Write Narcissist(25)</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|Wren}}==
{{trans|Go}}
<langsyntaxhighlight ecmascriptlang="wren">var narc = Fn.new { |n|
var power = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
var limit = 10
Line 4,355 ⟶ 5,687:
}
 
System.print(narc.call(25))</langsyntaxhighlight>
 
{{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|XPL0}}==
This is based on Ring's version for Own Digits Power Sum.
<syntaxhighlight lang="xpl0">func IPow(A, B); \A^B
int A, B, T, I;
[T:= 1;
for I:= 1 to B do T:= T*A;
return T;
];
 
int Count, M, N, Sum, T, Dig;
[Text(0, "0 ");
Count:= 1;
for M:= 1 to 9 do
for N:= IPow(10, M-1) to IPow(10, M)-1 do
[Sum:= 0;
T:= N;
while T do
[T:= T/10;
Dig:= rem(0);
Sum:= Sum + IPow(Dig, M);
];
if Sum = N then
[IntOut(0, N); ChOut(0, ^ );
Count:= Count+1;
if Count >= 25 then exit;
];
];
]</syntaxhighlight>
 
{{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|zkl}}==
<langsyntaxhighlight lang="zkl">fcn isNarcissistic(n){
ns,m := n.split(), ns.len() - 1;
ns.reduce('wrap(s,d){ z:=d; do(m){z*=d} s+z },0) == n
}</langsyntaxhighlight>
Pre computing the first 15 powers of 0..9 for use as a look up table speeds things up quite a bit but performance is pretty underwhelming.
<langsyntaxhighlight lang="zkl">var [const] powers=(10).pump(List,'wrap(n){
(1).pump(15,List,'wrap(p){ n.toFloat().pow(p).toInt() }) });
fcn isNarcissistic2(n){
m:=(n.numDigits - 1);
n.split().reduce('wrap(s,d){ s + powers[d][m] },0) == n
}</langsyntaxhighlight>
Now stick a filter on a infinite lazy sequence (ie iterator) to create an infinite sequence of narcissistic numbers (iterator.filter(n,f) --> n results of f(i).toBool()==True).
<langsyntaxhighlight lang="zkl">ns:=[0..].filter.fp1(isNarcissistic);
ns(15).println();
ns(5).println();
ns(5).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 4,385 ⟶ 5,751:
L(548834,1741725,4210818,9800817,9926315)
</pre>
 
=={{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)
<lang zxbasic> 1 DIM K(10): DIM M(10)
2 FOR Y=0 TO 9: LET M(Y+1)=Y: NEXT Y
3 FOR N=1 TO 7
4 FOR J=N TO 0 STEP -1
5 FOR I=N-J TO 0 STEP -1
6 FOR H=N-J-I TO 0 STEP -1
7 FOR G=N-J-I-H TO 0 STEP -1
8 FOR F=N-J-I-H-G TO 0 STEP -1
9 FOR E=N-J-I-H-G-F TO 0 STEP -1
10 FOR D=N-J-I-H-G-F-E TO 0 STEP -1
11 FOR C=N-J-I-H-G-F-E-D TO 0 STEP -1
12 FOR B=N-J-I-H-G-F-E-D-C TO 0 STEP -1
13 LET A=N-J-I-H-G-F-E-D-C-B
14 LET X=B+C*M(3)+D*M(4)+E*M(5)+F*M(6)+G*M(7)+H*M(8)+I*M(9)+J*M(10)
15 LET S$=STR$ (X)
16 IF LEN (S$)<N THEN GO TO 34
17 IF LEN (S$)<>N THEN GO TO 33
18 FOR Y=1 TO 10: LET K(Y)=0: NEXT Y
19 FOR Y=1 TO N
20 LET Z= CODE (S$(Y))-47
21 LET K(Z)=K(Z)+1
22 NEXT Y
23 IF A<>K(1) THEN GO TO 33
24 IF B<>K(2) THEN GO TO 33
25 IF C<>K(3) THEN GO TO 33
26 IF D<>K(4) THEN GO TO 33
27 IF E<>K(5) THEN GO TO 33
28 IF F<>K(6) THEN GO TO 33
29 IF G<>K(7) THEN GO TO 33
30 IF H<>K(8) THEN GO TO 33
31 IF I<>K(9) THEN GO TO 33
32 IF J=K(10) THEN PRINT X,
33 NEXT B: NEXT C: NEXT D: NEXT E: NEXT F: NEXT G: NEXT H: NEXT I: NEXT J
34 FOR Y=2 TO 9
35 LET M(Y+1)=M(Y+1)*Y
36 NEXT Y
37 NEXT N
38 PRINT
39 PRINT "DONE"</lang>
{{out}}
<pre>9 8
7 6
5 4
3 2
1 0
9 8
7 6
5 4
3 2
1 0
407 371
370 153
9474 8208
1634 93084
92727 54748
548834 9926315
9800817 4210818
1741725</pre>
9,476

edits