Advertisement
HasteBin0

Multiplicative Persistence Comment

Mar 21st, 2019
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.89 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from functools import reduce as rd
  3.  
  4.  
  5. # a comment on https://youtu.be/Wim9WJeDTHQ
  6.  
  7. def mul(a: int, b: int) -> int:
  8.     return a * b
  9.  
  10.  
  11. digits = lambda _t: (int(_i) for _i in str(_t))
  12.  
  13.  
  14. def multiplicative_persistence(x: int, show: bool = True) -> int:
  15.     tmp: int = x
  16.     count: int = 1
  17.     while True:
  18.         tmp_exp: [int] = tuple(digits(tmp))
  19.         if show:
  20.             print(f'#{count} is {tmp} (length {len(tmp_exp)})')
  21.         if len(tmp_exp) == 1:
  22.             break
  23.         try:
  24.             tmp = rd(mul, tmp_exp)
  25.         except OverflowError:
  26.             if show:
  27.                 print('Crash')
  28.                 break
  29.             else:
  30.                 raise
  31.         count += 1
  32.     return count
  33.  
  34.  
  35. mp = multiplicative_persistence
  36.  
  37.  
  38. def dsum(_d: [int]) -> int:
  39.     l: int = len(_d) - 1
  40.     return sum(v * 10 ** (l - i) for i, v in enumerate(_d))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement