2 power N loops

I'm trying figure out how to calculate powers of a certain numbers. For instance right now I have a loop iterates N from 0 to 99, then prints them on the left side, and then prints 2N for N the right side. I know I could easily use math.pow(), but for whatever reason my proffesser wants me to figure out another way to do it, which right now I can't seem to do.

Here's my code, I just plugged in the math.pow() function for now so you'll get an idea of what I'm doing.

int n = 0;

System.out.println (" N " +" " +"2 power N");

System.out.println ("" +" " +"");

for (int n1=0; n < 100; n++)

System.out.println (" "+ n +"" + Math.pow(2, n));

}

}

Thanks to anybody that can help.

[1102 byte] By [Dylancougara] at [2007-10-2 11:23:40]
# 1
2 to the power of 0 is 1.Each time through the loop you should be printing something thatis twice as big as you printed the last time.
pbrockway2a at 2007-7-13 4:27:55 > top of Java-index,Java Essentials,New To Java...
# 2

{

int n = 0;

System.out.println (" N " + " " + "2 power N");

System.out.println ("" + " " +"");

for (int n1=0; n < 100; n++)

System.out.println (" "+ n + "" + power(n,2));

}

public static int power(int n,int power){

int value = 1;

for(int i=0; i < power; i++){

value = value * n;

}

return value;

}

}

hope it is correct

--jubs--a at 2007-7-13 4:27:55 > top of Java-index,Java Essentials,New To Java...
# 3

Thanks for your help so far guys. --jubs-- with the code, it gives N to the power of 2 from 0 to 99, but what I need is 2 to power of N, I tried tweeking it a few different ways but didn't work.

2 to the power of 0 is 1.

Each time through the loop you should be printing something that

is twice as big as you printed the last time

The problem with this is that I need to print them in different columns. Meaning I print the N in one column and 2^n in the other with the N for example (2) and the 2^n of (2) , (1) being on the same row. I can't seem to do that by multplying what I get for (2^n) by 2.

If someone knows of an a way to express this using alegbraic expression, for example each time it loops increase the number of times 2 is multiplied by 2, (2 * 2) would 2^n, and so on,I tried racking my brain to come up with one, but I can't.

Dylancougara at 2007-7-13 4:27:55 > top of Java-index,Java Essentials,New To Java...
# 4

For the sake of your education, consider this. :)

You have a function available to you, Math.pow. It performs an exponentiation operation and that's all well and good. But it has to work somehow, right? The professor wants you to be able to invent an exponent function of your own so you can learn how to build a more complex tool out of the simpler tools available to you. Much of programming revolves around this skill, IMHO.

So, to do that, you have to try to break the task of exponentiation down and perform it using simpler tasks. What, precisely, does 2^3 mean? Well, you know that it is three twos multiplied together: 2*2*2. What about 3^4? 3*3*3*3. So, you need to determine how to take a base number (in the first example, 2) and multiply it by itself a certain number of times (in the first example, 3).

A similar but slightly simpler task would be creating a multiply function rather than using the * operator. What is multiplication? The expression of multiple addition operations as one operation.

jubs provided you code, but it's important that you understand how it works. ;) You need to be able to (1) multiply numbers together repeatedly, creating a total, and (2) control how many times you do so without coding that into the program. The first is accomplished by something like a=a*b, and the second is accomplished through a loop.

Enjoy your studies. :)

tvynra at 2007-7-13 4:27:55 > top of Java-index,Java Essentials,New To Java...
# 5

sorry about that didnt read it quite well...

but like tvynr said just find a way to multiply the base by itself a number of times....

public static int powerOfN(int power){

int value = 1;

for(int i=0; i < power; i++){

value = value * 2; //here 2 is the base

}

return value;

}

Hope this is what you wanted... Its better if you do these by urself...it helps in developing the logic.

Jubs

--jubs--a at 2007-7-13 4:27:55 > top of Java-index,Java Essentials,New To Java...
# 6

> sorry about that didnt read it quite well...

> but like tvynr said just find a way to multiply the

> base by itself a number of times....

>> public static int

>public static int powerOfN(int power){

>

>int value = 1;

> for(int i=0; i

>for(int i=0; i < power; i++){

> value = value * 2;

> value = value * 2; //here 2 is the base

>}

>

> return value;

>}

>

>

> Hope this is what you wanted... Its better if you do

> these by urself...it helps in developing the logic.

>

> Jubs

Ah perfect now I see how it works, thanks works perfectly now, and even better, now I've figured out the logic. tvynr thanks for your help also, now understand how to do similar tasks like this in the future.

Dylancougara at 2007-7-13 4:27:56 > top of Java-index,Java Essentials,New To Java...
# 7

>> 2 to the power of 0 is 1.

>> Each time through the loop you should be printing something that

>> is twice as big as you printed the last time

>The problem with this is that I need to print them in different columns.

So print them in different columns...System.out.println (" N " + " " + "2 power N");

System.out.println ("" + " " +"");

int powToPrint = 1;

for (int n=1; n < 100; n++) {

// do something to make powToPrint twice as big

System.out.println (" "+ n + "" + powToPrint);

}

Note: this is rather different from reimplementing pow as the others

have suggested (and which is a perfectly good idea) - I have no way of

knowing what your professor wants. If you need more help it might be a

good idea to post what you have so far.

pbrockway2a at 2007-7-13 4:27:56 > top of Java-index,Java Essentials,New To Java...
# 8

Another way to calculate powers of 2 is to left-shift a 1, like

public static int powerOf2(int power) {

return 1<<power;

}

>

pbrockway2a at 2007-7-13 4:27:56 > top of Java-index,Java Essentials,New To Java...
# 9

> Another way to calculate powers of 2 is to left-shift

> a 1, like

> > public static int powerOf2(int power) {

>return 1<<power;

> }

>

wow... thats even better.

Jubs

--jubs--a at 2007-7-13 4:27:56 > top of Java-index,Java Essentials,New To Java...