Make a triangle of numbers like below.
Sorry for my bad drawing!
First we have
12, and we take sum of the two numbers
2, which is
3. In case the sum of two numbers are greater than 9, we just take the second number from the sum. For example if we have
49 then the sum will be
4+9=13, we just take 3.
12 is the number we input, which is line 1,
3 - its sum, is line 2. We now create line 3 by cross-adding. For example, Line 1 contain
12 is split to
2, line 2 contain 3, so the line 3's first element will be the second element of line 1 plus line 2, and the second element of line 3 will be the first element of line 1 plus line 2. And remember, if the sum is greater than 9, just take the second number of the sum just like explained.
Now we come to line 4: the first element of line 4 now is calculated by line 2 plus the first element of line 3, and the third element of line 4 is line 2 plus the second element of line 3. The second element of line 3 - the middle, is the sum of all element in the previous line, which is line 3.
Now we come to line 5. Like line 4, the first and the last element of line 5 is calculated by the line 3's first element plus line 4 is first element and line 3's last element and line 4's last element. And the middle element of line 5, of course is the sum of all element in line 4. Now come to the second and the fourth element. You may notice that line 4 started to have the middle element, and the line 5's second element is just line 3's last element plus that middle element, and the line 5's fourth element is line 3's first element plus that middle element.
Line 6 is the same as line 5. And from now on, all the line just has 5 elements :)
- The integer ab will be a positive integer and contain only two digits (from 10 -> 99) Program will let user input two line: the first line is the ab number, and the second line is the number of lines they want to output.
- The program will output the triangle as a bunch of numbers. See the example in the Output section below.
- The number of lines will always be greater than or equal to 5.
- This is code-golf, so the shortest code in bytes wins!
If we input:
It should print out:
12 3 54 897 33441 11528
Have fun and good luck!