Narcissistic decimal number: Difference between revisions
Content added Content deleted
(Add Haskell - Narcissistic decimal number) |
(Updated D version 1 and 2) |
||
Line 395: | Line 395: | ||
import std.stdio, std.algorithm, std.conv, std.range; |
import std.stdio, std.algorithm, std.conv, std.range; |
||
immutable isNarcissistic = (in uint n) pure => |
immutable isNarcissistic = (in uint n) pure @safe => |
||
n.text.map!(d => (d - '0') ^^ n.text.length).sum == n; |
n.text.map!(d => (d - '0') ^^ n.text.length).sum == n; |
||
writefln("%(%(%d %)\n%)", |
writefln("%(%(%d %)\n%)", |
||
Line 411: | Line 411: | ||
<lang d>import std.stdio, std.algorithm, std.range, std.array; |
<lang d>import std.stdio, std.algorithm, std.range, std.array; |
||
uint[] narcissists(in uint m) pure nothrow { |
uint[] narcissists(in uint m) pure nothrow @safe { |
||
typeof(return) result; |
typeof(return) result; |
||
Line 417: | Line 417: | ||
const digitPowers = 10.iota.map!(i => i ^^ digits).array; |
const digitPowers = 10.iota.map!(i => i ^^ digits).array; |
||
foreach (immutable n; 10 ^^ (digits - 1) .. 10 ^^ digits) { |
foreach (immutable uint n; 10 ^^ (digits - 1) .. 10 ^^ digits) { |
||
uint div = n |
uint digitPSum, div = n; |
||
while (div) { |
while (div) { |
||
digitPSum += digitPowers[div % 10]; |
digitPSum += digitPowers[div % 10]; |