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 7077955
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T06:29:13+00:00 2026-05-28T06:29:13+00:00

So I’m trying to implement a more efficient method of calculating 2^n . I

  • 0

So I’m trying to implement a more efficient method of calculating 2^n.

I know that you can split it up so that it is O(logn) and it is easy to do using recursion. You keep dividing by 2 and multiply it by the lower power when its odd (or something like that). The problem is I wrote out my multiplication method by hand since its for big numbers. So it needs to return more than one parameter.

One solution I can think of is to make a pair which contains all the needed information. Other than that though I was trying to figure out how to write it using iteration. The only way I can see of doing is using some kind of data structure and then loop through dividing n by 2 and storing the value when n is odd. Then write a for loop and check at each iteration if the value is contained in the data structure. This seems to me like a relatively costly operation.

Is it possible that it would end up less efficient than the recursive version?

I’m doing this because:

  1. I can’t get gnp working.
  2. I think I am learning from writing the big numbers class and working with it.
  • 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-28T06:29:14+00:00Added an answer on May 28, 2026 at 6:29 am

    If you’re going to work with big numbers, rather than reinventing the wheel, you probably should take a look at the GNU MP Bignum Library.

    Regarding the recursion versus iteration question, the answer is that you can always write them to be equivalent; a recursive function that only calls itself as a tail call is as efficient as a while loop (provided that your compiler supports tail call optimization, but the most common compilers do). For instance, the tail-recursive version of the fast exponentiation function you are describing is (in pseudo-code):

    function fastExp(base, exponent, accumulator) {
      if(exponent == 0) {
        return accumulator;
      } else if(exponent % 2 == 0) {
        return fastExp(base * base, exponent/2, accumulator);
      } else {
        return fastExp(base, exponent-1, base * accumulator); 
      }
    }
    

    Think of this recursive function as a loop, where the looping condition is exponent != 0, and the recursive calls are like gotos to the beginning of the loop. (You need to call it with accumulator = 1 when you start, by the way.) It is equivalent to the following:

    function fastExp(base, exponent) {
      var accumulator = 1;
      while(exponent != 0) {
        if(exponent % 2 == 0) {
          base *= base;
          exponent /= 2;
        } else {
          exponent -= 1;
          accumulator *= base;
        }
      }
      return accumulator;
    }
    

    So you can see that they are equivalent, and therefore will perform the same number of operations.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into

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.