0

In a challenge i have to find the sum of digits of one number, like: n = 123 the result should be 1 + 2 + 3 = 6 and if i pass a k to the function, the n should be the concatanation de n * k, like: n = 132, k = 3, n = n * k (123123123), and result should be: 1 + 2 + 3 + 1 + 2 + 3 + 1 + 2 + 3.

some of research i think to use is this function:

sum = 0
while x > 0:
    rest = n % 10
    n = n // 10
    sum = sum + rest

but i think there is a better way to do that. U guys please can help me, please. Thanks :)

  • Why do you think there is a better way? What's wrong with your solution? Are you looking for a more readable solution? A more efficient one? (Or one of many other things "better" can mean) – amit Jun 10 '20 at 13:52
  • i looking for a more efficient one since the string should be gigant, n should be 1<=n<=10ˆ1000000 and k should be 1<=k<=10ˆ5 – Vinícius Santos Jun 10 '20 at 14:00

0 Answers0