Jump to content

Narcissistic decimal number: Difference between revisions

→‎OCaml: speedup by 4.5 with pre-calculated powers
(add OCaml)
(→‎OCaml: speedup by 4.5 with pre-calculated powers)
Line 3,210:
 
=={{header|OCaml}}==
===;Exhaustive search (integer series)===
<syntaxhighlight lang="ocaml">(*let speed-optimizednarcissistic exponentiation; doesn't support exponents < 1 *)=
let rec pow bnext n l p () =
let rec auxdigit_pow_sum xa en =
if n land 1 = 0
then if n =< 210 then ba *+ bp.(n) else powdigit_pow_sum (ba *+ bp.(n mod 10)) (n lsr/ 110)
in
else if n = 1l then bnext elsen b(l * pow10) (Array.mapi (b * b) (np) lsr 1()
 
else if n = digit_pow_sum 0 n then Seq.Cons (n, next (succ n) l p)
let is_narcissistic n =
else next (succ n) l p ()
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
nnext =0 fst10 (auxArray.init n10 1Fun.id)
 
let () =
narcissistic |> Seq.take 25 |> Seq.iter (Printf.printf " %u") |> print_newline</syntaxhighlight>
Seq.(ints 0 |> filter is_narcissistic |> take 25 |> map string_of_int)
|> List.of_seq |> String.concat " " |> print_endline</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}}==
559

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.