Loading...
Loading...
Loading...
In Pascal's triangle each row starts and ends with 1, and every other number is the sum of the two numbers above it:
`` 1 1 1 1 2 1 1 3 3 1 ``
Read n and print the first n rows. You have not learned arrays yet, so compute each number from the one before it in the same row.
Example
Input
5
Output
1 1 1 1 2 1 1 3 3 1 1 4 6 4 1
The fifth row is 1 4 6 4 1.
Try the problem first. Open one hint at a time.
Number k of row r is C(r, k), the number of ways to choose k items from r.
Each number follows from the one before it: C(r, k + 1) = C(r, k) × (r - k) / (k + 1). The first number is 1.
Multiply before dividing (the division is then always exact), and use long for the product.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor