Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Pyramid of Doubling Numbers

  1. #1
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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.




    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
    Last edited by copeg; October 28th, 2010 at 11:56 AM.


  2. #2
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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;

  3. The Following User Says Thank You to javapenguin For This Useful Post:

    Override (October 29th, 2010)

  4. #3
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default 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.

    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.
    Last edited by javapenguin; October 28th, 2010 at 02:30 PM.

  5. #4
    Junior Member
    Join Date
    Oct 2010
    Posts
    9
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default 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...

     
    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" )
    Last edited by Override; October 28th, 2010 at 08:26 PM.

  6. #5
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Pyramid of Doubling Numbers

    Done in about 10 minutes. Just input your pyramid length.

    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?
    Last edited by dustbreakangel; October 29th, 2010 at 08:52 AM.

  7. #6
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Pyramid of Doubling Numbers

    Quote Originally Posted by Override View Post
    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...

     
    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.
    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:
    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]);
    }
    Last edited by javapenguin; October 29th, 2010 at 01:59 PM.

  8. #7
    Banned
    Join Date
    May 2010
    Location
    North Central Illinois
    Posts
    1,631
    My Mood
    Sleepy
    Thanks
    390
    Thanked 112 Times in 110 Posts

    Default Re: Pyramid of Doubling Numbers

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

    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]
    Last edited by javapenguin; October 29th, 2010 at 02:16 PM. Reason: Grrrrrr, stupid coding won't let me type what to say, but I think I found a way.

  9. #8
    Junior Member
    Join Date
    Oct 2010
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Re: Pyramid of Doubling Numbers

    Why use lengthy codes? use mine. It worked.

  10. #9
    Junior Member
    Join Date
    Oct 2010
    Posts
    14
    Thanks
    2
    Thanked 2 Times in 1 Post

    Default Re: Pyramid of Doubling Numbers

    why use arrays? why not use algebra?

    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");
     
    		}
    	}
    }

Similar Threads

  1. [SOLVED] Number Pyramid Question
    By t-rank in forum Loops & Control Statements
    Replies: 2
    Last Post: December 2nd, 2013, 08:44 AM
  2. help with a loop (pyramid of numbers)
    By ande6870 in forum Loops & Control Statements
    Replies: 2
    Last Post: October 7th, 2010, 08:17 PM
  3. Help with sorting numbers
    By planktonx in forum Collections and Generics
    Replies: 2
    Last Post: October 5th, 2010, 08:01 AM
  4. adding up odd and even numbers
    By darlinho in forum What's Wrong With My Code?
    Replies: 10
    Last Post: September 30th, 2010, 03:28 PM
  5. Doubling hashing, infinite loop?
    By vluong in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 8th, 2009, 01:26 AM