The Artima Developer Community
Sponsored Link

Java Answers Forum
dealing with powers

4 replies on 1 page. Most recent reply: May 7, 2003 11:06 PM by Singh M.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
jake

Posts: 83
Nickname: onorok
Registered: May, 2002

dealing with powers Posted: May 7, 2003 5:10 PM
Reply to this message Reply
Advertisement
I have to write a function that takes two parameters x,y. y is the power and x is the number, I am not asking for you to write my homework for me, but can someone help me out here is what I have so far, it works if both the numbers are even. But... the odd numbers are the bitch.
thanks,
jake.

public class PowerUp
{
	
	public int Power(int x, int y)
	{
		if(y%2==0 && x%2==0)//if both numbers are even.
		{
			while(y!=1)
			{
				x=x*x;
				y=y/2;
			}
		}
		
		else//if they are both odd.
		{
			x=x*x;
			
				
		}
		return x;
		
	}
	
	public static void main(String[] args)
	{
		PowerUp newP=new PowerUp();
		System.out.println("This is the answer: "+newP.Power(8,4));
	}
}


Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: dealing with powers Posted: May 7, 2003 5:59 PM
Reply to this message Reply
public class PowerUp
{
    
    public int Power(int x, int y)
    {
       int result = 1;
        
        for (int i = 0; i < y; i++){
            result = result * x;
        }
        return result;
        
    }
    
    public static void main(String[] args)
    {
        PowerUp newP=new PowerUp();
        System.out.println("This is the answer: "+newP.Power(8,4));
        System.out.println("This is the answer: "+newP.Power(3,3));
    }
}

Victor VanAlter

Posts: 4
Nickname: cadetvva
Registered: May, 2003

Re: dealing with powers Posted: May 7, 2003 6:03 PM
Reply to this message Reply
Jake

I am not sure why you are determining whether the number is even or odd, as calculating the power would not require this.

x ^ y (x to the power of y) requires that you multiply x
y number of times, adding the result to each new multiplication. I would suggest putting this in a for next loop or a while loop.

I hope this will help.

VVA

jake

Posts: 83
Nickname: onorok
Registered: May, 2002

Re: dealing with powers Posted: May 7, 2003 6:16 PM
Reply to this message Reply
I can't do it that way says the teacher, I have to find a more efficient way of solving the problem. Anyone???

Singh M.

Posts: 154
Nickname: ms
Registered: Mar, 2002

Re: dealing with powers Posted: May 7, 2003 11:06 PM
Reply to this message Reply
use Math.pow.

Flat View: This topic has 4 replies on 1 page
Topic: To: Erikprice Previous Topic   Next Topic Topic: Got part of it!  Comparator??? Please Help!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use