Anonymous user
Digital root/Multiplicative digital root: Difference between revisions
Digital root/Multiplicative digital root (view source)
Revision as of 14:04, 15 September 2017
, 6 years ago→ultra-fast version: added/changed comments, whitespace, and indentations, optimized a function, used a template for output.
m (→idomatic version: added/changed comments and whitespace, optimized a function, used a template for the output.) |
m (→ultra-fast version: added/changed comments, whitespace, and indentations, optimized a function, used a template for output.) |
||
Line 2,217:
===ultra-fast version===
This fast version can handle a target of five hundred numbers with ease for the 2<sup>nd</sup> part of the task's requirement.
<lang rexx>/*REXX
numeric digits 2000 /*increase the number of
parse arg target x
if
say center('number',8) ' persistence multiplicative digital root'
say copies('─' ,8) ' ─────────── ───────────────────────────'
/* [↑] the title and the separator.
do j=1 for words(x); n=abs( word(x, j) )
parse value
say right(n,8) center(mp,13) center(mdr,30)
end /*j*/ /* [↑] show MP and MDR for each
say /* [↓] show a blank
say 'MDR first ' target " numbers that have a matching MDR"
say '═══ ' copies("═",(target+(target+1)**2)%2)
_=
else do m=k until hits==target /*find target numbers with an MDR of K.*/
if k==1 then m=copies(1, hits+1)▼
else if MDR(m, 1)\==k then iterate
if k==3 then do; o=strip(m, 'T', 1) /*strip trailing ones from M*/
say " "k': ['_"]"
if k==3 then
end
▲ if k==5 then if ?//2==0 then iterate
▲ if k==1 then m=copies(1,hits+1)
▲ else if mdr(m,1)\==k then iterate
▲ hits=hits+1; _=space(_ m) /*yes, we got a hit, add to list.*/
@.=
_=translate(@7, 9, 7)
@9=translate(_, , ',')
@3=
▲ end /* [↑] shift the "3" 1 place left*/
▲ m=m-1 /*adjust for DO index advancement*/
▲ end /* [↑] a shortcut to do DO index*/
▲ end /*m*/ /* [↑] built a list of MDRs = k */
do j=1 for words(@9)
L=length(_) + 1
@9=translate(_,,',') /*remove trailing commas from #'s*/▼
▲@3= /*assine null string before build*/
L=length(_)+1 /*use a "fudged" length of the #.*/▼
▲ new= /*this is the new numbers so far.*/
@.z=z; new=z new /*define it, and then add to list*/▼
▲ end /*i*/ /* [↑] end of 2nd insertion of 3*/
▲ end /*k*/ /* [↑] " " 1st " " "*/
▲ end /*j*/ /* [↑] end of insertion of "3"s.*/
do i=k to L; z=insert(3, q, i) /* " " 2nd "3"
x=word(a1,1); y=word(a2,1); if x=='' | y=='' then leave /*empty?*/▼
end /*k*/
end /*while ···*/ /* [+] only process just 'nuff. */▼
@3=
▲exit /*stick a fork in it, we're done.*/
▲ x=word(a1, 1); y=word(a2, 1)
parse var y 1 r 2; do k=2 to length(y); r=r*substr(y,k,1); end; y=r▼
;;;output'''  ' when the using the input of: <tt> 34 </tt>▼
say " "9': ['@"]" /*display the "9" (MDR) and the list.*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
MDR: procedure; parse arg y,s; y=abs(y) /*get the number and determine the MDR.*/
do p=1 until y<10; parse var y r 2
end /*k*/
y=r
end /*p*/ /* [↑] wash, rinse, and repeat ··· */
if s==1 then return r /*return multiplicative digital root. */
return p r /*return the persistence and the MDR. */</lang>
<pre>
number persistence multiplicative digital root
|