Digital root: Difference between revisions

Content deleted Content added
→‎Tcl: Added implementation
Eriksiers (talk | contribs)
added qbasic w/ "improve" tag
Line 9:
 
Ref 1 identifies the relationship of this task to casting out nines. See: http://rosettacode.org/wiki/Casting_out_nines for this wiki's use of this procedure.
 
=={{header|BASIC}}==
{{works with|QBasic}}
{{improve}}
 
This calculates the result "the hard way", but is limited to the limits of a 32-bit signed integer (+/-2,147,483,647) and therefore can't calculate the digital root of 393,900,588,225.
 
<lang qbasic>DECLARE SUB digitalRoot (what AS LONG)
 
'test inputs:
digitalRoot 627615
digitalRoot 39390
digitalRoot 588225
 
SUB digitalRoot (what AS LONG)
DIM w AS LONG, t AS LONG, c AS INTEGER
 
w = ABS(what)
IF w > 10 THEN
DO
c = c + 1
WHILE w
t = t + (w MOD (10))
w = w \ 10
WEND
w = t
t = 0
LOOP WHILE w > 9
END IF
PRINT what; ": additive persistance "; c; ", digital root "; w
END SUB</lang>
 
Output:
627615 : additive persistance 2 , digital root 9
39390 : additive persistance 2 , digital root 6
588225 : additive persistance 2 , digital root 3
 
=={{header|J}}==