Xeshan J
Xeshan J

Reputation: 372

Pyramid Pattern in Java

It was so hard to ask such a newbie question on this advanced site. But after so much tries and even loosing my hope i was forced to bring my self here. I am not been able to print the following pattern:

                    1                                                                                                                              
                1   2   1   
            1   2   4   2   1   
        1   2   4   8   4   2   1   
    1   2   4   8   16  8   4   2   1
1   2   4   8   16  32  16  8   4   2   1

But with my tiresome efforts i reached the following:

public static void main(String[] args) {
    int num = 1;
    for (int i = 0; i < 15; i++) {
        for (int j = 0; j < 15 - i; j++) {
            System.out.print(" ");
        }
        for (int k = 0; k <= i; k++) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
}


           1 
          1 1 
         1 1 1 
        1 1 1 1 
       1 1 1 1 1 
      1 1 1 1 1 1 
     1 1 1 1 1 1 1 
    1 1 1 1 1 1 1 1 
   1 1 1 1 1 1 1 1 1 
  1 1 1 1 1 1 1 1 1 1 
 1 1 1 1 1 1 1 1 1 1 1 
1 1 1 1 1 1 1 1 1 1 1 1 

Upvotes: 1

Views: 8173

Answers (2)

upperdim
upperdim

Reputation: 92

I find the other answer very overwhelming and dramatic. You don't need much maths and complexity to solve this problem. This might not be the best code but I think it is easy to understand. Not even an explanation is needed, it is a row by row approach, It's good to keep things simple.

public static void main(String[] args) {

    // Init
    int row = 0;
    int maxRows = 6;
    int num = 1;
    int indent = maxRows - 1;

    // Printing loop
    while (row < maxRows) {

        // Indent
        for (int i = 0; i < indent; ++i) 
            System.out.print("    ");

        // Print nums
        for (int i = 0; i < num; ++i) 
            System.out.printf("%4d", (int) Math.pow(2.0, i));
        for (int i = num - 2; i >= 0; --i)
            System.out.printf("%4d", (int) Math.pow(2.0, i));

        // New line
        System.out.println(""); 

        // Adjustments
        ++row;
        --indent;
        ++num;
    }

Output:

                               1
                           1   2   1
                       1   2   4   2   1
                   1   2   4   8   4   2   1
               1   2   4   8  16   8   4   2   1
           1   2   4   8  16  32  16   8   4   2   1

Upvotes: 1

mleko
mleko

Reputation: 12243

Here ya go

public static void main(String[] args) {
    int max = 6;
    int padLength = (int) Math.ceil(Math.log10(Math.pow(2, max) + 1)) + 2;
    for (int i = 0; i < max; i++) {
        for (int j = 1; j < max - i; j++) {
            System.out.print(pad(" ", padLength));
        }
        for (int k = 0; k <= i; k++) {
            System.out.print(pad(Math.pow(2, k), padLength));
        }
        for (int k = i - 1; k >= 0; k--) {
            System.out.print(pad(Math.pow(2, k), padLength));
        }
        System.out.println();
    }
}

public static String pad(double d, int l) {
    Integer i = (int) d;
    return pad(i.toString(), l);
}

public static String pad(String s, int l) {
    return String.format("%-" + l + "s", s);
}

Explanation

int padLength = (int) Math.ceil(Math.log10(Math.pow(2, max) + 1)) + 2;

Math.pow(2,max) - Gives me maximal number I will have to display

Math.ceil(Math.log10(number + 1)) - I use this to determine length of string representation of specific number. Please refer to wikipedia to check what logarithm is. I add 1 to skip edge case when number is exact power of 10 e.g. log10(10)->1 (this will never occur in task specified in question, it's just for purity of solution). Ceil just rounds number up.

+2 - minimum gap between two numbers is specified example was 2 spaces long so I just add this

You could use here Integer.toString(((int)Math.pow(2, max))).length()+2 but it's not as pretty :)

return String.format("%-" + l + "s", s);

First I build format string that looks like e.g. %-3s, which means print String with minimum length of 3, padding on the right. Second argument is the String I want to print. Refer to documentation

Running example

Upvotes: 3

Related Questions