Problem statement

https://binarysearch.com/problems/Sum-of-the-Digits/

Solution

If strings not allowed than divide number by 10 and get last digit until it is possible.

Complexity

It is O(log n) for time and O(1) for space.

Code

class Solution:
    def solve(self, num):
        ans = 0
        while num:
            num, last = divmod(num, 10)
            ans += last
        return ans