Digital root: Difference between revisions
no edit summary
Jazzedpineda (talk | contribs) (Added EasyLang implementation) |
No edit summary |
||
(13 intermediate revisions by 10 users not shown) | |||
Line 933:
627615 2 9
</pre>
==={{header|BASIC256}}===
<syntaxhighlight lang="vb">global dr
global ap
dim a = {627615, 39390, 588225}
for i = 0 to a[?]-1
dr = digitalRoot(a[i])
print a[i], "Additive Persistence = "; ap, "Digital root = "; dr
next i
end
function digitalRoot(n)
ap = 0
do
dr = 0
while n > 0
dr += n mod 10
n = n \ 10
end while
ap += 1
n = dr
until dr < 10
return dr
end function</syntaxhighlight>
{{out}}
<pre>627615 Additive Persistence = 2 Digital root = 9
39390 Additive Persistence = 2 Digital root = 6
588225 Additive Persistence = 2 Digital root = 3</pre>
==={{header|Nascom BASIC}}===
Line 961 ⟶ 991:
</syntaxhighlight>
{{out}}
<pre> 1 0 1
14 1 5
267 2 6
Line 968 ⟶ 997:
39390 2 6
588225 2 3
627615 2 9</pre>
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">SUB digitalroot (what)
LET dr = ABS(what)
IF dr > 10 THEN
LET ap = 0
DO
LET ap = ap + 1
DO WHILE dr <> 0
LET t = t + REMAINDER(dr, 10)
LET dr = IP(dr / 10)
LOOP
LET dr = t
LET t = 0
LOOP WHILE dr > 9
END IF
PRINT what, "Additive persistance ="; ap, "Digital root ="; dr
END SUB
CALL digitalroot (627615)
CALL digitalroot (39390)
CALL digitalroot (588225)
CALL digitalroot (393900588225)
END</syntaxhighlight>
{{out}}
<pre>
627615 Additive persistence = 2 Digital root = 9
39390 Additive persistence = 2 Digital root = 6
588225 Additive persistence = 2 Digital root = 3
393900588225 Additive persistence = 2 Digital root = 9</pre>
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">dim a(2)
a(0) = 627615 : a(1) = 39390 : a(2) = 588225
for i = 0 to arraysize(a(),1)
dr = digitalRoot(a(i))
print a(i), "\tAdditive persistence = ", ap, "\tDigital root = ", dr
next i
end
sub digitalRoot(n)
ap = 0
repeat
dr = 0
while n > 0
dr = dr + mod(n, 10)
n = int(n / 10)
wend
ap = ap + 1
n = dr
until dr < 10
return dr
end sub</syntaxhighlight>
{{out}}
<pre>627615 Additive persistence = 2 Digital root = 9
39390 Additive persistence = 2 Digital root = 6
588225 Additive persistence = 2 Digital root = 3</pre>
=={{header|Batch File}}==
Line 1,683 ⟶ 1,768:
=={{header|EasyLang}}==
<syntaxhighlight lang="easylang">
numberString$ = n
currentPersist = 0
Line 1,699 ⟶ 1,784:
numbers[] = [ 627615 39390 588225 393900588225 ]
for i in numbers[]
print i
print "Additive persistence: " & persistence
Line 1,803 ⟶ 1,888:
=={{header|Elena}}==
{{trans|C#}}
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,817 ⟶ 1,902:
while (num > 9)
{
num := num.toPrintable().toArray().selectBy::(ch => ch.toInt() - 48).summarize(new LongInteger());
additivepersistence += 1
Line 1,828 ⟶ 1,913:
public program()
{
new long[]{627615l, 39390l, 588225l, 393900588225l}.forEach::(num)
{
var t := num.DigitalRoot;
Line 2,069 ⟶ 2,154:
=={{header|Fōrmulæ}}==
{{FormulaeEntry|page=https://formulae.org/?script=examples/Digital_root}}
'''Solution'''
[[File:Fōrmulæ - Digital root 01.png]]
'''Test cases'''
[[File:Fōrmulæ - Digital root 02.png]]
[[File:Fōrmulæ - Digital root 03.png]]
=={{header|Go}}==
Line 2,345 ⟶ 2,436:
=={{header|J}}==
<syntaxhighlight lang="j">digrt=: 10&$: :(|&.<:^:<:)"0
addps=: 10&$: :([: <:@# +/@(#.inv)^:a:)"0</syntaxhighlight>
With these functions, the base can be supplied as a left argument (dyadic). When being called monadically, they default to base 10.
Example use:
<syntaxhighlight lang="j"> (,. addps ,. digrt) 627615 39390 588225 393900588225
627615 2 9
39390 2 6
588225 2 3
393900588225 2 9
8 digrt 8b4321
3
8 addps 8b4321
2</syntaxhighlight>
Here's an equality operator for comparing these base 10 digital roots:
<syntaxhighlight lang="j">equals=: =&(9&|)"0</syntaxhighlight>
<syntaxhighlight lang="j"> equals table i. 10
┌──────┬───────────────────┐
Line 2,379 ⟶ 2,473:
└──────┴───────────────────┘</syntaxhighlight>
Note that these routines merely calculate results, which are numbers. If you want the result to be displayed in some other base, converting the result from numbers to character strings needs an additional step. Since that's currently not a part of the task, this is left as an exercise for the reader.
=={{header|Janet}}==
<syntaxhighlight lang="janet">
(defn numbers [s] (filter (fn [y] (and (<= y 9) (>= y 0))) (map (fn [z] (- z 48)) (string/bytes s))))
(defn summa [s] (reduce (fn [x y] (+ x y)) 0 (numbers s)))
(defn minsumma [x p]
(if (<= x 9)
[x p]
(minsumma (summa (string/format "%d" x)) (+ 1 p))))
(defn test [t] (printf "%j" (minsumma (summa t) 1)))
(test "627615")
(test "39390")
(test "588225")
(test "393900588225")
(test "19999999999999999999999999999999999999999999999999999999999999999999999999999999999999")
(test "192348-0347203478-20483298402-39482-04720348-20394823-058720375204820-394823842-049802-93482-034892-3")
</syntaxhighlight>
{{out}}
<pre>
(9 2)
(6 2)
(3 2)
(9 2)
(1 4)
(6 3)
</pre>
=={{header|Java}}==
;<nowiki>Code:</nowiki>
Line 2,676 ⟶ 2,783:
f
16</pre>
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">
/* Function that returns a list of digits given a nonnegative integer */
decompose(num) := block([digits, remainder],
digits: [],
while num > 0 do
(remainder: mod(num, 10),
digits: cons(remainder, digits),
num: floor(num/10)),
digits
)$
/* Function that given a positive integer returns the sum of their digits */
auxdig(n):=block(decompose(n),apply("+",%%));
/* Function that given a positive integer returns a list of two: the additive persistence and the digital root */
digrt(n):=block([additive_persistence:0,digital_root:n],
while length(decompose(digital_root))>1 do (digital_root:auxdig(digital_root),additive_persistence:additive_persistence+1),
[additive_persistence,digital_root]);
/* Examples */
digrt(627615);
digrt(39390);
digrt(588225);
digrt(393900588225);
</syntaxhighlight>
{{out}}
<pre>
[2,9]
[2,6]
[2,3]
[2,9]
</pre>
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">testNumbers = [627615, 39390, 588225, 393900588225, 45, 9991]
pad = function(n, width)
return (n + " " * width)[:width]
end function
getDigitalRoot = function(n)
persistance = 0
while floor(log(n)) > 0
sum = 0
while n > 0
sum += n % 10
n = floor(n / 10)
end while
n = sum
persistance += 1
end while
return [n, persistance]
end function
for num in testNumbers
digRoot = getDigitalRoot(num)
print pad(num, 12), ""
print " has a digital root ", ""
print digRoot[0], ""
print " and additive persistance ",""
print digRoot[1]
end for</syntaxhighlight>
{{out}}
<pre>627615 has a digital root 9 and additive persistance 2
39390 has a digital root 6 and additive persistance 2
588225 has a digital root 3 and additive persistance 2
393900588225 has a digital root 9 and additive persistance 2
45 has a digital root 9 and additive persistance 1
9991 has a digital root 1 and additive persistance 3
</pre>
=={{header|Modula-2}}==
Line 2,874 ⟶ 3,052:
588225 has additive persistence 2 and digital root of 3
393900588225 has additive persistence 2 and digital root of 9</pre>
=={{header|OCaml}}==
<syntaxhighlight lang="ocaml">let rec digit_sum b n =
if n < b then n else digit_sum b (n / b) + n mod b
let digital_root b n =
let rec loop a x =
if x < b then a, x else loop (succ a) (digit_sum b x)
in
loop 0 n
let () =
let pr_fmt n (p, r) =
Printf.printf "%u: additive persistence = %u, digital root = %u\n" n p r
in
List.iter
(fun n -> pr_fmt n (digital_root 10 n))
[627615; 39390; 588225; 393900588225]</syntaxhighlight>
{{out}}
<pre>
627615: additive persistence = 2, digital root = 9
39390: additive persistence = 2, digital root = 6
588225: additive persistence = 2, digital root = 3
393900588225: additive persistence = 2, digital root = 9
</pre>
=={{header|Oforth}}==
Line 3,983 ⟶ 4,186:
return s
</syntaxhighlight>
=={{header|RPL}}==
Base 10 only. For other bases, use <code>DIGITS</code> from [[Factors of an integer#RPL]] instead of <code>∑DGIT</code> below, which is more compact - but works only in base 10.
≪ 0 SWAP
'''DO''' 10 / LAST MOD ROT + RND SWAP FLOOR
'''UNTIL''' DUP NOT '''END''' DROP
≫ ''''∑DGIT'''' STO
≪ 0 '''WHILE''' OVER 9 > '''REPEAT'''
1 + SWAP '''∑DGIT''' SWAP '''END''' R→C
≫ ''''DROOT'''' STO
≪ { 627615 39390 588225 393900588225 55 } → cases
≪ {} 1 cases SIZE '''FOR''' j cases j GET '''DROOT''' + '''NEXT'''
≫ ≫ EVAL
{out}
<pre>
1: { (9,2) (6,2) (3,2) (9,2) (1,2) }
</pre>
=={{header|Ruby}}==
Line 4,110 ⟶ 4,332:
0xd60141 has digital root 0xa and additive persistance 0x2
0x12343210 has digital root 0x1 and additive persistance 0x2
</pre>
=={{header|S-BASIC}}==
We operate on the number as a string to avoid the limitations of S-BASIC's 16-bit integer type
<syntaxhighlight lang="BASIC">
rem - return the digital sum of n represented as a string
function digitalsum(nstr = string) = integer
var i, slen, sum = integer
var ch = char
slen = len(nstr)
sum = 0
for i = 1 to slen
ch = mid(nstr, i, 1)
rem - don't process leading or embedded spaces, etc.
if ch >= '0' and ch <= '9' then
sum = sum + (ch - '0')
next i
end = sum
var nstr = string
var droot, pers = integer
0again
rem - input1 does not advance to next line; control-C will exit
input1 "What number"; nstr
droot = digitalsum(nstr)
pers = 1
while droot > 9 do
begin
droot = digitalsum(str$(droot))
pers = pers + 1
end
print " digital root ="; droot; " persistence ="; pers
goto 0again
end
</syntaxhighlight>
{{out}}
Control-C at the prompt provides a quick and dirty exit
<pre>
What number ? 627615 digital root = 9 persistence = 2
What number ? 39390 digital root = 6 persistence = 2
What number ? 588225 digital root = 3 persistence = 2
What number ? 393900588225 digital root = 9 persistence = 2
What number ?
</pre>
Line 4,699 ⟶ 4,966:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="
var sumDigits = Fn.new { |n|
|