Digital root: Difference between revisions

no edit summary
No edit summary
 
(6 intermediate revisions by 6 users not shown)
Line 1,888:
=={{header|Elena}}==
{{trans|C#}}
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'routines;
Line 1,902:
while (num > 9)
{
num := num.toPrintable().toArray().selectBy::(ch => ch.toInt() - 48).summarize(new LongInteger());
additivepersistence += 1
Line 1,913:
public program()
{
new long[]{627615l, 39390l, 588225l, 393900588225l}.forEach::(num)
{
var t := num.DigitalRoot;
Line 2,436:
 
=={{header|J}}==
<syntaxhighlight lang="j">digrt=: 10&$: :(|&.<:^:<:)"0
 
addps=: 10&$: :([: <:@# +/@(#.inv)^:a:)"0</syntaxhighlight>
<syntaxhighlight lang="j">digrot=: +/@(#.inv~&10)^:_
With these functions, the base can be supplied as a left argument (dyadic). When being called monadically, they default to base 10.
addper=: _1 + [: # +/@(#.inv~&10)^:a:</syntaxhighlight>
 
Example use:
<syntaxhighlight lang="j"> (,. addps ,. digrt) 627615 39390 588225 393900588225
 
<syntaxhighlight lang="j"> (, addper, digrot)&> 627615 39390 588225 393900588225
627615 2 9
39390 2 6
588225 2 3
393900588225 2 9</syntaxhighlight>
 
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>
 
tableTable of results:
 
<syntaxhighlight lang="j"> equals table i. 10
┌──────┬───────────────────┐
Line 2,470 ⟶ 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.
If digital roots other than 10 are desired, the modifier ~&10 can be removed from the above definitions of <code>digrot</code> and <code>addper</code>, and the base can be supplied as a left argument. Since this is a simplification, these definitions are shown here:
 
<syntaxhighlight lang="j">digrt=: +/@(#.inv)^:_
addpr=: _1 + [: # +/@(#.inv)^:a:</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.
 
Example use (note: names spelled slightly different for the updated definitions):
 
<syntaxhighlight lang="j"> 10 digrt 627615
9
10 addpr 627615
2</syntaxhighlight>
 
=={{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,767 ⟶ 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 4,245 ⟶ 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,834 ⟶ 4,966:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var sumDigits = Fn.new { |n|
5

edits