Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7432377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:26:52+00:00 2026-05-29T09:26:52+00:00

Have a look at the following code to find X^y. /* Find exponent in

  • 0

Have a look at the following code to find X^y.

/* Find exponent in logarithmic complexity */
int findPower(int base, exponent){
     if( 1 == exponent ) return base;
     return (findPower(base, exponent/2)*findPower(base, exponent-exponent/2));
}

int main(int argc, char *argv[]){
    if(argc < 3) {
        printf("Usage :: logpow baseNumber power\n");
        return -1;
    }
    printf("%s ^ %s  =  %d\n", argv[1], argv[2], findPow( atoi(argv[1]),
                                                          atoi(argv[2])) );
    return 0;
}

Analysis shows that this has a complexity of theta(log(n)).
But I ran it to measure time, and here are the results

Run 1: (calculating 1^500_million)
user-lm Programming # time ./a.out 1 500000000
1 ^ 500000000  =  1

real    0m5.009s
user    0m5.000s
sys 0m0.000s


Run 2: (calculating 1^1_Billion)
user-lm Programming # time ./a.out 1 1000000000
1 ^ 1000000000  =  1

real    0m9.667s
user    0m9.640s
sys 0m0.000s


Run 3: (calculating 1^2_Billion)
user-lm Programming # time ./a.out 1 2000000000
1 ^ 2000000000  =  1

real    0m18.649s
user    0m18.630s
sys 0m0.000s

From above we can see that the actual time complexity has linear behaviour rather than logarithmic!

What could be the reason for such a huge difference in complexity?

Regards,

Microkernel

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T09:26:53+00:00Added an answer on May 29, 2026 at 9:26 am

    You are actually invoking 2 function calls from each call. The recursion tree would be a binary tree of height log(exponent), so the number of nodes in it will be 2^log(exponent) == exponent. So overall it becomes a linear algorithm. You can rewrite it like this for better performance:

    int findPower(int base, int exponent){
        if( 0 == exponent ) return 1;
        int temp = findPower(base, exponent/2);
        if(exponent % 2 == 0) return temp * temp;
        return temp * temp * base;
    }
    

    The trick is, you have to store the value of findPower(base, exponent/2) to get the logarithmic complexity. The recursion tree still has height log(exponent) but each node has only one child now, so there would be log(exponent) nodes. If you actually call it twice it will degrade the performance even more than a linear one. There’s no need to calculate the same value second time if you already have that!

    As @David Schwartz pointed out, the number of calls made in your code would be doubled if exponent is doubled. But when you save the values, doubling the exponent make only one more call.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have a look at the following code you are not required to read the
Have a look here: In the following code, what would be the type of
am new here. i have a slight problem; PLease look at the following code
Please have look on the following code: $_SESSION[process_y] = new Process(); $process_y = $_SESSION[process_y];
Have a look at the following code: class A(object): defaults = {'a': 1} def
I have been trying to look for a reason why the following code is
I have the following code: #include <iostream> #include <string> void main() { std::string str;
I have the following code to access a HTML table. my $table = $tree->look_down(_tag
In my current project, Workflows have comments. Please have a look at the following
I have a data source with about 2000 lines that look like the following:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.