Narcissistic decimal number: Difference between revisions
Content added Content deleted
(→{{header|Chipmunk Basic}}: Added.) |
(add OCaml) |
||
Line 3,206: | Line 3,206: | ||
echo findNarcissistic(25).join(" ")</syntaxhighlight> |
echo findNarcissistic(25).join(" ")</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|OCaml}}== |
|||
===Exhaustive search (integer series)=== |
|||
<syntaxhighlight lang="ocaml">(* speed-optimized exponentiation; doesn't support exponents < 1 *) |
|||
let rec pow b n = |
|||
if n land 1 = 0 |
|||
then if n = 2 then b * b else pow (b * b) (n lsr 1) |
|||
else if n = 1 then b else b * pow (b * b) (n lsr 1) |
|||
let is_narcissistic n = |
|||
let rec aux x e = |
|||
if x < 10 |
|||
then pow x e, e |
|||
else let n, l = aux (x / 10) (succ e) in n + pow (x mod 10) l, l |
|||
in |
|||
n = fst (aux n 1) |
|||
let () = |
|||
Seq.(ints 0 |> filter is_narcissistic |> take 25 |> map string_of_int) |
|||
|> List.of_seq |> String.concat " " |> print_endline</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> |