# Challenge

Make a triangle of numbers like below.

# How to?

Let say we get a numbers in the form of ab, for example 12. We then create a triangle like below:

Sorry for my bad drawing!

First we have 12, and we take sum of the two numbers 1 and 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 1 and 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 :)

# Rule

• 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 , so the shortest code in bytes wins!

# Output example

If we input:

12
6


It should print out:

12
3
54
897
33441
11528


Have fun and good luck!

## Note that I will select the winner after 4 more hour since the time edited.

• Welcome to Code Golf! This looks like a reasonably well-specified challenge, but for future reference, we strongly recommend using the Sandbox to get feedback on challenge ideas before posting them to the main site. Jun 9, 2021 at 16:47
• It's generally up to you how long you want to leave a challenge in the Sandbox, but the recommended time frame is a week, to properly allow people to give feedback and for you to address that Jun 9, 2021 at 16:50
• That would depend on the clarity and complexity of the challenge, how much it's seen and how much feedback it gets, etc. Feel free to ask in The Nineteenth Byte (our main chat room) for more feedback on your sandboxed challenge if your post isn't getting attention (since you have enough reputation to do so). Leaving it for at least a week is recommended to get enough attention and feedback. Jun 9, 2021 at 16:50
• I see that line 7 will have only five elements, if we call them [a7, b7, c7, d7, e7] then is d7 the trailing digit of c6 + a5 (rather than c6 + b5)? Also, I'd suggest avoiding the cumbersome IO (i.e. I would allow people to take two integers in $[0..9]$ and to output in any reasonable way, e.g. a list of lists of integers); challenges without such restrictions tend to be better received (unless the IO is a core part of the challenge, unlike here). Jun 9, 2021 at 17:41
• Could you provide some other test cases? I'd recommend to include at least one with more than 6 lines and clarify how the 7th line and next ones are generated (as already asked by Jonathan). Jun 9, 2021 at 18:38

# Python 3, 195184 181 bytes

-3 bytes thanks to MarcMush!

Based on OP's code

def f(a,b):w=a//10;s=w+a;t=[[w,a],[s],[a+s,s+w],[2*s+a,s*3,2*s+w]];exec('*_,(x,*_,z),y=t;Y=y[len(y)//2];r,*t=t+[[x+y[0],z+Y,sum(y),x+Y,z+y[-1]]];print(*[x%10for x in r],sep="");'*b)


Try it online!

• This is short for python! Jun 11, 2021 at 5:50

# Charcoal, 102 bytes

≔Ｉ⪪⮌Ｓ¹θ⊞υ⮌θ⊞υ⟦Σθ⟧≧⁺Σθθ⊞υθ≔⁺§υ¹θζ≔Ｅζ⁺ι§ζ⊕κη⊞υη≔ηζＦ⁻Ｎ⁴«≔⁺⊞ＯＥθ⁺κ§ηλΣζＥθ⁺κ§η⊕λζ⊞υζ≔Φη¬﹪λ²θ≔Φζ¬﹪λ²η»Ｅυ⭆ι﹪λχ


Try it online! Link is to verbose version of code. Explanation:

≔Ｉ⪪⮌Ｓ¹θ


Input the two digits, reverse them, split them, and cast to integer.

⊞υ⮌θ


Push the reverse of this (i.e. the original integers) to the list of results.

⊞υ⟦Σθ⟧


Push the sum as a list to the list of results, giving the second row.

≧⁺Σθθ


Add the two together, giving the third row. This is also the first row that takes part in diagonal calculations.

⊞υθ


Push this to the list of results.

≔⁺§υ¹θζ


Concatenate the second and third rows.

≔Ｅζ⁺ι§ζ⊕κη


Rotate the result and add it elementwise to produce the fourth row. This is also the second row that takes part in diagonal calculations.

⊞υη


Push this to the list of results.

≔ηζ


This is also the last result so far.

Ｆ⁻Ｎ⁴«


For the remaining rows...

≔⁺⊞ＯＥθ⁺κ§ηλΣζＥθ⁺κ§η⊕λζ


Calculate the diagonal sums before and after the sum of the previous row, thus producing the next row.

⊞υζ


Push this to the list of results.

≔Φη¬﹪λ²θ≔Φζ¬﹪λ²η


Shift all the rows back one, removing elements that do not participate in the diagonal sums.

»Ｅυ⭆ι﹪λχ


Output just the last digit of every element.

# Python 3, 517501410 408 bytes

-32 bytes thanks to mypetlion

-3 bytes thanks to Dion

After fixing a bunch of bugs :), i come up with this:

a=int(input());b=int(input());t=[[a//10,a%10]];t.append([(t[0][0]+t[0][1])%10]);t.append([(t[0][1]+t[1][0])%10,(t[0][0]+t[1][0])%10]);t.append([(t[1][0]+t[2][0])%10,(t[2][0]+t[2][1])%10,(t[1][0]+t[2][1])%10])
for i in range(4,b):x=t[i-2];y=t[i-1];t.append([(x[0]+y[0])%10,(x[-1]+y[len(y)//2])%10,sum(y)%10,(x[0]+y[len(y)//2])%10,(x[-1]+y[-1])%10])
for i in range(b):
for j in t[i]:print(end=str(j))
print()


Input and output:

D:\Data\dev>python codegolf1.py
12
6
12
3
54
897
33441
11528


The first 12 and 6 is the input, all other number is output.

• Change t=[];t.append([a//10,a%10]) to t=[[a//10,a%10]] to save 11 bytes. Jun 10, 2021 at 21:16
• Change for j in range(len(t[i])):print(t[i][j],end="") to for j in t[i]:print(j,end="") to save 21 bytes. Jun 10, 2021 at 21:19
• Change print(j,end="") to print(end=j)
– Dion
Jun 11, 2021 at 5:45
• you can change your last 3 lines by : for i in t:print(*i,sep="") saving 33 bytes. You can also replace all the t.append(element) by t+=[element] saving another 20 bytes. Jun 11, 2021 at 7:59
• here is a link with all the modifications : Try it online! Jun 11, 2021 at 8:35

# Jelly, 54 bytes

.ị+U,$€Ḣ$}Ṛṭ@FṖS;ʋ
“½“¿“€“ç!‘Dṫ-ç@/ṭƊ⁸¡EÐḟUṚ;@Ʋ€ḋ%⁵ḣ⁸


Try it online!

A set of links taking the left argument as the number of rows and the right as the initial integers. Returns a list of lists of integers.

If the IO is inflexible, here is a solution for 56 bytes that takes the arguments in the opposite order, the starting point as a single integer and prints the number triangle to STDOUT.

## Explanation

Take the previous line on the left and the last but one on the right and generate the next line

.ị                  | Last and first items in the most recent line
+     $} | Add to the following applied to the last but one line: U,$€             | - Pair a reversed version of each list member to itself
Ḣ            | - And then just take the first of these
Ṛ         | Reverse order
ṭ@     ʋ | Tag on the following (effectively applied to the previous line)
F      | - Flatten
Ṗ     | - Remove last
S    | - Sum
;  | - Concatenate to itself


“½“¿“€“ç!‘                          | [[10],[11],[12],[23,33]]
D                         | Convert to decimal digits [[[1,0]],[[1,1]],[[1,2]],[[2,3],[3,3]]]
Ɗ⁸¡                | Repeat the following the number of times indicated by the original left argument
ṫ-                       | - Take the last two list members
ç@/                    | - Call the helper link using the last line as the left argument and the penultimate one as the right
ṭ                   | - Tag the new line onto the end of the list
Ʋ€       | For each line:
EÐḟ             | - Filter out items where both members of the list are equal
U            | - Reverse each list member
Ṛ           | - Reverse order of list members
;@         | - Concatenate to existing list
ḋ      | Dot product with starting integers
%⁵    | Mod 10
ḣ⁸  | Take the number of lines indicated by the original left argument

• Wow, super tiny! Jun 11, 2021 at 5:45

# 05AB1E, 53 bytes

ÐˆO©D¸ˆ+Âˆ®+®3*ªÀ©ˆI4-F®н®ÅsD®Os®θ)¯¨θÂ‚€н0ª5∍+©ˆ}¯T%


Try it online!

This uses lists of digits as I/O, 56 bytes with the format in the challenge. The output is built row by row in the global array:

Ð                    # push three copies of the input [w, a]
ˆ                   # append one copy to the global array
O©                 # take the sum s=w+a and store it in the register
D¸               # duplicate s and wrap in a list
ˆ              # append [s] to the global array
+             # add s to each [w, a]
Â            # duplicate and reverse the copy
ˆ           # append [a+s, w+s] to the global array
®+         # add s to [w+s, a+s]
®3*      # push 3*s
ª     # append: [w+2*s, a+2*s, 3*s]
À    # rotate left
©   # store in the register, from now the register always stores the last row
ˆ  # append [a+2*s, 3*s, w+2*s] to the global array

I4-                  # push second input - 4
F              }  # iterate this many times
®н               # get the first value of the last row y
®Ås            # middle value of last row
®O         # sum of last row
D  s        # middle value again
®θ      # last value of last row
)     # wrap all these numbers into a list      [y[0], y[m], sum(y), y[m], y[-1]]
¯¨θ              # push second-to-last row of global array (x)
Â‚            # pair with its reverse                   [x, x[::-1]]
€н          # push first element for each             [x[0], x[-1]]
0ª        # append a 0                              [x[0], x[-1], 0]
5∍      # extend to length 5                      [x[0], x[-1], 0, x[0], x[-1]]
+     # add both lists element-wise             [y[0]+x[0], y[m]+x[-1], sum(y)+0, y[m]+x[0], y[-1]+x[-1]]
©ˆ   # save new row in register and add to global array

¯                    # push the global array
T%                  # each number modulo 10

• This is shorter, but I have selected OP's answer because they answer sooner than you and your answer is late. But thanks for the short answer!!!! Jun 13, 2021 at 17:09
• Some minor alternatives for two parts of the code (couldn't find anything shorter, though). Nice answer. :) Aug 5, 2021 at 7:28

# JavaScript (SpiderMonkey), 226 bytes

R=readline
v=R()
h=(n,k=--n<4?n:5,g=(i,j,s=1
12
233
35845
51508.split
[i])=>s?s[j]*v[0]+v*s[(i<3?i:4)-j]:j-2?g(i-2,j%3*4)+g(i-1,j&4|j%2*2):(v- -v[0])*'093859835433'[i%12])=>k--?h(n,k)+g(n-1,k)%10:n?h(n)+
:v
print(h(R()))


Try it online!

• @Arnauld should be fixed
– tsh
Jun 11, 2021 at 1:22

# JavaScript (ES6),  203  198 bytes

Expects (N)(n), where N is the initial number as a string and n is the number of lines.

N=>n=>(s=7,g=k=>k<n?${F=n=>n>k-2?k-n:F(++n)+F(++n),A=[d=s+F(1),b=F,c=s+F(2),s+=a,a=F(-1)],[41,1,14,134][k]||10324} .replace(/./g,n=>(A[n]*N[0]+A["24031"[n]]*N)%10)+g(k+1,a+=k>3?b+c+d:b):'')(a=0)  Try it online! # Julia 0.7, 162 159 153 148 144 bytes -6 bytes from ovs's answer !x=x.%10 ~L=L[end] (a,b)$n=.![(a,b);c=a+b;((b,a).+c,(b,c,a).+2c)>n]
(l,L)>n=n<3?l:[l;(L,(l[1]+L[1],(N=L[end÷2+1])+~l,sum(L),N+l[1],~l+~L))>n-1]


Try it online!

takes the initial number as 2 numbers and output is a list of tuples

+10 bytes with stricter I/O

# Python 3, 295292289281 272 bytes

a,b=map(int,input())
e=[[a,b],[a+b],[b+a+b,a+a+b],[3*b+a+a,3*a+3*b,3*a+b+b]]
for i in range(3,int(input())-1):f=e[i];e.append([e[i-1][0]+f[0],e[i][len(e[i])//2]+e[i-1][-1],sum(f),f[len(f)//2]+e[i-1][0],e[i-1][-1]+f[-1]])
for i in e:
for j in i:print(j%10,end="")
print()


Try it online!

-11 bytes thanks to @raspiduino

-9 bytes thanks to @MarcMush

• You can change line 6 in your code to for j in i:print(end=str(j)[-1]) to save 2 bytes! Jun 11, 2021 at 5:44
• In e.append([e[i-1][0]+e[i][0],e[i][len(e[i])//2]+e[i-1][-1],sum(e[i]),e[i][len(e[i])//2]+e[i-1][0],e[i-1][-1]+e[i][-1]]) you call e[i] multiple times so you should define e[i] as something and call it. That will save lots of bytes! Jun 11, 2021 at 6:07
• you can use a,b=map(int,input()) as suggested by @Jakque in @raspiduino's answer Jun 11, 2021 at 15:16
• @MarcMush dammit. Knew there was a way to do that input efficiently
– Dion
Jun 11, 2021 at 15:21

# Haskell, 205 bytes

l=last
x%y|m<-y!!div(length y)2=x:y%[x!!0+y!!0,l x+m,sum y,x!!0+m,l x+l y]
a#b|s<-a+b=[a,b]:[s]:[s+b,a+s]%[3*s-a,3*s,3*s-b]
main=do[a,b]<-getLine;n<-readLn;mapM(putStrLn.map(l.show))$take n$read[a]#read[b]


Try it online!