# Pyramid of Doubling Numbers

• October 28th, 2010, 11:10 AM
Override
Pyramid of Doubling Numbers
What's up,

I'm new to this forum, and hope to stick around awhile to learn Java more. Currently I'm halfway through the class, and am interested, but today I ran into a problem. I have a midterm project that I need to complete. The problem is to create a number pyramid like this, using only two for loops:

1
1 2
1 2 4
1 2 4 8
1 2 4 8 16
1 2 4 8 16 32
1 2 4 8 16 32 64
1 2 4 8 16 32 64 128
1 2 4 8 16 32 64
1 2 4 8 16 32
1 2 4 8 16
1 2 4 8
1 2 4
1 2
1

So far, I have this, and my teacher is no help -- he hasn't responded to my numerous email.

Code java:

```package midtermfinal;     public class Main {     public static void main(String[] args) {   int numLines = 15; int print_number; for (int i = 1; i < numLines * 2; i++) {   print_number =(i * 2);   for (int j = 1; i < print_number; j++) System.out.println(i + " " + print_number); }         }       }   }```

-----

I'm not looking for a quick solution, I'm just looking for a bit of advice, because not only do I want to finish this project before its due, but I want to understand the process. What I have now is actually creating a repeated lines of "1 2"

Any help would be appreciated.

Thanks,

Override
• October 28th, 2010, 12:43 PM
javapenguin
Re: Pyramid of Doubling Numbers
Well, perhaps you could have an incrementing for loop and a decrementing for loop.

The idea here is to use recursion to get it to print x plus all the x/2 till x is 1.

if num = 1, print 1;

else if (num!= 1)
print num/2 + " " + print num;
• October 28th, 2010, 01:07 PM
javapenguin
Re: Pyramid of Doubling Numbers
Hmmmm..use an array. An array of Strings.

Get it to print your current value plus all the values you want before it till it gets to 1.

You want an array with 8 places.

Also an int array with 8 values also would be nice.

int[] intArray = new int[8];

intArray[0] = 1;

for (int v = 1; v < 8; v++)
{
intArray[v] = 2 * intArray[v-1];
}

However, if you can only use 2 for loops, just do it manually without that for loop above.

The trick now is to get it to print out

...+ intArray[x/8] + intArray[x/4] + intArray[x/2] + intArray[x].

In incrementing order till you reach intArray[7] = 128;

Then you have to use the second for loop to decrement it so it prints 1 less each time.

Another interesting thing I just noticed it that it is printed out, like this, with the numbers I'm going to put below as the powers of 2.

0
0 1
0 1 2
0 1 2 3
0 1 2 3 4
0 1 2 3 4 5
0 1 2 3 4 5 6
0 1 2 3 4 5 6 7

then
0 1 2 3 4 5 6
0 1 2 3 4 5
0 1 2 3 4
0 1 2 3
0 1 2
0 1
0

In the first for loop, perhaps you should, but make sure to find a way to get spaces.

Code java:

```String [] str = new String[8]; int[] intArray = new int[8]; intArray[0] = 1; intArray[1] = 2; intArray[2] = 4; intArray[3] = 8; intArray[4] = 16; intArray[5] = 32; intArray[6] = 64; intArray[7] = 128; for (int x = 0; x < 8; x++) { if (x==0) str[x] = intArray[x];   else str[x] = str[x-1] + " " + intArray[x]; }```
You'll need to somehow get the two arrays to correspond together.

str[0] = intArray[0];
str[1] = intArray[0] + " " + intArray[1];
str[2] = intArray[0] + " " + intArray[1] + " " + intArray[2];

However str[1] can also be written as str[0] + " " + intArray[1];
and str[2] can be written as str[1] + " " + intArray[2];

hmmm
perhaps
if (x==0)
str[x] = intArray[x];

else
str[x] = str[x-1] + " " + intArray[x];

Yes, that should do it for the increasing part.

In the increasing order pyramid, there is one more number than the previous line.

In the decreasing order pyramid, this is one less number than the previous line.

Wait, change the type of intArray from int to Integer and change it to intArray[x].toString();

For the decreasing order, start with y = 6 and go to 0, including 0.

Oh, and for the decreasing order for loop, use a second String array of size 8.
• October 28th, 2010, 08:24 PM
Override
Re: Pyramid of Doubling Numbers
Hey, thanks for the advice. I tried out the arrays, but couldn't find a way to do it. We're just starting arrays, and I'm not too strong on it. But I did get an idea from your manipulating the 128. I tried it this way...

Code java:

```  package midtermfinal;     public class Main {     public static void main(String[] args) {   int numLines = 8; int print_number = 128;     for (int i = 1; i == 1; i++) {   System.out.println((print_number / 128)); System.out.println((print_number/ 128) + " " + (print_number/64)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2) + " " + (print_number/1)));     for (int j = 1; j < i; j++) System.out.println(print_number); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32)); System.out.println((print_number/128) + " " + (print_number/64)); System.out.println((print_number/128));     } }   }```

Now, I feel like I didn't use the two nested loops right, since I kept the loop condition continuation statement at " y == 1". Any suggestions on what I missed, such as a way for it to generate the numbers automatically? ( I just don't want my teacher to say "Oh, well you might as well have just printed it out manually" )
• October 29th, 2010, 08:33 AM
dustbreakangel
Re: Pyramid of Doubling Numbers
Done in about 10 minutes. Just input your pyramid length.

Code :

```import java.util.Scanner;   public class DoublePyramid { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int length = scan.nextInt(); String result = ""; int tempresult = 1; String[] array = new String[100];   for( int i = 0 ; i < length ; i++ ) { result += tempresult + " "; tempresult *= 2; System.out.println(result); array[i] = result; }   for( int i = length-2 ; i >= 0 ; i-- ) { System.out.println(array[i]); }   } }```

Sorry I can't make it any clearer. I'm kinda busy right now. If you don't know anything just ask :)

Btw how can I make cool code container like yours?
• October 29th, 2010, 01:45 PM
javapenguin
Re: Pyramid of Doubling Numbers
Quote:

Originally Posted by Override
Hey, thanks for the advice. I tried out the arrays, but couldn't find a way to do it. We're just starting arrays, and I'm not too strong on it. But I did get an idea from your manipulating the 128. I tried it this way...

Code java:

```  package midtermfinal;     public class Main {     public static void main(String[] args) {   int numLines = 8; int print_number = 128;     for (int i = 1; i == 1; i++) {   System.out.println((print_number / 128)); System.out.println((print_number/ 128) + " " + (print_number/64)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2) + " " + (print_number/1)));     for (int j = 1; j < i; j++) System.out.println(print_number); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4) + " " + (print_number/2))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8 + " " + (print_number/4))); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16) + " " + (print_number/8)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32) + " " + (print_number/16)); System.out.println((print_number/128) + " " + (print_number/64) + " " + (print_number/32)); System.out.println((print_number/128) + " " + (print_number/64)); System.out.println((print_number/128));     } }   }```

Now, I feel like I didn't use the two nested loops right, since I kept the loop condition continuation statement at " y == 1". Any suggestions on what I missed, such as a way for it to generate the numbers automatically? ( I just don't want my teacher to say "Oh, well you might as well have just printed it out manually" )

Well, hmmm...without arrays,

Not sure if Integers can have their values reset like this.
Code java:

```Integer i; String str = "";   for (int x =0, x < 8, x++) { i = (Integer) Math.pow(2,x); // casting result which returns a double to an Integer. if (x ==0) { str = i.toString(); System.out.println(str); } else if (x ==1) { str = (Integer) Math.pow(2,x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==2) { str = (Integer) Math.pow(2,x-2).toString() + " " + (Integer) Math.pow(2,x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==3) { str = (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==4) { str = (Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } else if (x==5) { str = (Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); }   else if (x==6) { str = (Integer) Math.pow(2,x-6).toString() + " " +(Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); }   else { str = (Integer) Math.pow(2,x-7).toString() + " " + (Integer) Math.pow(2,x-6).toString() + " " +(Integer) Math.pow(2, x-5).toString() + " " +(Integer) Math.pow(2,x-4).toString() + " " + (Integer) Math.pow(2,x-3).toString() + " " + (Integer) Math.pow(2,x-2).toString() + " " + (Integer)Math.pow(2, x-1).toString() + " " + i.toString(); System.out.println(str); } }```

However, you're better off using at least two arrays, assuming that Integers can have their values reset, you can simply do this:
Code java:

```Integer i, j;   String[] str = new String[8]; String[] str2 = new String[8];   for (int x = 0; x < 8; x++) { i = (Integer) Math.pow(2,x);   if (x==0) { str[0] = i.toString(); }   else { str[x] = str[x-1] + " " + i.toString(); }   System.out.println(str[x]); }   for (int y = 6; y >=0; y--) { j = (Integer) Math.pow(2,y);   if (y==0) { str2[y] = i.toString(); } else { str2[y] = str2[y-1] + " " + i.toString(); }   System.out.println(str2[y]); }```
• October 29th, 2010, 02:06 PM
javapenguin
Re: Pyramid of Doubling Numbers
Quote:

Originally Posted by dustbreakangel
Done in about 10 minutes. Just input your pyramid length.

Code :

```import java.util.Scanner;   public class DoublePyramid { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int length = scan.nextInt(); String result = ""; int tempresult = 1; String[] array = new String[100];   for( int i = 0 ; i < length ; i++ ) { result += tempresult + " "; tempresult *= 2; System.out.println(result); array[i] = result; }   for( int i = length-2 ; i >= 0 ; i-- ) { System.out.println(array[i]); }   } }```

Sorry I can't make it any clearer. I'm kinda busy right now. If you don't know anything just ask :)

Btw how can I make cool code container like yours?

I'll use java code to try to bypass the stupid code formatting so it doesn't show the format instead of showing how to make the format, which is what I'm aiming for.

String str = "highlight=java";

Code for opening is
[str]

String st2 = "/highlight";

code for closing is:
[str2]
• October 29th, 2010, 08:32 PM
dustbreakangel
Re: Pyramid of Doubling Numbers
Why use lengthy codes? use mine. It worked.
• October 29th, 2010, 10:01 PM
nemo
Re: Pyramid of Doubling Numbers
why use arrays? why not use algebra?

Code java:

```public class Doubling{   public static void main(String[] args) { for(int rows = 1; rows <= 15; rows++) { int num = 1; for(int x = 1; x <= -Math.abs(rows-8)+8; x++) { num *= 2; System.out.print((num/2) + " "); } System.out.print("\n");   } } }```