Home > database >  How do I calculate the T(n) of this recursive function?
How do I calculate the T(n) of this recursive function?

Time:04-05

My recursion code:

int sum_of_digit(int n){
       if(n == 0)
          return 0;
     return (n % 10   sum_of_digit(n/10) )  
    
    }

Note : here T(n) is the recurrence time

CodePudding user response:

You correctly computed that

T(n) = T(n/10)   O(1)

This means that the number of recursions will be such that:

floor(n / (10^k)) = 0 
log10(n) - k = 0
k = log10(n) 

Therefore the time complexity will be O(log10(n))

  • Related