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 bad drawing!

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 :)


  • 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:


It should print out:


Have fun and good luck!

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

  • 7
    \$\begingroup\$ 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. \$\endgroup\$
    – pxeger
    Jun 9, 2021 at 16:47
  • 2
    \$\begingroup\$ 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 \$\endgroup\$ Jun 9, 2021 at 16:50
  • 2
    \$\begingroup\$ 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. \$\endgroup\$
    – hyper-neutrino
    Jun 9, 2021 at 16:50
  • 5
    \$\begingroup\$ 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). \$\endgroup\$ Jun 9, 2021 at 17:41
  • 3
    \$\begingroup\$ 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). \$\endgroup\$
    – Arnauld
    Jun 9, 2021 at 18:38

10 Answers 10


Python 3, 195 184 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!

  • \$\begingroup\$ This is short for python! \$\endgroup\$
    – raspiduino
    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, 517 501 410 408 bytes

-32 bytes thanks to mypetlion

-3 bytes thanks to Dion

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

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))

Input and output:

D:\Data\dev>python codegolf1.py

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

  • 2
    \$\begingroup\$ Change t=[];t.append([a//10,a%10]) to t=[[a//10,a%10]] to save 11 bytes. \$\endgroup\$
    – mypetlion
    Jun 10, 2021 at 21:16
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – mypetlion
    Jun 10, 2021 at 21:19
  • 1
    \$\begingroup\$ Change print(j,end="") to print(end=j) \$\endgroup\$
    – Dion
    Jun 11, 2021 at 5:45
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Jakque
    Jun 11, 2021 at 7:59
  • 2
    \$\begingroup\$ here is a link with all the modifications : Try it online! \$\endgroup\$
    – Jakque
    Jun 11, 2021 at 8:35

Jelly, 54 bytes


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.


Helper link

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

Main link

“½“¿“€“ç!‘                          | [[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
  • \$\begingroup\$ Wow, super tiny! \$\endgroup\$
    – raspiduino
    Jun 11, 2021 at 5:45

05AB1E, 53 bytes


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
  • \$\begingroup\$ 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!!!! \$\endgroup\$
    – raspiduino
    Jun 13, 2021 at 17:09
  • 1
    \$\begingroup\$ Some minor alternatives for two parts of the code (couldn't find anything shorter, though). Nice answer. :) \$\endgroup\$ Aug 5, 2021 at 7:28

JavaScript (SpiderMonkey), 226 bytes

`[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)+`

Try it online!


JavaScript (ES6),  203  198 bytes

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


Try it online!


Julia 0.7, 162 159 153 148 144 bytes

-6 bytes from ovs's answer


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, 295 292 289 281 272 bytes

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="")

Try it online!

-11 bytes thanks to @raspiduino

-9 bytes thanks to @MarcMush

  • \$\begingroup\$ You can change line 6 in your code to for j in i:print(end=str(j)[-1]) to save 2 bytes! \$\endgroup\$
    – raspiduino
    Jun 11, 2021 at 5:44
  • \$\begingroup\$ 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! \$\endgroup\$
    – raspiduino
    Jun 11, 2021 at 6:07
  • \$\begingroup\$ you can use a,b=map(int,input()) as suggested by @Jakque in @raspiduino's answer \$\endgroup\$
    – MarcMush
    Jun 11, 2021 at 15:16
  • \$\begingroup\$ @MarcMush dammit. Knew there was a way to do that input efficiently \$\endgroup\$
    – Dion
    Jun 11, 2021 at 15:21

Haskell, 205 bytes

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]
main=do[a,b]<-getLine;n<-readLn;mapM(putStrLn.map(l.show))$take n$read[a]#read[b]

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.