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

  • Home
  • SEARCH
  • 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 6876921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:32:17+00:00 2026-05-27T04:32:17+00:00

This question came up when I was thinking about the algorithm to fast compute

  • 0

This question came up when I was thinking about the algorithm to fast compute the power of a number, say compute x^n.

In Java, the recursive part is something like:

return power(x,n/2) * power(x,n/2);

So after the program has returned the value of power(x,n/2), will it go through the whole recursive process again to compute the value for the second power(x,n/2)?

If so, should we store the value in some variable first, then return the square of that value?

  • 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-27T04:32:18+00:00Added an answer on May 27, 2026 at 4:32 am

    What you have in mind is called memoization: A pure function (i.e. a function whose return value depends only on the argument values) can remember the result for arguments that it has previously encountered.

    Memoization is performed by some high-level special-purpose languages like Maple. However, general-purpose languages don’t have this (rather complex) behaviour by default.

    However, in your case this isn’t necessary. Rather than using recursion, your algorithm is better implemented as iteration. Binary exponentiation by repeated squaring is the standard algorithm.

    Here’s some C-like pseudo-code (my Java isn’t 100%):

    // compute pow(base, exponent)
    
    int result = 1;
    int term = base;
    
    while (exponent != 0)
    {
      if (exponent % 2 != 0) { result *= term; }
      term = term * term;
      exponent /= 2;
    }
    
    return result;
    

    For some examples, 7 is 111 in binary, so b7 = b1 × b2 × b4, and we only need to keep track of one copy of the running term. Another one: 5 = 101b, so b5 = b1 × 1 × b4.

    In C++ it’s quite easy to build a generic memoizing wrapper for any function R f(T1, T2, ..., TN) that stores the memory in a hash map std::unordered_map<std::tuple<T1, ..., TN>, R>; upon invocation the wrapper checks if the argument-tuple already exists, and if yes, it returns the value from the map, and if no it performs the computation and inserts the result into the map. I’m sure something similar can be rigged up in Java.

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

Sidebar

Related Questions

While thinking about this question and conversing with the participants, the idea came up
This question came about because the cells gem specifies template directories using File.join('app','cells'). That
let me tell you a bit about where this question came from. I have
I just came across this question about initializing local variables. Many of the answers
I was thinking about this problem to myself previously, and came up with the
I was playing around the ternary operator in Java when suddenly this question came
This question came to my mind when I learned C++ with a background of
This question came today in the manipulatr mailing list. http://groups.google.com/group/manipulatr/browse_thread/thread/fbab76945f7cba3f I am rephrasing. Given
Problem This question actually came up at work today. We are planning an experiment
Today I came across this question: you have a code static int counter =

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.