Programmin-in-Python

Repeated Sum of Digits after Exponentiation

Dec 18th, 2020
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.46 KB | None | 0 0
  1. def expo(a, b):
  2.     res = a**b
  3.     str_res = f"{a}^{b} = {res} => "
  4.     L1 = list(str(res))
  5.  
  6.     while len(L1) != 1:
  7.         sumd = 0
  8.         for i in range(len(L1)):
  9.             if i == 0:
  10.                 str_res += f"{L1[i]}"
  11.             else:
  12.                 str_res += f"+{L1[i]}"
  13.  
  14.             sumd += int(L1[i])
  15.         else:
  16.             str_res += " = "       
  17.         L1 = list(str(sumd))
  18.  
  19.         if len(L1) == 1:
  20.             str_res += f"{sumd}"
  21.             break
  22.         else:
  23.             str_res += f"{sumd} => "
  24.  
  25.     print(str_res)
  26.  
  27. # Call the Function with 2 arguments
  28. expo(8,8)
Advertisement
Add Comment
Please, Sign In to add comment