Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
36
Task/Digital-root/D/digital-root.d
Normal file
36
Task/Digital-root/D/digital-root.d
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import std.stdio, std.typecons, std.conv, std.bigint, std.math,
|
||||
std.traits;
|
||||
|
||||
Tuple!(uint, Unqual!T) digitalRoot(T)(in T inRoot, in uint base)
|
||||
pure nothrow
|
||||
in {
|
||||
assert(base > 1);
|
||||
} body {
|
||||
Unqual!T root = inRoot.abs;
|
||||
uint persistence = 0;
|
||||
while (root >= base) {
|
||||
auto num = root;
|
||||
root = 0;
|
||||
while (num != 0) {
|
||||
root += num % base;
|
||||
num /= base;
|
||||
}
|
||||
persistence++;
|
||||
}
|
||||
return typeof(return)(persistence, root);
|
||||
}
|
||||
|
||||
void main() {
|
||||
enum f1 = "%s(%d): additive persistance= %d, digital root= %d";
|
||||
foreach (immutable b; [2, 3, 8, 10, 16, 36]) {
|
||||
foreach (immutable n; [5, 627615, 39390, 588225, 393900588225])
|
||||
writefln(f1, text(n, b), b, n.digitalRoot(b)[]);
|
||||
writeln;
|
||||
}
|
||||
|
||||
enum f2 = "<BIG>(%d): additive persistance= %d, digital root= %d";
|
||||
immutable n = BigInt("581427189816730304036810394583022044713" ~
|
||||
"00738980834668522257090844071443085937");
|
||||
foreach (immutable b; [2, 3, 8, 10, 16, 36])
|
||||
writefln(f2, b, n.digitalRoot(b)[]); // Shortened output.
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue