Digital root/Multiplicative digital root: Difference between revisions

→‎{{header|Python}}: Add faster implementation.
(+ 3 D versions)
(→‎{{header|Python}}: Add faster implementation.)
Line 155:
 
=={{header|Python}}==
===Python: Inspired by the solution to the [[Digital root#Python|Digital root]] task===
<lang python>try:
from functools import reduce
Line 201 ⟶ 202:
8: [8, 18, 24, 29, 36]
9: [9, 19, 33, 91, 119]</pre>
 
===Python: Inspired by the [[Digital_root/Multiplicative_digital_root#More_Efficient_Version|more efficient version of D]].===
Substitute the following function to run twice as fast when calculating mdroot(n) with n in range(1000000).
<lang python>def mdroot(n):
count, mdr = 0, n
while mdr > 9:
m, digitsMul = mdr, 1
while m:
m, md = divmod(m, 10)
digitsMul *= md
mdr = digitsMul
count += 1
return count, mdr</lang>
 
{{out}}
(Exactly the same as before).
Anonymous user