Recursively find sum of digits
Easy Accuracy: 0.0% Submissions: 2 Points: 2

Gievn two numbers A and B, the task is to find f(AB). f(n) which takes a positive integer n as input and does the following:


if n < 10

    return n

else return f( sum_of_digits(n) )


Example 1:

Input: A = 6, B = 6
Output: 9
f(66) = f(46656) = f(27) = f(9) = 9

Example 2:

Input: A = 7, B = 3
Output: 1
f(73) = f(343) = f(10) = f(1) = 1


Your Task:
You don't need to read or print anything. Your task is to complete the function SumofDigits() which takes A and B as input parameter and returns f(AB).


Expected Time Complexity: O(Log(B))
Expected Space Complexity: O(1)


1 <= A, B <= 109 

to report an issue on this page.


We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?


All Submissions

My Submissions:

Login to access your submissions.