I have been trying to write Pascal's triangle using a combination formula but it just doesn't work properly and I am not sure what the problem is?
Here's the input:
public class Probs {
public static int fact(int n) {
int f;
for (f = 1; n > 1; n--) {
f *= n;
}
return f;
}
public static int comb(int i, int j) {
return fact(i) / fact(i - j) * fact(j);
}
public static void main(String[] args) {
int n = 5;
int i;
int j;
for (i = 0; i < n; i++) {
for (j = 0; j < n - i; j++) {
System.out.print(" ");
}
for (j = 0; j <= i; j++) {
System.out.print(" " + comb(i, j));
}
System.out.println();
}
}
}
The Output:
1
1 1
1 2 4
1 3 12 36
1 4 24 144 576
Can you explain me why in a beginner-friendly way?
You need to add parentheses around the operations in comb() to get the correct precedence.
Both / and * have the same precedence, so when you write
return fact(i)/ fact(i-j)*fact(j);
This expression is actually equivalent to
return (fact(i)/fact(i-j)) * fact(j);
Which is not really what you want...
Fix it by adding parentheses around the product in the denominator:
return fact(i) / (fact(i-j)*fact(j));
Check your formula for getting the factorial. It should be:
factorial(i) / (factorial(i - j) * factorial(j))
Using streams, your code might look something like this:
public static int factorial(int n) {
return IntStream.rangeClosed(2, n).reduce((a, b) -> a * b).orElse(1);
}
public static int[][] pascalsTriangle(int n) {
return IntStream.range(0, n)
.mapToObj(i -> IntStream.range(0, i + 1)
.map(j -> factorial(i) / (factorial(i - j) * factorial(j)))
.toArray())
.toArray(int[][]::new);
}
public static void main(String[] args) {
int n = 10;
int[][] arr = pascalsTriangle(n);
pyramidOutput(arr);
}
public static void pyramidOutput(int[][] arr) {
String[] output = Arrays.stream(arr)
.map(row -> Arrays.stream(row).mapToObj(String::valueOf)
.collect(Collectors.joining(" ")))
.toArray(String[]::new);
int max = Arrays.stream(output)
.max(Comparator.comparing(String::length))
.orElse("").length();
Arrays.stream(output)
.map(row -> " ".repeat((max - row.length()) / 2) + row)
.forEach(System.out::println);
}
Output:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
See also: How do I make this into a Pascal's triangle instead of a Right triangle?
Related
I'd like to print the number of combination and not the actual combination of bits. How can I code that? I'm looking forward for some solution. Thank you!
The Task:
Write a program that accepts a number. This number corresponds to the number of bits to be taken into account. The program should then display on the screen how many binary combinations there are that do not consist of two adjacent 1s. For example, given a 3-bit number, there are 5 out of 8 possible combinations.
import java.util.Scanner;
public class BinaryS {
public static String toString(char[] a) {
String string = new String(a);
return string;
}
static void generate(int k, char[] ch, int n) {
if (n == k) {
for (int i = 0; i < ch.length; i++) {}
System.out.print(toString(ch) + " ");
return;
}
// If the first Character is
//Zero then adding**
if (ch[n - 1] == '0') {
ch[n] = '0';
generate(k, ch, n + 1);
ch[n] = '1';
generate(k, ch, n + 1);
}
// If the Character is One
// then add Zero to next**
if (ch[n - 1] == '1') {
ch[n] = '0';
// Calling Recursively for the
// next value of Array
generate(k, ch, n + 1);
}
}
static void fun(int k) {
if (k <= 0) {
return;
}
char[] ch = new char[k];
// Initializing first character to Zero
ch[0] = '0';
// Generating Strings starting with Zero--
generate(k, ch, 1);
// Initialized first Character to one--
ch[0] = '1';
generate(k, ch, 1);
}
public static void main(String args[]) {
System.out.print("Number: ");
Scanner scanner = new Scanner(System.in);
int k = scanner.nextInt();
//Calling function fun with argument k
fun(k);
}
}
The program actually works fine , my only problem is I would like to print the number of combinations and not the actual combination. For example for the input 3 we get 000 001 010 100 101 which is 5.
Unfortunately, your code has some problems. For one you have an empty forloop in the generate method. However, I can help you get the count by doing it a different way and printing the results. Forgetting about the loop that goes from 2 to 20, here is what is going on. And this may not be most efficient way of finding the matches but for short runs it exposes the counts as a recognizable pattern (which could also be determined by mathematical analysis).
first, create an IntPredicate that checks for adjacent one bits by masking the lower order two bits.
Generate an IntStream from 0 to 2n where n is the number of bits.
then using aforementioned predicate with a filter count every value that does not contain two adjacent 1 bits.
IntPredicate NoAdjacentOneBits = (n)-> {
while (n > 0) {
if ((n & 3) == 3) {
return false;
}
n>>=1;
}
return true;
};
for (int n = 1; n <= 20; n++) {
long count = IntStream.range(0, (int) Math.pow(2, n))
.filter(NoAdjacentOneBits).count();
System.out.println("For n = " + n + " -> " + count);
}
prints (with annotated comments on first three lines)
For n = 1 -> 2 // not printed but would be 0 and 1
For n = 2 -> 3 // 00, 01, 10
For n = 3 -> 5 // 000, 001, 010, 100, 101
For n = 4 -> 8
For n = 5 -> 13
For n = 6 -> 21
For n = 7 -> 34
For n = 8 -> 55
For n = 9 -> 89
For n = 10 -> 144
For n = 11 -> 233
For n = 12 -> 377
For n = 13 -> 610
For n = 14 -> 987
For n = 15 -> 1597
For n = 16 -> 2584
For n = 17 -> 4181
For n = 18 -> 6765
For n = 19 -> 10946
For n = 20 -> 17711
The counts are directly related to the nth term of the Fibonacci Series that starts with 2 3 5 8 . . .
So you really don't even need to inspect the values for adjacent bits. Just compute the related term of the series.
I'm having trouble with an assignment where we are required to print out this array:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
My code is somewhat correct but it is not printing 10 and 19 where it should be.
My output:
Choose a number for the rows from 0 to 16.
5
Choose a number for the columns from 0 to 16
5
1 0 10 0 19
2 9 11 18 20
3 8 12 17 21
4 7 13 16 22
5 6 14 15 23
My code:
//snake move with the number
import java.util.Scanner;
public class SnakeMove {
public static void main(String[] args) {
//create Scanner object
Scanner inScan = new Scanner(System.in);
//prompt the user to choose number for the Row from 0 to 16
System.out.println("Choose a number for the rows from 0 to 16.");
//take the input from user with nextInt() method
//use the variable int row
int row = inScan.nextInt();
//prompt the user to choose number for the Col from 0 to 16
System.out.println("Choose a number for the columns from 0 to 16");
//take the input from user with nextInt()
//use the variable int col
int col = inScan.nextInt();
if (row != col) {
System.out.println("Run the program again and choose the same number for Row and Col");
System.exit(0);
}
int[][] arr = move(row, col);
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}//main method
static int[][] move(int row, int col) {
boolean flag = true;
int count = 1;
int[][] array = new int[row][col];
for (int j = 0; j < array[0].length; j++) {
if (flag) {
for (int i = 0; i < array.length; i++) {
//assign the increment value of count
// to specific array cells
array[i][j] = count;
count++;
}
flag = false;
} else {
//row decrement going up
for (int i = array.length - 1; i > 0; i--) {
//assign the increment value of count
// to specific array cells
array[i][j] = count;
count++;
}
flag = true;
}
}//column increment
return array;
}//move method
}//end SnakeMove class
Can anyone detect what is causing the error? Any help would be appreciated.
I believe this is a good alternative and easy to understand. Do comment if you have a simplified version of the same.
Code:
import java.util.Arrays;
import java.util.Scanner;
public class SnakePatternProblem {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // *INPUT*
int row = scanner.nextInt();
int col = scanner.nextInt();
int[][] array = new int[row][col];
// Initializing first row of the array with a generalized expression
for (int i = 0; i < col; i++) {
if (i % 2 != 0) array[0][i] = col * (i+1);
else array[0][i] = (col * i) + 1;
}
array[0][0] = 1; // this element is not covered in the above loop.
// Nested loop for incrementing and decrementing as per the first row of elements.
for (int i= 1; i< row; i++){
for (int j=0; j< col; j++){
if(j%2==0) array[i][j] = array[i-1][j] + 1;
else array[i][j] = array[i-1][j] - 1;
}
}
System.out.println(Arrays.deepToString(array)); // *OUTPUT
}
}
Explanation:
Consider first row of 5 x 5 matrix named "arr" with snake pattern:
1 10 11 20 21
The element in the odd column is equivalent to ((currentColumn + 1) * Total_No_Of_columns);
Example: arr[0][1] = (1 + 1)* 5 = 10
The element in the even column is equivalent to (currentColumn * Total_No_Of_columns) + 1;
Example: arra[0][2] = (2 * 5) + 1 = 11;
Important Note: element at first row, first column is Zero
arr[0][0] = 1 -> must be declared
Now, the remaining matrix is incrementing or decrementing loop from the first element in that column.
Elements with even column number gets incremented by 1 in each row from the first element of that column.
1 -> First element of column-0
2 -> (1 + 1)
3 -> (2 + 1).... so on
4
5
Elements with odd column number gets decremented by 1 in each row from the first element of that column.
10 -> First element of column - 1
9 -> (10 - 1)
8 -> (9 - 1).... so on
7
6
This will generate the "snaking" pattern you described.
It could be simplified with ternary but this makes it more readable I think
It would be interesting to find a more clever way about it though, if anyone finds a better way pls comment
public static int[][] genArray(int length) {
int[][] arr = new int[length][length];
int counter = 0;
for (int col = 0; col < arr.length; col++) {
if (col % 2 == 0) {
for (int row = 0; row < arr.length; row++) {
arr[row][col] = counter++;
}
} else {
for (int row = arr.length - 1; row >= 0; row--) {
System.out.println("row: " + row + ", col: " + col);
arr[row][col] = counter++;
}
}
}
}
return arr;
}
You can create such an array as follows:
int m = 5;
int n = 4;
int[][] snakeArr = IntStream.range(0, n)
.mapToObj(i -> IntStream.range(0, m)
// even row - straight order,
// odd row - reverse order
.map(j -> (i % 2 == 0 ? j : m - j - 1) + i * m + 1)
.toArray())
.toArray(int[][]::new);
// output
Arrays.stream(snakeArr)
.map(row -> Arrays.stream(row)
.mapToObj(e -> String.format("%2d", e))
.collect(Collectors.joining(" ")))
.forEach(System.out::println);
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
Transposing snake array:
int[][] transposedSA = new int[m][n];
IntStream.range(0, m).forEach(i ->
IntStream.range(0, n).forEach(j ->
transposedSA[i][j] = snakeArr[j][i]));
// output
Arrays.stream(transposedSA)
.map(row -> Arrays.stream(row)
.mapToObj(e -> String.format("%2d", e))
.collect(Collectors.joining(" ")))
.forEach(System.out::println);
1 10 11 20
2 9 12 19
3 8 13 18
4 7 14 17
5 6 15 16
An easy way to do it is to create a 2-D array as shown below and then print its transpose.
1 2 3 4 5
10 9 8 7 6
11 12 13 14 15
20 19 18 17 16
21 22 23 24 25
It is a 2-D array of the dimension, LEN x LEN, where LEN = 5.
The above pattern goes like this:
The pattern starts with a start value of 1.
When the main loop counter is an even number, the counter, which assigns a value to the array, starts with start value and increases by one, LEN times. Finally, it sets start for the next row to start with final_value_of_the_array_value_assignment_counter - 1 + LEN.
When the main loop counter is an odd number, the counter, which assigns a value to the array, starts with start value and decreases by one LEN times. Finally, it sets start for the next row to start with start + 1.
The transpose of the above matrix will look like:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
In terms of code, we can write it as:
public class Main {
public static void main(String[] args) {
final int LEN = 5;
int[][] arr = new int[LEN][LEN];
int start = 1, j, k, col;
for (int i = 0; i < LEN; i++) {
if (i % 2 == 0) {
k = 1;
for (j = start, col = 0; k <= LEN; j++, k++, col++) {
arr[i][col] = j;
}
start = j - 1 + LEN;
} else {
k = 1;
for (j = start, col = 0; k <= LEN; j--, k++, col++) {
arr[i][col] = j;
}
start++;
}
}
// Print the transpose of the matrix
for (int r = 0; r < LEN; r++) {
for (int c = 0; c < LEN; c++) {
System.out.print(arr[c][r] + "\t");
}
System.out.println();
}
}
}
Output:
1 10 11 20 21
2 9 12 19 22
3 8 13 18 23
4 7 14 17 24
5 6 15 16 25
Change the value of LEN to 10 and you will get the following pattern:
1 20 21 40 41 60 61 80 81 100
2 19 22 39 42 59 62 79 82 99
3 18 23 38 43 58 63 78 83 98
4 17 24 37 44 57 64 77 84 97
5 16 25 36 45 56 65 76 85 96
6 15 26 35 46 55 66 75 86 95
7 14 27 34 47 54 67 74 87 94
8 13 28 33 48 53 68 73 88 93
9 12 29 32 49 52 69 72 89 92
10 11 30 31 50 51 70 71 90 91
I am trying to make a java program to print the Pascaline triangle. But it is not working properly. The code is provided below :
int rows=10;
int[] array=new int[10], temp=new int[10];
array[0]=1;
temp[0]=1;
System.out.println(1);
for(int i=1;i<rows;i++)
{
for(int j=1;j<=i;j++)
{
temp[j]=array[j-1]+array[j];
}
for(int term:temp)
{
System.out.print(term+"\t");
}
System.out.println();
array=temp;
}
It is giving the following output :
1
1 1
1 2 3
1 3 5 5
.....
Please tell what's wrong with the code.
Pascaline triangle is not factorial serial
A proposal is (warning I am not a Java programmer, please don't be rude with me if something is stupid / can be improved easily) :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
and to be a little prettier :
public class Pascaline {
public static void main(String args[]) {
int n = 10, i, j;
int [] f = new int[n];
f[0] = 1;
for (i = 1; i != n; i++)
f[i] = f[i - 1] * i;
for(i = 0; i < n; i++) {
for(j = 0; j < n-i; j++)
System.out.print(" ");
for(j = 0; j <= i; j++)
System.out.print((f[i] / (f[i - j] * f[j])) + " ");
System.out.println();
}
}
}
Compilation and execution :
pi#raspberrypi:/tmp $ javac Pascaline.java
pi#raspberrypi:/tmp $ java Pascaline
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
I got an assignment that requires us to print out pascal's triangles based on the user entered value of N. We were provided a main that allows the user to calculate Pascal’s Triangle based on a value of n. In this case if n is 0, then Pascal’s Triangle is 1. Otherwise for n being greater than 0, the appropriate Pascal’s Triangle will be created and displayed. Here is the main:
public class CSCD210Lab13
{
public static void main(String[] args)
{
int n = 0;
int [][] pascal = null;
do
{
n = Lab13Methods.readN();
pascal = Lab13Methods.createPascalsTriangle(n);
Lab13Methods.printPascals(pascal);
}while(MyUtil.goAgain());
}// end main
}// end class
Here is my Methods file:
import java.util.*;
public class Lab13Methods
{
public static int readN()
{
Scanner kb = new Scanner(System.in);
System.out.println("Enter N: ");
int n = kb.nextInt();
while(n < 0)
{
System.out.println("Number Below 1. Re-Enter: ");
n = kb.nextInt();
}
return n;
}
public static int[][] createPascalsTriangle(int n)
{
int[][]pascalTri = new int[n + 1][(n + 1) * 2];
int sideOne, side;
pascalTri[0][n - 1] = 1;
sideOne = side = n - 1;
for (int y = 1; y < n; y++)
{
pascalTri[y][sideOne] = 1;
pascalTri[y][side] = 1;
sideOne--;
side++;
for (int k = 1; k <= y; k++)
{
int left = pascalTri[y - 1][sideOne + (2 * k) - 1];
int right = pascalTri[y - 1][sideOne + (2 * k) + 1];
pascalTri[y][sideOne + (2 * k)] = left + right;
}
}
return pascalTri;
}
public static void printPascals(int[][]pascal)
{
for (int f = 0; f < pascal.length; f++)
{
for (int v = 0; v < pascal[f].length; v++)
{
if (pascal[f][v] == 0)
{
System.out.print("");
}
else
{
System.out.print(pascal[f][v]+" ");
}
}
System.out.println();
}
}
}
Here is my goAgain file:
public static boolean goAgain()
{
boolean goAgain = false;
String answer;
Scanner kb = new Scanner(System.in);
System.out.println();
System.out.print("Do you want to go again? ");
answer = kb.nextLine();
while(!answer.equalsIgnoreCase("yes") && !answer.equalsIgnoreCase("no"))
{
System.out.print("Invalid Input. Do you want to go again? ");
answer = kb.nextLine();
}
if(answer.equalsIgnoreCase("yes"))
{
goAgain = true;
}
else if(answer.equalsIgnoreCase("no"))
{
goAgain = false;
}
return goAgain;
}
}
My question is about how it's printing. If I enter 10 to be the value of N, this is how it is supposed to print:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1
1 6 15 20 15 6 1
1 7 21 35 35 21 7 1
1 8 28 56 70 56 28 8 1
1 9 36 84 126 126 84 36 9 1
1 10 45 120 210 252 210 120 45 10 1
However, this is how mine prints:
1
1 1 1 1
1 1 2 1 1
1 1 3 3 1 1
1 1 4 6 4 1 1
1 1 5 10 10 5 1 1
1 1 6 15 20 15 6 1 1
1 1 7 21 35 35 21 7 1 1
1 1 8 28 56 70 56 28 8 1 1
What am I doing wrong?
I think your error may be here:
pascalTri[y][sideOne] = 1;
pascalTri[y][side] = 1;
sideOne--;
side++;
Your program is designed to fill in the cells of the array in a checkerboard pattern:
for any two adjacent rows, one row will have non-zero entries only
in even-numbered locations, and the other will have non-zero entries
only in odd-numbered locations.
Notice that right after you do pascalTri[y][sideOne] = 1;, you decrement sideOne.
That means if you are in a row that should be using odd-numbered cells,
sideOne now is odd, but when you did pascalTri[y][sideOne] = 1;,
sideOne was still even. So you have put an even-numbered entry in a row that
should have only odd-numbered entries.
That is where all the extra 1s are coming from in your output.
Just delete these lines:
pascalTri[y][sideOne] = 1;
pascalTri[y][side] = 1;
All they are doing is creating those extra, unwanted 1 values. All the correct values
are being written in the array by other statements.
I don't know if you know what a pascal triangle is let me explain to you what it is.
11^0 = 1
11^1 = 11
11^2 = 121
11^3 = 1331
11^4 = 14641
11^5 = 161051
I don't know why have you done some much code all when you need was
public static void printPascalsTriangle(int n)
{
long number=11l;
for(int i=0;i<=n;i++)
{
System.out.println(new Double(Math.pow(number,i)).longValue());
}
}
You would need a case more that five which can be handled like this link.
Refer this short pascal code i have written which is depend on user input:
public class Pascal {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scanner= new Scanner(System.in);
System.out.println("Enter the Number of levels of Pascal");
int levelCount = scanner.nextInt();
for(int i =0;i<levelCount;i++) {
int value = 1;
for(int j=0;j<=i;j++) {
System.out.println(value);
value = value * (i - j) / (j + 1);
}
System.out.println("\n");
}
}
}
Enjoy..!!
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
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
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