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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:17:39+00:00 2026-05-22T01:17:39+00:00

Here is what I have for my solution: public int powerN(int base, int n)

  • 0

Here is what I have for my solution:

 public int powerN(int base, int n) {

    if(n == 0)
       return 1;

    else if(n % 2 == 0)
       return base * base;

    else
       return base * powerN(base, n-1); 

 }

However, if n > 3 then this function doesn’t work.
For instance, powerN(2, 4) yields 4 and powerN(2, 5) yields 8.
I know that a much simpler solution exists, but it just bothers me that I can’t figure out why this is not working correctly.

  • 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-22T01:17:40+00:00Added an answer on May 22, 2026 at 1:17 am

    Into pseudocode

    Let me translate your code into pseudocode:

    public int powerN(int base, int exponent)  {
        if the exponent is 0
            then return 1
        otherwise, if the exponent is even
            then return base * base
        otherwise
            base * powerN(base, exponent - 1)
    }
    

    The second branch has a logic error. What your code is saying is this: “As long as the exponent is even, the result should be base * base (that is, base squared)”. You’ve already mentioned that this is the result you get when you run your code.

    How to solve it

    What you probably want to do is to raise base to half the exponent (base * base * base * ... for exponent / 2 times), and then multiply that number by itself. That way, you get base multiplied by itself exponent times.

    In pseudocode:

    otherwise, if the exponent is even
        then return powerN(base, exponent / 2) * powerN(base, exponent / 2)
    

    Realistically, this would actually be the following:

    otherwise, if the exponent is even
        then {
            let x = powerN(base, exponent / 2)
            return x * x
        }
    

    Done. Mostly.

    Translate that back to Java and you’ll be set.

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

Sidebar

Related Questions

I have a problem here and no have ideia to solution this. I have
gem install heroku failed with following message and I have tried the solution here
I have searched the web and have found no definitive solution, so here goes:
There might be an easy solution here, but it seems to have me tripped
Lets say I have the following code: public class Base { // Some stuff
found this puzzle HERE ... I made a brute force solution and I would
Here is my object [Serializable()] public class PersistentObject { public virtual int ID {
I have a classic Order -> Order Row scenario something like this: public class
I have this code up on my server here (Yes I known ASMX is
Here is my scenario. I have an ASP.NET MVC solution with 3 projects. Data,

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.