Q2 double sumexponentialsint n n is a power of 3 ie n3k or k
Q2.
 double sum_exponentials(int n)
 //n is a power of 3, i.e., n=3k or k=log3n
 {
 int sum=0;
 for (int i=1; i<n; i=i*3)
 sum = sum + i;
 return sum;
 }
Solution
Explanation:
 Time Complexity of a loop is considered as O(Logn) if the loop variables is divided / multiplied by a constant amount.
for (int i = 1; i <=n; i *= c) {
 // some O(1) expressions
 }
 for (int i = n; i > 0; i /= c) {
 // some O(1) expressions
 }
So, your function is:
double sum_exponentials(int n)
 //n is a power of 3, i.e., n=3k or k=log3n
 {
    int sum=0; // O(1) time to run this line
    for (int i=1; i<n; i=i*3) //it iterates logn times where base of log function is 3
        sum = sum + i; // this takes O(1) times , logn (base 3) times it executes
    return sum; // O(1)
 }
Total : O(1) + logn (base 3) + O(1)
 = O(logn)

