Can anybody explain to me the time complexity of the following code:
cin >> n;
while(n>9)
{
int num = n;
int s = 0;
while(num!=0)
{
s = s + num%10;
num = num/10;
}
n = s;
}
cout<<n<<endl;
The above code calculates the sum of the digits of the number until the sum becomes a single digit number.
Example: 45859
= 4+5+8+5+9
= 31
= 3+1
= 4
Edit: I think that the inner loop calculating the sum of digits has O(log_base_10(n)) complexity, but the outer loop continues till the sum obtained so far is less than 10. So the total complexity depends on how many times the outer loop is going to run.. I am not able to figure that out... Some kind of mathematical gimmicks to calculate the complexity of the outer loop would help!!!