Narcissistic decimal number: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) m (→{{header|Raku}}: split versions apart, add concurrency to 1st) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32: | Line 32: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F narcissists(m) |
||
[Int] result |
[Int] result |
||
L(digits) 0.. |
L(digits) 0.. |
||
Line 49: | Line 49: | ||
print(n, end' ‘ ’) |
print(n, end' ‘ ’) |
||
I (L.index + 1) % 5 == 0 |
I (L.index + 1) % 5 == 0 |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 62: | Line 62: | ||
=={{header|Ada}}== |
=={{header|Ada}}== |
||
< |
<syntaxhighlight lang="ada">with Ada.Text_IO; |
||
procedure Narcissistic is |
procedure Narcissistic is |
||
Line 93: | Line 93: | ||
Current := Current + 1; |
Current := Current + 1; |
||
end loop; |
end loop; |
||
end Narcissistic;</ |
end Narcissistic;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 100: | Line 100: | ||
=={{header|Agena}}== |
=={{header|Agena}}== |
||
Tested with Agena 2.9.5 Win32 |
Tested with Agena 2.9.5 Win32 |
||
< |
<syntaxhighlight lang="agena">scope |
||
# print the first 25 narcissistic numbers |
# print the first 25 narcissistic numbers |
||
Line 155: | Line 155: | ||
io.writeline() |
io.writeline() |
||
epocs</ |
epocs</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 162: | Line 162: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
< |
<syntaxhighlight lang="algol68"># find some narcissistic decimal numbers # |
||
# returns TRUE if n is narcissitic, FALSE otherwise; n should be >= 0 # |
# returns TRUE if n is narcissitic, FALSE otherwise; n should be >= 0 # |
||
Line 194: | Line 194: | ||
FI |
FI |
||
OD; |
OD; |
||
print( ( newline ) )</ |
print( ( newline ) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 202: | Line 202: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|Agena}} |
{{Trans|Agena}} |
||
< |
<syntaxhighlight lang="algolw">begin |
||
% print the first 25 narcissistic numbers % |
% print the first 25 narcissistic numbers % |
||
Line 267: | Line 267: | ||
write() |
write() |
||
end.</ |
end.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 274: | Line 274: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
< |
<syntaxhighlight lang="apl"> |
||
∇r ← digitsOf n;digitList |
∇r ← digitsOf n;digitList |
||
digitList ← ⍬ |
digitList ← ⍬ |
||
Line 308: | Line 308: | ||
getASN 25 |
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 |
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> |
|||
</lang> |
|||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
Line 322: | Line 322: | ||
For an imperative hand-optimisation, and a contrasting view, see the variant approach below :-) |
For an imperative hand-optimisation, and a contrasting view, see the variant approach below :-) |
||
< |
<syntaxhighlight lang="applescript">------------------------- NARCISSI ----------------------- |
||
-- isDaffodil :: Int -> Int -> Bool |
-- isDaffodil :: Int -> Int -> Bool |
||
Line 493: | Line 493: | ||
end script |
end script |
||
end if |
end if |
||
end mReturn</ |
end mReturn</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight 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}</syntaxhighlight> |
||
---- |
---- |
||
Line 502: | Line 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. |
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. |
||
< |
<syntaxhighlight lang="applescript">(* |
||
Return the first q narcissistic decimal numbers |
Return the first q narcissistic decimal numbers |
||
(or as many of the q as can be represented by AppleScript number values). |
(or as many of the q as can be represented by AppleScript number values). |
||
Line 580: | Line 580: | ||
end narcissisticDecimalNumbers |
end narcissisticDecimalNumbers |
||
return narcissisticDecimalNumbers(25)</ |
return narcissisticDecimalNumbers(25)</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
< |
<syntaxhighlight 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}</syntaxhighlight> |
||
=={{header|Arturo}}== |
=={{header|Arturo}}== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
< |
<syntaxhighlight lang="rebol">powers: map 0..9 'x [ |
||
map 0..9 'y [ |
map 0..9 'y [ |
||
x ^ y |
x ^ y |
||
Line 612: | Line 612: | ||
] |
] |
||
inc 'i |
inc 'i |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 643: | Line 643: | ||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
<syntaxhighlight lang="autohotkey"> |
|||
<lang AutoHotkey> |
|||
#NoEnv ; Do not try to use environment variables |
#NoEnv ; Do not try to use environment variables |
||
SetBatchLines, -1 ; Execute as quickly as you can |
SetBatchLines, -1 ; Execute as quickly as you can |
||
Line 686: | Line 686: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 703: | Line 703: | ||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f NARCISSISTIC_DECIMAL_NUMBER.AWK |
# syntax: GAWK -f NARCISSISTIC_DECIMAL_NUMBER.AWK |
||
BEGIN { |
BEGIN { |
||
Line 720: | Line 720: | ||
exit(0) |
exit(0) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
<p>output:</p> |
<p>output:</p> |
||
<pre> |
<pre> |
||
Line 730: | Line 730: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="befunge">p55*\>:>:>:55+%\55+/00gvv_@ |
||
>1>+>^v\_^#!:<p01p00:+1<>\> |
>1>+>^v\_^#!:<p01p00:+1<>\> |
||
>#-_>\>20p110g>\20g*\v>1-v| |
>#-_>\>20p110g>\20g*\v>1-v| |
||
^!p00:-1g00+$_^#!:<-1<^\.:<</ |
^!p00:-1g00+$_^#!:<-1<^\.:<</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 743: | Line 743: | ||
<code>B10</code> is a BQNcrate idiom to get the digits of a number. |
<code>B10</code> is a BQNcrate idiom to get the digits of a number. |
||
< |
<syntaxhighlight lang="bqn">B10 ← 10{⌽𝕗|⌊∘÷⟜𝕗⍟(↕1+·⌊𝕗⋆⁼1⌈⊢)} |
||
IsNarc ← {𝕩=+´⋆⟜≠B10 𝕩} |
IsNarc ← {𝕩=+´⋆⟜≠B10 𝕩} |
||
/IsNarc¨ ↕1e7</ |
/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. |
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}}== |
=={{header|C}}== |
||
Line 756: | Line 756: | ||
The following prints the first 25 numbers, though not in order... |
The following prints the first 25 numbers, though not in order... |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
#include <gmp.h> |
#include <gmp.h> |
||
Line 818: | Line 818: | ||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 843: | Line 843: | ||
=={{header|C sharp|C#}}== |
=={{header|C sharp|C#}}== |
||
< |
<syntaxhighlight lang="csharp"> |
||
using System; |
using System; |
||
Line 883: | Line 883: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 893: | Line 893: | ||
</pre> |
</pre> |
||
===or=== |
===or=== |
||
< |
<syntaxhighlight lang="csharp"> |
||
//Narcissistic numbers: Nigel Galloway: February 17th., 2015 |
//Narcissistic numbers: Nigel Galloway: February 17th., 2015 |
||
using System; |
using System; |
||
Line 922: | Line 922: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 955: | Line 955: | ||
{{libheader|System.Numerics}} |
{{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. |
{{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. |
||
< |
<syntaxhighlight lang="csharp">using System; |
||
using System.Collections.Generic; |
using System.Collections.Generic; |
||
using System.Linq; |
using System.Linq; |
||
Line 1,072: | Line 1,072: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}(with command line parameter = "39") |
{{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 |
<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,237: | Line 1,237: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
< |
<syntaxhighlight lang="cpp"> |
||
#include <iostream> |
#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 1,305: | Line 1,305: | ||
return system( "pause" ); |
return system( "pause" ); |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,313: | Line 1,313: | ||
=={{header|Clojure}}== |
=={{header|Clojure}}== |
||
Find N first Narcissistic numbers. |
Find N first Narcissistic numbers. |
||
<syntaxhighlight lang="clojure"> |
|||
<lang Clojure> |
|||
(ns narcissistic.core |
(ns narcissistic.core |
||
(:require [clojure.math.numeric-tower :as math])) |
(:require [clojure.math.numeric-tower :as math])) |
||
Line 1,327: | Line 1,327: | ||
(defn firstNnarc [n] ;;list of the first "n" Narcissistic numbers. |
(defn firstNnarc [n] ;;list of the first "n" Narcissistic numbers. |
||
(take n (filter narcissistic? (range)))) |
(take n (filter narcissistic? (range)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
by Average-user |
by Average-user |
||
Line 1,337: | Line 1,337: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
<syntaxhighlight lang="cobol"> |
|||
<lang COBOL> |
|||
PROGRAM-ID. NARCISSIST-NUMS. |
PROGRAM-ID. NARCISSIST-NUMS. |
||
DATA DIVISION. |
DATA DIVISION. |
||
Line 1,396: | Line 1,396: | ||
END PROGRAM NARCISSIST-NUMS. |
END PROGRAM NARCISSIST-NUMS. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 1,425: | Line 1,425: | ||
=={{header|Common Lisp}}== |
=={{header|Common Lisp}}== |
||
< |
<syntaxhighlight lang="lisp"> |
||
(defun integer-to-list (n) |
(defun integer-to-list (n) |
||
(map 'list #'digit-char-p (prin1-to-string n))) |
(map 'list #'digit-char-p (prin1-to-string n))) |
||
Line 1,440: | Line 1,440: | ||
counting (narcissisticp c) into narcissistic |
counting (narcissisticp c) into narcissistic |
||
do (if (narcissisticp c) (print c)))) |
do (if (narcissisticp c) (print c)))) |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,475: | Line 1,475: | ||
=={{header|D}}== |
=={{header|D}}== |
||
===Simple Version=== |
===Simple Version=== |
||
< |
<syntaxhighlight lang="d">void main() { |
||
import std.stdio, std.algorithm, std.conv, std.range; |
import std.stdio, std.algorithm, std.conv, std.range; |
||
Line 1,482: | Line 1,482: | ||
writefln("%(%(%d %)\n%)", |
writefln("%(%(%d %)\n%)", |
||
uint.max.iota.filter!isNarcissistic.take(25).chunks(5)); |
uint.max.iota.filter!isNarcissistic.take(25).chunks(5)); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 |
<pre>0 1 2 3 4 |
||
Line 1,492: | Line 1,492: | ||
===Fast Version=== |
===Fast Version=== |
||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.algorithm, std.range, std.array; |
||
uint[] narcissists(in uint m) pure nothrow @safe { |
uint[] narcissists(in uint m) pure nothrow @safe { |
||
Line 1,520: | Line 1,520: | ||
void main() { |
void main() { |
||
writefln("%(%(%d %)\n%)", 25.narcissists.chunks(5)); |
writefln("%(%(%d %)\n%)", 25.narcissists.chunks(5)); |
||
}</ |
}</syntaxhighlight> |
||
With LDC2 compiler prints the same output in less than 0.3 seconds. |
With LDC2 compiler prints the same output in less than 0.3 seconds. |
||
===Faster Version=== |
===Faster Version=== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="d">import std.stdio, std.bigint, std.conv; |
||
struct Narcissistics(TNum, uint maxLen) { |
struct Narcissistics(TNum, uint maxLen) { |
||
Line 1,578: | Line 1,578: | ||
foreach (immutable i; 1 .. maxLength + 1) |
foreach (immutable i; 1 .. maxLength + 1) |
||
narc.show(i); |
narc.show(i); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>length 1: 9 8 7 6 5 4 3 2 1 0 |
<pre>length 1: 9 8 7 6 5 4 3 2 1 0 |
||
Line 1,600: | Line 1,600: | ||
=={{header|Elixir}}== |
=={{header|Elixir}}== |
||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="elixir">defmodule RC do |
||
def narcissistic(m) do |
def narcissistic(m) do |
||
Enum.reduce(1..10, [0], fn digits,acc -> |
Enum.reduce(1..10, [0], fn digits,acc -> |
||
Line 1,631: | Line 1,631: | ||
catch |
catch |
||
x -> IO.inspect x |
x -> IO.inspect x |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,640: | Line 1,640: | ||
=={{header|ERRE}}== |
=={{header|ERRE}}== |
||
< |
<syntaxhighlight lang="erre">PROGRAM NARCISISTIC |
||
!$DOUBLE |
!$DOUBLE |
||
Line 1,661: | Line 1,661: | ||
N=N+1 |
N=N+1 |
||
END LOOP |
END LOOP |
||
END PROGRAM</ |
END PROGRAM</syntaxhighlight> |
||
Output |
Output |
||
<pre> |
<pre> |
||
Line 1,669: | Line 1,669: | ||
=={{header|F Sharp|F#}}== |
=={{header|F Sharp|F#}}== |
||
< |
<syntaxhighlight lang="fsharp"> |
||
//Naïve solution of Narcissitic number: Nigel Galloway - Febryary 18th., 2015 |
//Naïve solution of Narcissitic number: Nigel Galloway - Febryary 18th., 2015 |
||
open System |
open System |
||
Line 1,677: | Line 1,677: | ||
let d = _Digits (n, []) |
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") |
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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,708: | Line 1,708: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
<lang>USING: io kernel lists lists.lazy math math.functions |
<syntaxhighlight lang="text">USING: io kernel lists lists.lazy math math.functions |
||
math.text.utils prettyprint sequences ; |
math.text.utils prettyprint sequences ; |
||
IN: rosetta-code.narcissistic-decimal-number |
IN: rosetta-code.narcissistic-decimal-number |
||
Line 1,722: | Line 1,722: | ||
: main ( -- ) first25 [ pprint bl ] each ; |
: main ( -- ) first25 [ pprint bl ] each ; |
||
MAIN: main</ |
MAIN: main</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,730: | Line 1,730: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{works with|GNU Forth|0.7.0}} |
{{works with|GNU Forth|0.7.0}} |
||
< |
<syntaxhighlight lang="forth"> |
||
: dig.num \ returns input number and the number of its digits ( n -- n n1 ) |
: dig.num \ returns input number and the number of its digits ( n -- n n1 ) |
||
dup |
dup |
||
Line 1,810: | Line 1,810: | ||
25 narc.num |
25 narc.num |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,843: | Line 1,843: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
===Simple Version=== |
===Simple Version=== |
||
< |
<syntaxhighlight lang="freebasic">' normal version: 14-03-2017 |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
' can go up to 18 digits (ulongint is 64bit), above 18 overflow will occur |
' can go up to 18 digits (ulongint is 64bit), above 18 overflow will occur |
||
Line 1,903: | Line 1,903: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9 |
<pre>9 |
||
Line 1,934: | Line 1,934: | ||
<pre>It takes about 35 min. to find all 88 numbers (39 digits). |
<pre>It takes about 35 min. to find all 88 numbers (39 digits). |
||
To go all the way it takes about 2 hours.</pre> |
To go all the way it takes about 2 hours.</pre> |
||
< |
<syntaxhighlight lang="freebasic">' gmp version: 17-06-2015 |
||
' uses gmp |
' uses gmp |
||
' compile with: fbc -s console |
' compile with: fbc -s console |
||
Line 2,036: | Line 2,036: | ||
Print : Print "hit any key to end program" |
Print : Print "hit any key to end program" |
||
Sleep |
Sleep |
||
End</ |
End</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Left side: program output, right side: sorted on length, value |
Left side: program output, right side: sorted on length, value |
||
Line 2,130: | Line 2,130: | ||
=={{header|FunL}}== |
=={{header|FunL}}== |
||
< |
<syntaxhighlight lang="funl">def narcissistic( start ) = |
||
power = 1 |
power = 1 |
||
powers = array( 0..9 ) |
powers = array( 0..9 ) |
||
Line 2,149: | Line 2,149: | ||
narc( start ) |
narc( start ) |
||
println( narcissistic(0).take(25) )</ |
println( narcissistic(0).take(25) )</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,167: | Line 2,167: | ||
=={{header|Go}}== |
=={{header|Go}}== |
||
Nothing fancy as it runs in a fraction of a second as-is. |
Nothing fancy as it runs in a fraction of a second as-is. |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 2,195: | Line 2,195: | ||
func main() { |
func main() { |
||
fmt.Println(narc(25)) |
fmt.Println(narc(25)) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,204: | Line 2,204: | ||
{{trans|FreeBASIC}} |
{{trans|FreeBASIC}} |
||
Maximum for N (double) is14 digits, there are no 15 digits numbers |
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 |
2 FOR A = 0 TO 9 : X2(A) = A : NEXT A |
||
3 FOR N = 1 TO 7 |
3 FOR N = 1 TO 7 |
||
Line 2,234: | Line 2,234: | ||
29 PRINT |
29 PRINT |
||
30 PRINT "done" |
30 PRINT "done" |
||
31 END</ |
31 END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 9 8 7 6 5 |
<pre> 9 8 7 6 5 |
||
Line 2,244: | Line 2,244: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
===Exhaustive search (integer series)=== |
===Exhaustive search (integer series)=== |
||
< |
<syntaxhighlight lang="haskell">import Data.Char (digitToInt) |
||
isNarcissistic :: Int -> Bool |
isNarcissistic :: Int -> Bool |
||
Line 2,253: | Line 2,253: | ||
main :: IO () |
main :: IO () |
||
main = mapM_ print $ take 25 (filter isNarcissistic [0 ..])</ |
main = mapM_ print $ take 25 (filter isNarcissistic [0 ..])</syntaxhighlight> |
||
===Reduced search (unordered digit combinations)=== |
===Reduced search (unordered digit combinations)=== |
||
Line 2,260: | Line 2,260: | ||
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. |
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. |
||
< |
<syntaxhighlight lang="haskell">import Data.Bifunctor (second) |
||
narcissiOfLength :: Int -> [Int] |
narcissiOfLength :: Int -> [Int] |
||
Line 2,306: | Line 2,306: | ||
w = maximum (length . xShow <$> xs) |
w = maximum (length . xShow <$> xs) |
||
in unlines $ |
in unlines $ |
||
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</ |
s : fmap (((++) . rjust w ' ' . xShow) <*> ((" -> " ++) . fxShow . f)) xs</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Narcissistic decimal numbers of length 1-7: |
<pre>Narcissistic decimal numbers of length 1-7: |
||
Line 2,321: | Line 2,321: | ||
The following is a quick, dirty, and slow solution that works in both languages: |
The following is a quick, dirty, and slow solution that works in both languages: |
||
< |
<syntaxhighlight lang="unicon">procedure main(A) |
||
limit := integer(A[1]) | 25 |
limit := integer(A[1]) | 25 |
||
every write(isNarcissitic(seq(0))\limit) |
every write(isNarcissitic(seq(0))\limit) |
||
Line 2,331: | Line 2,331: | ||
every (sum := 0) +:= (!sn)^m |
every (sum := 0) +:= (!sn)^m |
||
return sum = n |
return sum = n |
||
end</ |
end</syntaxhighlight> |
||
Sample run: |
Sample run: |
||
Line 2,365: | Line 2,365: | ||
=={{header|J}}== |
=={{header|J}}== |
||
< |
<syntaxhighlight lang="j">getDigits=: "."0@": NB. get digits from number |
||
isNarc=: (= +/@(] ^ #)@getDigits)"0 NB. test numbers for Narcissism</ |
isNarc=: (= +/@(] ^ #)@getDigits)"0 NB. test numbers for Narcissism</syntaxhighlight> |
||
'''Example Usage''' |
'''Example Usage''' |
||
< |
<syntaxhighlight 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</ |
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|Java}}== |
=={{header|Java}}== |
||
{{works with|Java|1.5+}} |
{{works with|Java|1.5+}} |
||
< |
<syntaxhighlight lang="java5">public class Narc{ |
||
public static boolean isNarc(long x){ |
public static boolean isNarc(long x){ |
||
if(x < 0) return false; |
if(x < 0) return false; |
||
Line 2,395: | Line 2,395: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
||
Line 2,401: | Line 2,401: | ||
{{works with|Java|1.8}} |
{{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! |
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! |
||
< |
<syntaxhighlight lang="java5"> |
||
import java.util.stream.IntStream; |
import java.util.stream.IntStream; |
||
public class NarcissisticNumbers { |
public class NarcissisticNumbers { |
||
Line 2,427: | Line 2,427: | ||
}); |
}); |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
||
Line 2,434: | Line 2,434: | ||
===ES5=== |
===ES5=== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="javascript">function isNarc(x) { |
||
var str = x.toString(), |
var str = x.toString(), |
||
i, |
i, |
||
Line 2,457: | Line 2,457: | ||
} |
} |
||
return n.join(' '); |
return n.join(' '); |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>"0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315"</pre> |
<pre>"0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315"</pre> |
||
Line 2,463: | Line 2,463: | ||
===ES6=== |
===ES6=== |
||
====Exhaustive search (integer series)==== |
====Exhaustive search (integer series)==== |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,502: | Line 2,502: | ||
) |
) |
||
.narc |
.narc |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
< |
<syntaxhighlight lang="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]</syntaxhighlight> |
||
Line 2,514: | Line 2,514: | ||
(Generating the unordered digit combinations directly as power sums allows faster testing later, and needs less space) |
(Generating the unordered digit combinations directly as power sums allows faster testing later, and needs less space) |
||
< |
<syntaxhighlight lang="javascript">(() => { |
||
'use strict'; |
'use strict'; |
||
Line 2,657: | Line 2,657: | ||
// MAIN --- |
// MAIN --- |
||
return main(); |
return main(); |
||
})();</ |
})();</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Narcissistic decimal numbers of lengths [1..7]: |
<pre>Narcissistic decimal numbers of lengths [1..7]: |
||
Line 2,674: | Line 2,674: | ||
A function for checking whether a given non-negative integer is narcissistic could be implemented in jq as follows: |
A function for checking whether a given non-negative integer is narcissistic could be implemented in jq as follows: |
||
< |
<syntaxhighlight lang="jq">def is_narcissistic: |
||
def digits: tostring | explode[] | [.] | implode | tonumber; |
def digits: tostring | explode[] | [.] | implode | tonumber; |
||
def pow(n): . as $x | reduce range(0;n) as $i (1; . * $x); |
def pow(n): . as $x | reduce range(0;n) as $i (1; . * $x); |
||
Line 2,680: | Line 2,680: | ||
(tostring | length) as $len |
(tostring | length) as $len |
||
| . == reduce digits as $d (0; . + ($d | pow($len)) ) |
| . == reduce digits as $d (0; . + ($d | pow($len)) ) |
||
end;</ |
end;</syntaxhighlight> |
||
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]]. |
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: |
To update this array for increasing values of i, the function powers(j) is defined as follows: |
||
< |
<syntaxhighlight lang="jq"># Input: [i, [0^i, 1^i, 2^i, ..., 9^i]] |
||
# Output: [j, [0^j, 1^j, 2^j, ..., 9^j]] |
# Output: [j, [0^j, 1^j, 2^j, ..., 9^j]] |
||
# provided j is i or (i+1) |
# provided j is i or (i+1) |
||
Line 2,691: | Line 2,691: | ||
else .[0] += 1 |
else .[0] += 1 |
||
| reduce range(0;10) as $k (.; .[1][$k] *= $k) |
| reduce range(0;10) as $k (.; .[1][$k] *= $k) |
||
end;</ |
end;</syntaxhighlight> |
||
The function is_narcisstic can now be modified to use powers(j) as follows: |
The function is_narcisstic can now be modified to use powers(j) as follows: |
||
< |
<syntaxhighlight lang="jq"># Input: [n, [i, [0^i, 1^i, 2^i,...]]] where i is the number of digits in n. |
||
def is_narcissistic: |
def is_narcissistic: |
||
def digits: tostring | explode[] | [.] | implode | tonumber; |
def digits: tostring | explode[] | [.] | implode | tonumber; |
||
Line 2,701: | Line 2,701: | ||
| if . < 0 then false |
| if . < 0 then false |
||
else . == reduce digits as $d (0; . + $powers[$d] ) |
else . == reduce digits as $d (0; . + $powers[$d] ) |
||
end;</ |
end;</syntaxhighlight> |
||
'''The task''' |
'''The task''' |
||
< |
<syntaxhighlight lang="jq"># If your jq has "while", then feel free to omit the following definition: |
||
def while(cond; update): |
def while(cond; update): |
||
def _while: if cond then ., (update | _while) else empty end; |
def _while: if cond then ., (update | _while) else empty end; |
||
Line 2,725: | Line 2,725: | ||
| "\(.[2]): \(.[0])" ; |
| "\(.[2]): \(.[0])" ; |
||
narcissistic(25)</ |
narcissistic(25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
< |
<syntaxhighlight lang="sh">jq -r -n -f Narcissitic_decimal_number.jq |
||
1: 0 |
1: 0 |
||
2: 1 |
2: 1 |
||
Line 2,752: | Line 2,752: | ||
23: 4210818 |
23: 4210818 |
||
24: 9800817 |
24: 9800817 |
||
25: 9926315</ |
25: 9926315</syntaxhighlight> |
||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
This easy to implement brute force technique is plenty fast enough to find the first few Narcissistic decimal numbers. |
This easy to implement brute force technique is plenty fast enough to find the first few Narcissistic decimal numbers. |
||
< |
<syntaxhighlight lang="julia">using Printf # for Julia version 1.0+ |
||
function isnarcissist(n, b=10) |
function isnarcissist(n, b=10) |
||
Line 2,779: | Line 2,779: | ||
findnarcissist() |
findnarcissist() |
||
@time findnarcissist(true) |
@time findnarcissist(true) |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
Finding the first 25 Narcissistic numbers: |
Finding the first 25 Narcissistic numbers: |
||
Line 2,811: | Line 2,811: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
< |
<syntaxhighlight lang="scala">// version 1.1.0 |
||
fun isNarcissistic(n: Int): Boolean { |
fun isNarcissistic(n: Int): Boolean { |
||
Line 2,839: | Line 2,839: | ||
} |
} |
||
while (count < 25) |
while (count < 25) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,848: | Line 2,848: | ||
=={{header|Ksh}}== |
=={{header|Ksh}}== |
||
< |
<syntaxhighlight lang="ksh"> |
||
#!/bin/ksh |
#!/bin/ksh |
||
Line 2,889: | Line 2,889: | ||
_isnarcissist ${i} ; (( $? )) && printf "%3d. %d\n" $(( ++cnt )) ${i} |
_isnarcissist ${i} ; (( $? )) && printf "%3d. %d\n" $(( ++cnt )) ${i} |
||
done |
done |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}}<pre> |
{{out}}<pre> |
||
1. 0 |
1. 0 |
||
Line 2,919: | Line 2,919: | ||
=={{header|Lua}}== |
=={{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. |
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. |
||
< |
<syntaxhighlight lang="lua">function isNarc (n) |
||
local m, sum, digit = string.len(n), 0 |
local m, sum, digit = string.len(n), 0 |
||
for pos = 1, m do |
for pos = 1, m do |
||
Line 2,935: | Line 2,935: | ||
end |
end |
||
n = n + 1 |
n = n + 1 |
||
until count == 25</ |
until count == 25</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 2,942: | Line 2,942: | ||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
<syntaxhighlight lang="maple"> |
|||
<lang Maple> |
|||
Narc:=proc(i) |
Narc:=proc(i) |
||
Line 2,966: | Line 2,966: | ||
end do: |
end do: |
||
NDN; |
NDN; |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">narc[1] = 0; |
||
narc[n_] := narc[n] = NestWhile[# + 1 &, narc[n - 1] + 1, Plus @@ (IntegerDigits[#]^IntegerLength[#]) != # &]; |
narc[n_] := narc[n] = NestWhile[# + 1 &, narc[n - 1] + 1, Plus @@ (IntegerDigits[#]^IntegerLength[#]) != # &]; |
||
narc /@ Range[25]</ |
narc /@ Range[25]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315}</pre> |
<pre>{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315}</pre> |
||
=={{header|MATLAB}}== |
=={{header|MATLAB}}== |
||
< |
<syntaxhighlight lang="matlab">function testNarcissism |
||
x = 0; |
x = 0; |
||
c = 0; |
c = 0; |
||
Line 2,992: | Line 2,992: | ||
dig = sprintf('%d', n) - '0'; |
dig = sprintf('%d', n) - '0'; |
||
tf = n == sum(dig.^length(dig)); |
tf = n == sum(dig.^length(dig)); |
||
end</ |
end</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
||
=={{header|Nanoquery}}== |
=={{header|Nanoquery}}== |
||
< |
<syntaxhighlight lang="nanoquery">def is_narcissist(num) |
||
digits = {} |
digits = {} |
||
for digit in str(num) |
for digit in str(num) |
||
Line 3,029: | Line 3,029: | ||
print num + " " |
print num + " " |
||
end |
end |
||
println</ |
println</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315 </pre> |
||
Line 3,035: | Line 3,035: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
A simple solution which runs in about one second. |
A simple solution which runs in about one second. |
||
< |
<syntaxhighlight lang="nim">import sequtils, strutils |
||
func digits(n: Natural): seq[int] = |
func digits(n: Natural): seq[int] = |
||
Line 3,061: | Line 3,061: | ||
m *= 10 |
m *= 10 |
||
echo findNarcissistic(25).join(" ")</ |
echo findNarcissistic(25).join(" ")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,068: | Line 3,068: | ||
=={{header|Oforth}}== |
=={{header|Oforth}}== |
||
< |
<syntaxhighlight lang="oforth">: isNarcissistic(n) |
||
| i m | |
| i m | |
||
n 0 while( n ) [ n 10 /mod ->n swap 1 + ] ->m |
n 0 while( n ) [ n 10 /mod ->n swap 1 + ] ->m |
||
Line 3,077: | Line 3,077: | ||
ListBuffer new dup ->l |
ListBuffer new dup ->l |
||
0 while(l size n <>) [ dup isNarcissistic ifTrue: [ dup l add ] 1 + ] drop ; |
0 while(l size n <>) [ dup isNarcissistic ifTrue: [ dup l add ] 1 + ] drop ; |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
Line 3,088: | Line 3,088: | ||
=={{header|PARI/GP}}== |
=={{header|PARI/GP}}== |
||
Naive code, could be improved by splitting the digits in half and meeting in the middle. |
Naive code, could be improved by splitting the digits in half and meeting in the middle. |
||
< |
<syntaxhighlight 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)))))</ |
v=List();for(n=1,1e9,if(isNarcissistic(n),listput(v,n);if(#v>24, return(Vec(v)))))</syntaxhighlight> |
||
{{out}} |
{{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> |
<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> |
||
Line 3,097: | Line 3,097: | ||
A recursive version starting at the highest digit and recurses to digit 0. Bad runtime. One more digit-> 10x runtime |
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). |
runtime ~ 10^(count of Digits). |
||
< |
<syntaxhighlight lang="pascal"> |
||
program NdN; |
program NdN; |
||
//Narcissistic decimal number |
//Narcissistic decimal number |
||
Line 3,178: | Line 3,178: | ||
NextPowDig; |
NextPowDig; |
||
end; |
end; |
||
end.</ |
end.</syntaxhighlight> |
||
;output: |
;output: |
||
<pre> |
<pre> |
||
Line 3,196: | Line 3,196: | ||
recursive solution.Just counting the different combination of digits<BR> |
recursive solution.Just counting the different combination of digits<BR> |
||
See [[Combinations_with_repetitions]]<BR> |
See [[Combinations_with_repetitions]]<BR> |
||
< |
<syntaxhighlight lang="pascal">program PowerOwnDigits; |
||
{$IFDEF FPC} |
{$IFDEF FPC} |
||
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$COPERATORS ON} |
{$MODE DELPHI}{$OPTIMIZATION ON,ALL}{$COPERATORS ON} |
||
Line 3,398: | Line 3,398: | ||
{$ENDIF} |
{$ENDIF} |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out|@TIO.RUN}} |
{{out|@TIO.RUN}} |
||
<pre style="height:180px"> |
<pre style="height:180px"> |
||
Line 3,464: | Line 3,464: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
Simple version using a naive predicate. |
Simple version using a naive predicate. |
||
< |
<syntaxhighlight lang="perl">use v5.36; |
||
sub is_narcissistic ($n) { |
sub is_narcissistic ($n) { |
||
Line 3,478: | Line 3,478: | ||
} |
} |
||
say join ' ', @N;</ |
say join ' ', @N;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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: #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> |
||
Line 3,502: | Line 3,502: | ||
<span style="color: #008080;">end</span> <span style="color: #008080;">while</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> |
<span style="color: #7060A8;">pp</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> |
||
<!--</ |
<!--</syntaxhighlight>--> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,511: | Line 3,511: | ||
At least 100 times faster, gets the first 47 (the native precision limit) before the above gets the first 25.<br> |
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. |
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)--> |
||
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span> |
||
<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> |
<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> |
||
Line 3,587: | Line 3,587: | ||
<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;">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> |
<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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 3,605: | Line 3,605: | ||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
< |
<syntaxhighlight lang="picolisp">(let (C 25 N 0 L 1) |
||
(loop |
(loop |
||
(when |
(when |
||
Line 3,617: | Line 3,617: | ||
(T (=0 C) 'done) ) ) |
(T (=0 C) 'done) ) ) |
||
(bye)</ |
(bye)</syntaxhighlight> |
||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
===version 1=== |
===version 1=== |
||
{{trans|REXX}} |
{{trans|REXX}} |
||
< |
<syntaxhighlight lang="pli"> narn: Proc Options(main); |
||
Dcl (j,k,l,nn,n,sum) Dec Fixed(15)init(0); |
Dcl (j,k,l,nn,n,sum) Dec Fixed(15)init(0); |
||
Dcl s Char(15) Var; |
Dcl s Char(15) Var; |
||
Line 3,673: | Line 3,673: | ||
Return(result); |
Return(result); |
||
End |
End |
||
End;</ |
End;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 narcissistic: 0 0 16:10:17.586 |
<pre> 1 narcissistic: 0 0 16:10:17.586 |
||
Line 3,708: | Line 3,708: | ||
===version 2=== |
===version 2=== |
||
Precompiled powers |
Precompiled powers |
||
<lang>*process source xref attributes or(!); |
<syntaxhighlight lang="text">*process source xref attributes or(!); |
||
narn3: Proc Options(main); |
narn3: Proc Options(main); |
||
Dcl (i,j,k,l,nn,n,sum) Dec Fixed(15)init(0); |
Dcl (i,j,k,l,nn,n,sum) Dec Fixed(15)init(0); |
||
Line 3,764: | Line 3,764: | ||
Return(result); |
Return(result); |
||
End; |
End; |
||
End;</ |
End;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 1 narcissistic: 0 0 00:41:43.632 |
<pre> 1 narcissistic: 0 0 00:41:43.632 |
||
Line 3,798: | Line 3,798: | ||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
<syntaxhighlight lang="powershell"> |
|||
<lang PowerShell> |
|||
function Test-Narcissistic ([int]$Number) |
function Test-Narcissistic ([int]$Number) |
||
{ |
{ |
||
Line 3,829: | Line 3,829: | ||
$narcissisticNumbers | Format-Wide {"{0,7}" -f $_} -Column 5 -Force |
$narcissisticNumbers | Format-Wide {"{0,7}" -f $_} -Column 5 -Force |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{Out}} |
{{Out}} |
||
<pre> |
<pre> |
||
Line 3,843: | Line 3,843: | ||
This solution pre-computes the powers once. |
This solution pre-computes the powers once. |
||
< |
<syntaxhighlight lang="python">from __future__ import print_function |
||
from itertools import count, islice |
from itertools import count, islice |
||
Line 3,860: | Line 3,860: | ||
print(n, end=' ') |
print(n, end=' ') |
||
if i % 5 == 0: print() |
if i % 5 == 0: print() |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 3,872: | Line 3,872: | ||
{{trans|D}} |
{{trans|D}} |
||
< |
<syntaxhighlight lang="python">try: |
||
import psyco |
import psyco |
||
psyco.full() |
psyco.full() |
||
Line 3,929: | Line 3,929: | ||
narc.show(i) |
narc.show(i) |
||
main()</ |
main()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>length 1: 9 8 7 6 5 4 3 2 1 0 |
<pre>length 1: 9 8 7 6 5 4 3 2 1 0 |
||
Line 3,949: | Line 3,949: | ||
{{Trans|Haskell}}{{Trans|JavaScript}} |
{{Trans|Haskell}}{{Trans|JavaScript}} |
||
{{Works with|Python|3.7}} |
{{Works with|Python|3.7}} |
||
< |
<syntaxhighlight lang="python">'''Narcissistic decimal numbers''' |
||
from itertools import chain |
from itertools import chain |
||
Line 4,068: | Line 4,068: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>Narcissistic numbers of digit lengths 1 to 7: |
<pre>Narcissistic numbers of digit lengths 1 to 7: |
||
Line 4,082: | Line 4,082: | ||
=={{header|Quackery}}== |
=={{header|Quackery}}== |
||
< |
<syntaxhighlight lang="quackery"> [ [] swap |
||
[ 10 /mod |
[ 10 /mod |
||
rot join swap |
rot join swap |
||
Line 4,098: | Line 4,098: | ||
[ tuck join swap ] |
[ tuck join swap ] |
||
1+ over size 25 = until ] |
1+ over size 25 = until ] |
||
drop echo</ |
drop echo</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,107: | Line 4,107: | ||
===For loop solution=== |
===For loop solution=== |
||
This is a slow method and it needed above 5 minutes on a i3 machine. |
This is a slow method and it needed above 5 minutes on a i3 machine. |
||
< |
<syntaxhighlight lang="rsplus">for (u in 1:10000000) { |
||
j <- nchar(u) |
j <- nchar(u) |
||
set2 <- c() |
set2 <- c() |
||
Line 4,118: | Line 4,118: | ||
} |
} |
||
if (sum(control) == u) print(u) |
if (sum(control) == u) print(u) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,153: | Line 4,153: | ||
*As we are using format anyway, we take the chance to make the output look nicer. |
*As we are using format anyway, we take the chance to make the output look nicer. |
||
< |
<syntaxhighlight lang="rsplus">generateArmstrong <- function(howMany) |
||
{ |
{ |
||
resultCount <- i <- 0 |
resultCount <- i <- 0 |
||
Line 4,164: | Line 4,164: | ||
} |
} |
||
} |
} |
||
generateArmstrong(25)</ |
generateArmstrong(25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Armstrong number 1: 0 |
<pre>Armstrong number 1: 0 |
||
Line 4,193: | Line 4,193: | ||
=={{header|Racket}}== |
=={{header|Racket}}== |
||
< |
<syntaxhighlight lang="racket">;; OEIS: A005188 defines these as positive numbers, so I will follow that definition in the function |
||
;; definitions. |
;; definitions. |
||
;; |
;; |
||
Line 4,256: | Line 4,256: | ||
(n (sequence-filter narcissitic? (in-naturals 1)))) n) |
(n (sequence-filter narcissitic? (in-naturals 1)))) n) |
||
'(1 2 3 4 5 6 7 8 9 153 370 371)) |
'(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)))</ |
(check-equal? (next-narcissitics 0 12) '(1 2 3 4 5 6 7 8 9 153 370 371)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,264: | Line 4,264: | ||
===Faster Version=== |
===Faster Version=== |
||
This version uses lists of digits, rather than numbers themselves. |
This version uses lists of digits, rather than numbers themselves. |
||
< |
<syntaxhighlight lang="racket">#lang racket |
||
(define (non-decrementing-digital-sequences L) |
(define (non-decrementing-digital-sequences L) |
||
(define (inr d l) |
(define (inr d l) |
||
Line 4,322: | Line 4,322: | ||
(check-equal? (narcissitic-numbers-of-length<= 1) '(0 1 2 3 4 5 6 7 8 9)) |
(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)))</ |
(check-equal? (narcissitic-numbers-of-length<= 3) '(0 1 2 3 4 5 6 7 8 9 153 370 371 407)))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,332: | Line 4,332: | ||
===Simple, with concurrency=== |
===Simple, with concurrency=== |
||
Simple implementation is not exactly speedy, but concurrency helps move things along. |
Simple implementation is not exactly speedy, but concurrency helps move things along. |
||
<lang |
<syntaxhighlight lang="raku" line>sub is-narcissistic(Int $n) { $n == [+] $n.comb »**» $n.chars } |
||
my @N = lazy (0..∞).hyper.grep: *.&is-narcissistic; |
my @N = lazy (0..∞).hyper.grep: *.&is-narcissistic; |
||
@N[^25].join(' ').say;</ |
@N[^25].join(' ').say;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
<pre>0 1 2 3 4 5 6 7 8 9 153 370 371 407 1634 8208 9474 54748 92727 93084 548834 1741725 4210818 9800817 9926315</pre> |
||
Line 4,340: | Line 4,340: | ||
===Single-threaded, with precalculations=== |
===Single-threaded, with precalculations=== |
||
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. |
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 |
<syntaxhighlight lang="raku" line>sub kigits($n) { |
||
my int $i = $n; |
my int $i = $n; |
||
my int $b = 1000; |
my int $b = 1000; |
||
Line 4,357: | Line 4,357: | ||
say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d; |
say $l++, "\t", $_ if .&is-narcissistic for 10**($d-1) ..^ 10**$d; |
||
last if $l > 25 |
last if $l > 25 |
||
};</ |
};</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>1 0 |
<pre>1 0 |
||
Line 4,387: | Line 4,387: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
===idiomatic=== |
===idiomatic=== |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */ |
||
numeric digits 39 /*be able to handle largest Armstrong #*/ |
numeric digits 39 /*be able to handle largest Armstrong #*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
Line 4,403: | Line 4,403: | ||
#=# + 1 /*bump count of narcissistic numbers. */ |
#=# + 1 /*bump count of narcissistic numbers. */ |
||
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/ |
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/ |
||
end /*j*/ /*stick a fork in it, we're all done. */</ |
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= when using the default input:}} |
{{out|output|text= when using the default input:}} |
||
<pre> |
<pre> |
||
Line 4,437: | Line 4,437: | ||
It is about '''77%''' faster then 1<sup>st</sup> REXX version. |
It is about '''77%''' faster then 1<sup>st</sup> REXX version. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */ |
||
numeric digits 39 /*be able to handle largest Armstrong #*/ |
numeric digits 39 /*be able to handle largest Armstrong #*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
Line 4,459: | Line 4,459: | ||
#=# + 1 /*bump count of narcissistic numbers. */ |
#=# + 1 /*bump count of narcissistic numbers. */ |
||
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/ |
say right(#, 9) ' narcissistic:' j /*display index and narcissistic number*/ |
||
end /*j*/ /*stick a fork in it, we're all done. */</ |
end /*j*/ /*stick a fork in it, we're all done. */</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
||
Line 4,469: | Line 4,469: | ||
It is about '''44%''' faster then 2<sup>nd</sup> REXX version, and |
It is about '''44%''' faster then 2<sup>nd</sup> REXX version, and |
||
<br>it is about '''154%''' faster then 1<sup>st</sup> REXX version. |
<br>it is about '''154%''' faster then 1<sup>st</sup> REXX version. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */ |
||
numeric digits 39 /*be able to handle largest Armstrong #*/ |
numeric digits 39 /*be able to handle largest Armstrong #*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
Line 4,499: | Line 4,499: | ||
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
||
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/ |
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/ |
||
return /*return to invoker & keep on truckin'.*/</ |
return /*return to invoker & keep on truckin'.*/</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
||
Line 4,508: | Line 4,508: | ||
<br>it is about '''136%''' faster then 2<sup>nd</sup> REXX version, and |
<br>it is about '''136%''' faster then 2<sup>nd</sup> REXX version, and |
||
<br>it is about '''317%''' faster then 1<sup>st</sup> REXX version. |
<br>it is about '''317%''' faster then 1<sup>st</sup> REXX version. |
||
< |
<syntaxhighlight lang="rexx">/*REXX program generates and displays a number of narcissistic (Armstrong) numbers. */ |
||
numeric digits 39 /*be able to handle largest Armstrong #*/ |
numeric digits 39 /*be able to handle largest Armstrong #*/ |
||
parse arg N . /*obtain optional argument from the CL.*/ |
parse arg N . /*obtain optional argument from the CL.*/ |
||
Line 4,547: | Line 4,547: | ||
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
say right(#,9) ' narcissistic:' arg(1) /*display index and narcissistic number*/ |
||
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/ |
if #==n & n<11 then exit /*finished showing of narcissistic #'s?*/ |
||
return /*return to invoker & keep on truckin'.*/</ |
return /*return to invoker & keep on truckin'.*/</syntaxhighlight> |
||
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
{{out|output|text= is identical to the 1<sup>st</sup> REXX version.}} |
||
Line 4,554: | Line 4,554: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
n = 0 |
n = 0 |
||
count = 0 |
count = 0 |
||
Line 4,575: | Line 4,575: | ||
nr = (sum = n) |
nr = (sum = n) |
||
return nr |
return nr |
||
</syntaxhighlight> |
|||
</lang> |
|||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
< |
<syntaxhighlight lang="ruby">class Integer |
||
def narcissistic? |
def narcissistic? |
||
return false if negative? |
return false if negative? |
||
Line 4,587: | Line 4,587: | ||
end |
end |
||
puts 0.step.lazy.select(&:narcissistic?).first(25)</ |
puts 0.step.lazy.select(&:narcissistic?).first(25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,618: | Line 4,618: | ||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
< |
<syntaxhighlight lang="rust"> |
||
fn is_narcissistic(x: u32) -> bool { |
fn is_narcissistic(x: u32) -> bool { |
||
let digits: Vec<u32> = x |
let digits: Vec<u32> = x |
||
Line 4,644: | Line 4,644: | ||
} |
} |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,676: | Line 4,676: | ||
{{works with|Scala|2.9.x}} |
{{works with|Scala|2.9.x}} |
||
< |
<syntaxhighlight lang="scala">object NDN extends App { |
||
val narc: Int => Int = n => (n.toString map (_.asDigit) map (math.pow(_, n.toString.size)) sum) toInt |
val narc: Int => Int = n => (n.toString map (_.asDigit) map (math.pow(_, n.toString.size)) sum) toInt |
||
Line 4,683: | Line 4,683: | ||
println((Iterator from 0 filter isNarc take 25 toList) mkString(" ")) |
println((Iterator from 0 filter isNarc take 25 toList) mkString(" ")) |
||
}</ |
}</syntaxhighlight> |
||
Output: |
Output: |
||
Line 4,689: | Line 4,689: | ||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">func is_narcissistic(n) { |
||
n.digits »**» n.len -> sum == n |
n.digits »**» n.len -> sum == n |
||
} |
} |
||
Line 4,699: | Line 4,699: | ||
break if (count == 25) |
break if (count == 25) |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,731: | Line 4,731: | ||
=={{header|Swift}}== |
=={{header|Swift}}== |
||
< |
<syntaxhighlight lang="swift">extension BinaryInteger { |
||
@inlinable |
@inlinable |
||
public var isNarcissistic: Bool { |
public var isNarcissistic: Bool { |
||
Line 4,753: | Line 4,753: | ||
let narcs = Array((0...).lazy.filter({ $0.isNarcissistic }).prefix(25)) |
let narcs = Array((0...).lazy.filter({ $0.isNarcissistic }).prefix(25)) |
||
print("First 25 narcissistic numbers are \(narcs)")</ |
print("First 25 narcissistic numbers are \(narcs)")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,760: | Line 4,760: | ||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
< |
<syntaxhighlight lang="tcl">proc isNarcissistic {n} { |
||
set m [string length $n] |
set m [string length $n] |
||
for {set t 0; set N $n} {$N} {set N [expr {$N / 10}]} { |
for {set t 0; set N $n} {$N} {set N [expr {$N / 10}]} { |
||
Line 4,778: | Line 4,778: | ||
} |
} |
||
puts [join [firstNarcissists 25] ","]</ |
puts [join [firstNarcissists 25] ","]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,786: | Line 4,786: | ||
=={{header|UNIX Shell}}== |
=={{header|UNIX Shell}}== |
||
{{works with|ksh93}} |
{{works with|ksh93}} |
||
< |
<syntaxhighlight lang="bash">function narcissistic { |
||
integer n=$1 len=${#n} sum=0 i |
integer n=$1 len=${#n} sum=0 i |
||
for ((i=0; i<len; i++)); do |
for ((i=0; i<len; i++)); do |
||
Line 4,799: | Line 4,799: | ||
done |
done |
||
echo "${nums[*]}" |
echo "${nums[*]}" |
||
echo "elapsed: $SECONDS"</ |
echo "elapsed: $SECONDS"</syntaxhighlight> |
||
{{output}} |
{{output}} |
||
Line 4,806: | Line 4,806: | ||
=={{header|VBA}}== |
=={{header|VBA}}== |
||
{{trans|Phix}}< |
{{trans|Phix}}<syntaxhighlight lang="vb">Private Function narcissistic(n As Long) As Boolean |
||
Dim d As String: d = CStr(n) |
Dim d As String: d = CStr(n) |
||
Dim l As Integer: l = Len(d) |
Dim l As Integer: l = Len(d) |
||
Line 4,828: | Line 4,828: | ||
Loop |
Loop |
||
Debug.Print Join(s, ", ") |
Debug.Print Join(s, ", ") |
||
End Sub</ |
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> |
<pre>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315</pre> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
< |
<syntaxhighlight lang="vb">Function Narcissist(n) |
||
i = 0 |
i = 0 |
||
j = 0 |
j = 0 |
||
Line 4,848: | Line 4,848: | ||
End Function |
End Function |
||
WScript.StdOut.Write Narcissist(25)</ |
WScript.StdOut.Write Narcissist(25)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315,</pre> |
<pre>0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 153, 370, 371, 407, 1634, 8208, 9474, 54748, 92727, 93084, 548834, 1741725, 4210818, 9800817, 9926315,</pre> |
||
Line 4,854: | Line 4,854: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
< |
<syntaxhighlight lang="ecmascript">var narc = Fn.new { |n| |
||
var power = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
var power = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
||
var limit = 10 |
var limit = 10 |
||
Line 4,876: | Line 4,876: | ||
} |
} |
||
System.print(narc.call(25))</ |
System.print(narc.call(25))</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,885: | Line 4,885: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
This is based on Ring's version for Own Digits Power Sum. |
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; |
int A, B, T, I; |
||
[T:= 1; |
[T:= 1; |
||
Line 4,910: | Line 4,910: | ||
]; |
]; |
||
]; |
]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 4,918: | Line 4,918: | ||
=={{header|zkl}}== |
=={{header|zkl}}== |
||
< |
<syntaxhighlight lang="zkl">fcn isNarcissistic(n){ |
||
ns,m := n.split(), ns.len() - 1; |
ns,m := n.split(), ns.len() - 1; |
||
ns.reduce('wrap(s,d){ z:=d; do(m){z*=d} s+z },0) == n |
ns.reduce('wrap(s,d){ z:=d; do(m){z*=d} s+z },0) == n |
||
}</ |
}</syntaxhighlight> |
||
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. |
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. |
||
< |
<syntaxhighlight lang="zkl">var [const] powers=(10).pump(List,'wrap(n){ |
||
(1).pump(15,List,'wrap(p){ n.toFloat().pow(p).toInt() }) }); |
(1).pump(15,List,'wrap(p){ n.toFloat().pow(p).toInt() }) }); |
||
fcn isNarcissistic2(n){ |
fcn isNarcissistic2(n){ |
||
m:=(n.numDigits - 1); |
m:=(n.numDigits - 1); |
||
n.split().reduce('wrap(s,d){ s + powers[d][m] },0) == n |
n.split().reduce('wrap(s,d){ s + powers[d][m] },0) == n |
||
}</ |
}</syntaxhighlight> |
||
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). |
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). |
||
< |
<syntaxhighlight lang="zkl">ns:=[0..].filter.fp1(isNarcissistic); |
||
ns(15).println(); |
ns(15).println(); |
||
ns(5).println(); |
ns(5).println(); |
||
ns(5).println();</ |
ns(5).println();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 4,943: | Line 4,943: | ||
=={{header|ZX Spectrum Basic}}== |
=={{header|ZX Spectrum Basic}}== |
||
Array index starts at 1. Only 1 character long variable names are allowed for For-Next loops. 8 Digits or higher numbers are displayed as floating point numbers. Needs about 2 hours (3.5Mhz) |
Array index starts at 1. Only 1 character long variable names are allowed for For-Next loops. 8 Digits or higher numbers are displayed as floating point numbers. Needs about 2 hours (3.5Mhz) |
||
< |
<syntaxhighlight lang="zxbasic"> 1 DIM K(10): DIM M(10) |
||
2 FOR Y=0 TO 9: LET M(Y+1)=Y: NEXT Y |
2 FOR Y=0 TO 9: LET M(Y+1)=Y: NEXT Y |
||
3 FOR N=1 TO 7 |
3 FOR N=1 TO 7 |
||
Line 4,981: | Line 4,981: | ||
37 NEXT N |
37 NEXT N |
||
38 PRINT |
38 PRINT |
||
39 PRINT "DONE"</ |
39 PRINT "DONE"</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>9 8 |
<pre>9 8 |