Digital root: Difference between revisions
Content deleted Content added
Updated D entry |
Added Forth version |
||
Line 320:
</lang>
=={{header|Forth}}==
This is trivial to do in Forth, because radix control is one of its most prominent feature. The 32-bits version just takes two lines:
<lang forth>: (Sdigit) 0 swap begin base @ /mod >r + r> dup 0= until drop ;
: digiroot 0 swap begin (Sdigit) >r 1+ r> dup base @ < until ;</lang>
This will take care of most numbers:
<pre>
627615 digiroot . . 9 2 ok
39390 digiroot . . 6 2 ok
588225 digiroot . . 3 2 ok
</pre>
For the last one we will need a "double number" version. '''MU/MOD''' is not available in some Forth implementations, but it is easy to define:
<lang forth>[UNDEFINED] mu/mod [IF] : mu/mod >r 0 r@ um/mod r> swap >r um/mod r> ; [THEN]
: (Sdigit) 0. 2swap begin base @ mu/mod 2>r s>d d+ 2r> 2dup d0= until 2drop ;
: digiroot 0 -rot begin (Sdigit) 2>r 1+ 2r> 2dup base @ s>d d< until d>s ;</lang>
That one will take care of the last one:
<pre>
393900588225. digiroot . . 9 2 ok
</pre>
=={{header|Go}}==
Using package from task "Sum digits of an integer."
Line 378 ⟶ 397:
fmt.Println("all tests passed")
}</lang>
=={{header|Haskell}}==
|