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:

f(n):

if n < 10

    return n

else return f( sum_of_digits(n) )

 

Example 1:

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

Example 2:

Input: A = 7, B = 3
Output: 1
Explanation: 
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)

 

Constraints:
1 <= A, B <= 109 

to report an issue on this page.

Editorial

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

Yes

All Submissions

My Submissions:

Login to access your submissions.