June 2018 Update

This commit is contained in:
Ingy döt Net 2018-06-22 20:57:24 +00:00
parent ba8067c3b7
commit 22f33d4004
5278 changed files with 84726 additions and 14379 deletions

View file

@ -0,0 +1,30 @@
import extensions.
import system'routines.
extension $op
{
digitalRoot
[
int additivepersistence := 0.
long num := self.
while (num > 9)
[
num := num literal; toArray; selectBy(:ch)(ch toInt - 48); summarize(LongInteger new).
additivepersistence += 1.
].
^ { item1 = additivepersistence. item2 = num toInt. }.
]
}
program =
[
(627615l, 39390l, 588225l, 393900588225l) forEach(:num)
[
var t := num digitalRoot.
console printLineFormatted("{0} has additive persistence {1} and digital root {2}", num, t item1, t item2).
]
].

View file

@ -0,0 +1,13 @@
USING: arrays formatting kernel math math.text.utils sequences ;
IN: rosetta-code.digital-root
: digital-root ( n -- persistence root )
0 swap [ 1 digit-groups dup length 1 > ] [ sum [ 1 + ] dip ]
while first ;
: print-root ( n -- )
dup digital-root
"%-12d has additive persistence %d and digital root %d.\n"
printf ;
{ 627615 39390 588225 393900588225 } [ print-root ] each

View file

@ -1,15 +1,14 @@
function digitalroot{S<:Integer,T<:Integer}(n::S, bs::T=10)
-1 < n && 1 < bs || throw(DomainError())
ds = n
pers = 0
while bs <= ds
function digitalroot(n::Integer, bs::Integer=10)
if n < 0 || bs < 2 throw(DomainError()) end
ds, pers = n, 0
while bs ≤ ds
ds = sum(digits(ds, bs))
pers += 1
end
return (pers, ds)
return pers, ds
end
for i in {627615, 39390, 588225, 393900588225, big(2)^100}
(pers, ds) = digitalroot(i)
for i in [627615, 39390, 588225, 393900588225, big(2) ^ 100]
pers, ds = digitalroot(i)
println(i, " has persistence ", pers, " and digital root ", ds)
end

View file

@ -0,0 +1,51 @@
MODULE DigitalRoot;
FROM FormatString IMPORT FormatString;
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
TYPE Root =
RECORD
persistence,root : LONGINT;
END;
PROCEDURE digitalRoot(inRoot,base : LONGINT) : Root;
VAR root,persistence,num : LONGINT;
BEGIN
root := ABS(inRoot);
persistence := 0;
WHILE root>=base DO
num := root;
root := 0;
WHILE num#0 DO
root := root + (num MOD base);
num := num DIV base;
END;
INC(persistence)
END;
RETURN Root{persistence, root}
END digitalRoot;
PROCEDURE Print(n,b : LONGINT);
VAR
buf : ARRAY[0..63] OF CHAR;
r : Root;
BEGIN
r := digitalRoot(n,b);
FormatString("%u (base %u): persistence=%u, digital root=%u\n", buf, n, b, r.persistence, r.root);
WriteString(buf)
END Print;
VAR
buf : ARRAY[0..63] OF CHAR;
b,n : LONGINT;
r : Root;
BEGIN
Print(1,10);
Print(14,10);
Print(267,10);
Print(8128,10);
Print(39390,10);
Print(627615,10);
Print(588225,10);
ReadChar
END DigitalRoot.

View file

@ -0,0 +1,15 @@
#!/usr/bin/env bash
numbers=(627615 39390 588225 393900588225 55)
declare root
for number in "${numbers[@]}"; do
declare -i iterations
root="${number}"
while [[ "${#root}" -ne 1 ]]; do
root="$(( $(fold -w1 <<<"${root}" | xargs | sed 's/ /+/g') ))"
iterations+=1
done
echo -e "${number} has additive persistence ${iterations} and digital root ${root}"
unset iterations
done | column -t