Digital root/Multiplicative digital root: Difference between revisions

m
→‎idomatic version: added/changed comments and whitespace, optimized a function, used a template for the output.
No edit summary
m (→‎idomatic version: added/changed comments and whitespace, optimized a function, used a template for the output.)
Line 2,161:
=={{header|REXX}}==
===idomatic version===
<lang rexx>/*REXX pgmprogram finds the persistence and multiplicative digital root of some #'snumbers.*/
numeric digits 100 /*increase the number of digits.decimal digits*/
parse arg x /*getobtain someoptional numbersarguments from the C.L. CL*/
if x='' | x="," then x=123321 7739 893 899998 /*useNot defaultsspecified? if noneThen specifieduse the default.*/
say center('number', 8) ' persistence multiplicative digital root'
say copies('─' , 8) ' ─────────── ───────────────────────────'
/* [↑] the title and separator. */
do j=1 for words(x); n=word(x, j) /*process each number in the X list.*/
parse value mdr MDR(n) with mp mdr /*obtain the persistence and the MDR. */
say right(n,8) center(mp,13) center(mdr,30) /*display #a number, mp persistence, mdr MDR.*/
end /*j*/ /* [↑] show MP and& MDR for each #number. */
say; target=5
say 'MDR first ' target " numbers that have a matching MDR"
say '═══ ═══════════════════════════════════════════════════'
 
do k=0 for 10; hits=0; _= /*show #'s that have an MDR of K.*/
do m=k=0 untilfor 10; hits=0; _=target /*findshow targetnumbers #sthat withhave an MDR of K. */
ifdo word(mdr(m),2)\==k thenuntil hits==target iterate /*isfind thetarget MDRnumbers what'swith an MDR wanted?of K.*/
hits=hits+1; if _=spaceword(_ MDR(m'),' 2)\==k then iterate /*yes,is wethis gotthe MDR that's wanted? a hit, add to list.*/
end /*m*/ hits=hits + 1; _=space(_ m',') /*yes, [↑]we builtgot a listhit, of MDRsadd =to kthe list. */
say " "k':end /*m*/ ['strip(_,,',')"]" /*display the[↑] Kbuilt a (mdr)list andof listMDRs that = K. */
endsay " /*"k*/': ['strip(_, , ',')"]" /*display the K /* [↑] (MDR) done withand the K mdr list. */
exit end /*k*/ /*stick a[↑] fork indone it,with we'rethe done K MDR list. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────MDR subroutine──────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
mdr: procedure; parse arg y; y=abs(y) /*get the number and find the MDR*/
MDR: procedure; parse do p=1 untilarg y; y<10=abs(y) /*get the number and determine /*findthe multiplicative digRoot (Y)MDR.*/
parse var y 1 r 2; do kp=21 to length(y); r=r*substr( until y,k,1)<10; end; parse var y= r 2
end /*p*/ do k=2 to length(y); r=r /*wash substr(y, rinsek, repeat ··· */1)
return p r end /*return the persistence and MDR.k*/</lang>
y=r
'''output''' &nbsp; when using the default inputs:
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
return p r /*return the persistence and the MDR. */</lang>
'''{{out|output''' |text=&nbsp; when using the default inputs:}}
<pre>
number persistence multiplicative digital root