RosettaCodeData/Task/Digital-root/NetRexx/digital-root.netrexx

35 lines
1.5 KiB
Plaintext

/* NetRexx ************************************************************
* Test digroot
**********************************************************************/
Say 'number -> digital_root persistence'
test_digroot(7 ,7, 0)
test_digroot(627615 ,9, 2)
test_digroot(39390 ,6, 2)
test_digroot(588225 ,3, 2)
test_digroot(393900588225,9, 2)
test_digroot(393900588225,9, 3) /* test error case */
method test_digroot(n,dx,px) static
res=digroot(n)
Parse res d p
If d=dx & p=px Then tag='ok'
Else tag='expected:' dx px
Say n '->' d p tag
method digroot(n) static
/**********************************************************************
* Compute the digital root and persistence of the given decimal number
* 19.08.2012 Walter Pachl derived from Rexx
**************************** Bottom of Data **************************/
p=0 /* persistence */
Loop While n.length()>1 /* more than one digit in n */
s=0 /* initialize sum */
p=p+1 /* increment persistence */
Loop while n<>'' /* as long as there are digits */
Parse n c +1 n /* pick the first one */
s=s+c /* add to the new sum */
End
n=s /* the 'new' number */
End
return n p /* return root and persistence */