Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 136,411 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,453 people online right now. Registration is fast and FREE... Join Now!




Iterative/recursive methods

 
Reply to this topicStart new topic

Iterative/recursive methods

Program1
15 Oct, 2008 - 08:22 AM
Post #1

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 11


My Contributions
I need to write an iterative method called power1 to compute x^n for some n>=0.

This is all I have so far. Any help is appreciated

CODE

import java.awt.*;
import javax.swing.*;
import java.lang.Math.*;

class power1
{
    power1()
    {
        int n=0;
        int x=0;
    
        while (n<0)
        {
            x = x^n;
        }
        System.out.println();    
    }

        
}


User is offlineProfile CardPM
+Quote Post

Locke37
RE: Iterative/recursive Methods
15 Oct, 2008 - 08:40 AM
Post #2

I'm not a thief...I prefer the term TREASURE HUNTER!
Group Icon

Joined: 20 Mar, 2008
Posts: 1,005



Thanked: 39 times
Dream Kudos: 325
My Contributions
The operator ^ will not do what you want it to do. It's a bitwise operator, not a power operator.

Try this.

java
public static double power(int base, int exp)
{
double answer = base;

for (int x = exp; x > 0; x--)
{
// code to multiply stuff here
}
}


I'm not going to give you the entire method, but this will loop exp - 1 number of times passed to the method by exp. You should multiply something by something else in that loop. It takes only 1 statement if I'm thinking correctly (I'm in a music class, so I'm a little distracted tongue.gif)

Hope this helps! biggrin.gif

This post has been edited by Locke37: 15 Oct, 2008 - 08:41 AM
User is online!Profile CardPM
+Quote Post

Program1
RE: Iterative/recursive Methods
15 Oct, 2008 - 08:43 AM
Post #3

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 11


My Contributions
QUOTE(Locke37 @ 15 Oct, 2008 - 09:40 AM) *

The operator ^ will not do what you want it to do. It's a bitwise operator, not a power operator.

Try this.

java
public static double power(int base, int exp)
{
double answer = base;

for (int x = exp; x > 0; x--)
{
// code to multiply stuff here
}
}


I'm not going to give you the entire method, but this will loop exp - 1 number of times passed to the method by exp. You should multiply something by something else in that loop. It takes only 1 statement if I'm thinking correctly (I'm in a music class, so I'm a little distracted tongue.gif)

Hope this helps! biggrin.gif


Thanks. I'll try it your way.
User is offlineProfile CardPM
+Quote Post

bbq
RE: Iterative/recursive Methods
15 Oct, 2008 - 08:44 AM
Post #4

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
This works, i just coded it up. You could use integers however i prefer using doubles and perhaps a string format if you are to print the values out...

Function
java

double determinePower(double base, double power)
{
double answer = 1;

if (power == 0)
return 1;

else if (power == 1)
return base;

else
for(int i = 1; i <= power; i++)
{
answer *= base;
}
return answer;


}



Full example implementation
java

class Power
{
void run()
{
System.out.println("Program to compute a number a to the power of b\nTrial Values will be 5^0, 5^1 and 5^2, 5^3\nEnjoy");

System.out.println(String.format("%.0f",determinePower(5,0)) );
System.out.println(String.format("%.0f",determinePower(5,1)) );
System.out.println(String.format("%.0f",determinePower(5,2)) );
System.out.println(String.format("%.0f",determinePower(5,3)) );
}

double determinePower(double base, double power)
{
double answer = 1;

if (power == 0)
return 1;

else if (power == 1)
return base;

else
for(int i = 1; i <= power; i++)
{
answer *= base;
}
return answer;


}

public static void main(String[] args)
{
Power myPower = new Power();
myPower.run();
}


}


Hope this helped you
User is offlineProfile CardPM
+Quote Post

Program1
RE: Iterative/recursive Methods
15 Oct, 2008 - 08:53 AM
Post #5

New D.I.C Head
*

Joined: 25 Apr, 2008
Posts: 11


My Contributions
QUOTE(bbq @ 15 Oct, 2008 - 09:44 AM) *

This works, i just coded it up. You could use integers however i prefer using doubles and perhaps a string format if you are to print the values out...

Function
java

double determinePower(double base, double power)
{
double answer = 1;

if (power == 0)
return 1;

else if (power == 1)
return base;

else
for(int i = 1; i <= power; i++)
{
answer *= base;
}
return answer;


}



Full example implementation
java

class Power
{
void run()
{
System.out.println("Program to compute a number a to the power of b\nTrial Values will be 5^0, 5^1 and 5^2, 5^3\nEnjoy");

System.out.println(String.format("%.0f",determinePower(5,0)) );
System.out.println(String.format("%.0f",determinePower(5,1)) );
System.out.println(String.format("%.0f",determinePower(5,2)) );
System.out.println(String.format("%.0f",determinePower(5,3)) );
}

double determinePower(double base, double power)
{
double answer = 1;

if (power == 0)
return 1;

else if (power == 1)
return base;

else
for(int i = 1; i <= power; i++)
{
answer *= base;
}
return answer;


}

public static void main(String[] args)
{
Power myPower = new Power();
myPower.run();
}


}


Hope this helped you


Yes, this helps a lot. Thank you very much. biggrin.gif


User is offlineProfile CardPM
+Quote Post

bbq
RE: Iterative/recursive Methods
21 Oct, 2008 - 11:26 PM
Post #6

D.I.C Head
Group Icon

Joined: 15 May, 2008
Posts: 192



Thanked: 17 times
Dream Kudos: 50
My Contributions
You are welcome biggrin.gif
User is offlineProfile CardPM
+Quote Post

Gloin
RE: Iterative/recursive Methods
22 Oct, 2008 - 02:28 AM
Post #7

On MeD.i.Cation
Group Icon

Joined: 4 Aug, 2008
Posts: 723



Thanked: 47 times
My Contributions
This topic has been discussed before and it should be added that the function can be written recursively to perform in logarithmic time which is (for large powers) a substantial improvement to the iterative version that performs in linear time.

ex:
To calculate x^1000 using the iterative version will need to do 1000 multiplications.
To calculate x^1000 using the recursive version will need to do 2log(1000) = 10 multiplications.

(This is not equivalent with recursion being 100 times faster since handling of registers will take different time depending on the size of the numbers. Needless to say it's still alot faster)
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/2/08 12:24PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month