Given a "T" shape on an x * y number grid, with length W on the top bar and H on the stem of the T, with the bottom of the T on the square numbered n: calculate the total of all of the numbers in the T shape. W must be an odd number and all must be positive integers.

Here are some examples of valid T's on a 9*9 grid:


Looking at the T where n=32, W=3 & H=4, you can see that the total is:
4 + 5 + 6 + 14 + 23 + 32 = 84.

The Challenge

Your challenge is to create a program which, when given five positive integers, x, y, W, H and n, output the total of the T with those values (W must be odd, given an even number the program can output anything or even crash). The numbers may be inputted in any reasonable format. If the T does not fit on the given grid, then any output is acceptable.

Example Code (Python 3.6.2)

x = int(input())
y = int(input())
W = int(input())
H = int(input())
n = int(input())

total = 0

#"Stem" of the T
for i in range(H - 1):
    total += n - (x * i) #Decrease increment by length of row each time

#Top of the T 
for i in range(-W//2 + 1, W//2 + 1): #Iterate over the width offset so that the center is zero
    total += (n - (x * (H - 1))) + i #Add the top squares on the T, one less to the left and one more to the right


As with most challenges here this is , so the answer with the shortest code in bytes wins.

  • 5
    \$\begingroup\$ Isn't y redundant information? \$\endgroup\$ Sep 6 '17 at 20:09
  • \$\begingroup\$ @JonathanAllan I'm presuming it needn't be a square grid in which case y would not be redundant. x & y together would define the grid, n defines the bottom of the T shape, and W & H together define the T shape's size. \$\endgroup\$ Sep 6 '17 at 20:11
  • 1
    \$\begingroup\$ @EngineerToast we are told n and x and told "If the T does not fit on the given grid, then any output is acceptable." so y may be inferred. \$\endgroup\$ Sep 6 '17 at 20:12
  • 1
    \$\begingroup\$ There could be many ways to approach the challenge, there's probably a builtin for it in Mathematical. \$\endgroup\$
    – Leo
    Sep 6 '17 at 20:33
  • 3
    \$\begingroup\$ Can you add some test cases, please? \$\endgroup\$
    – Shaggy
    Sep 6 '17 at 22:19

14 Answers 14


JavaScript (ES6), 32 30 bytes

Saved 2 bytes thanks to @Shaggy


Takes input in a curried format: f(W,H,x,y)(n)

let f =

  f(3, 4, 9, 9)(32)


First we note that the sum of the T starting at n with height H can be broken down into two sums:

  • n
  • The sum of the T starting one row higher with height H - 1

By repeatedly adding n to the total, moving one row up, and subtracting 1 from H until it reaches 1, we end up summing the vertical stem of the T. Moving one row up is accomplished by subtracting x from n, since it can be observed that the difference between any cell and the one above is x.

When H reaches 1, we now have only the crossbar of width W left to sum. At this point n represents the center of the crossbar. We could sum the crossbar recursively as we did with the stem, but instead we take advantage of a fairly simple formula:

sum(n - a : n + a) = (n - a) + (n - (a-1)) + ... + n + ... + (n + (a-1)) + (n + a)
                   = (n - a) + (n + a) + (n - (a-1)) + (n + (a-1)) + ... + n
                   = 2n + 2n + ... + n
                   = n * (2a + 1)

In this case, our a is (W - 1) / 2, which makes

n * (2a + 1) = n * (((W - 1) / 2) * 2 + 1)
             = n * ((W - 1) + 1)
             = n * W

which is the sum of the crossbar.

  • \$\begingroup\$ Seeing as you don't use y you just go with (x,w,h)=>Q=n=> and then call it using f(x,w,h)(n,y)? Seem to remember this coming up on Meta recently but can't remember the outcome. \$\endgroup\$
    – Shaggy
    Sep 6 '17 at 20:32
  • \$\begingroup\$ @Shaggy Thank you, found a better format for that trick :-) \$\endgroup\$ Sep 6 '17 at 20:47

C# (Visual C# Compiler), 64 52 bytes

(x,y,W,H,n)=>{for(y=0;H-->1;n-=x)y+=n;return y+W*n;}

Try it online!

The non-recursive answer did indeed turn out significantly shorter. Gross misuse of for loops and the fact that y is officially a mandatory input even though it's not used.

  • \$\begingroup\$ Should return y+3*n; be return y+W*n? Otherwise you wouldn't need to pass in W. \$\endgroup\$
    – Ayb4btu
    Sep 7 '17 at 0:59
  • \$\begingroup\$ @Ayb4btu ... You know, I've actually had to fix that mistake multiple times along the way. I don't know how it keeps getting back in. \$\endgroup\$ Sep 7 '17 at 2:27

MATL, 12 bytes


Inputs are: H, x, n, W.

Try it online!


:     % Implicit input: H. Push [1, 2, ..., H]
q     % Subtract 1. Gives [0, 1, ..., H-1]
*     % Implicit input: x. Multiply. Gives [0, x, ..., x*(H-1)]
-     % Implicit input: n. Subtract. Gives [n-0, n-x, ..., n-x*(H-1)]. This is
      % the stem, bottom to top
t     % Duplicate
0)    % Get last element, that is, n-x*(H-1). This is the center top of the "T"
i     % Input: W
q     % Subtract 1
*     % Multiply. Gives n-x*(H-1)*(w-1). This is the sum of the vertical bar
      % excluding its center, which is already accounted for in the stem
h     % Concatenate into a row vector
s     % Sum of vector. Implicit display

Python 3, 38 bytes

lambda x,W,H,n:~-H*(x*(1-H/2-W)+n)+W*n

Try it online!

-4 bytes thanks to Jonathan Allan
-4 bytes thanks to Kevin Cruijssen/Ayb4btu

  • \$\begingroup\$ EDIT: four bytes ...Save two bytes with our old friend tilde lambda x,W,H,n:n*H-x*H*~-H/2+~-W*(n-H*x+x) (FYI the TIO link has too many args in the call to f) \$\endgroup\$ Sep 6 '17 at 21:25
  • \$\begingroup\$ @JonathanAllan Oh whoops thanks. And yay thanks! :D \$\endgroup\$
    – hyper-neutrino
    Sep 6 '17 at 21:29
  • \$\begingroup\$ @LuisMendo Wait whoops I fixed that but forgot to update the link and then un-fixed it when golfing. Thanks! \$\endgroup\$
    – hyper-neutrino
    Sep 7 '17 at 1:26
  • \$\begingroup\$ lambda x,W,H,n:~-H*(x*(1-H/2-W)+n)+W*n is 4 bytes shorter. (Credit goes to @Ayb4btu's C# .NET answer.) \$\endgroup\$ Sep 7 '17 at 13:20
  • \$\begingroup\$ @KevinCruijssen Oh cool, thanks! \$\endgroup\$
    – hyper-neutrino
    Sep 7 '17 at 13:39

Perl 5, 52 bytes


Try it online!

  • \$\begingroup\$ Do yoy need to take y as input, seeing as you're not using it? \$\endgroup\$
    – Shaggy
    Sep 7 '17 at 8:45
  • \$\begingroup\$ Not really, but it was part of the input spec, so I included it. \$\endgroup\$
    – Xcali
    Sep 7 '17 at 13:09

Jelly,  18 16  15 bytes

-1 byte thanks to Erik the Outgolfer (use of chain separator)


A full program taking [H,W], x, n in that order (you can add y to the arguments if you like, it's not used).

Try it online!


The total is:

  • n multiplied by the number of squares used in the T (which is W+H-1)
  • minus the width of the grid, x, times the triangle number of the height (1+2+3+...+h) to account for the lack as we go up the stem
  • minus the width of the grid, x, times one less that the height (H-1) times one less that the width (W-1) to account for the lack at the top of the T, excluding the lack we already accounted for at the top of the stem.

That is:

(W + H - 1) * n - ((H - 1) * (W - 1) + Triangle(H)) * x

The triangle number of H is its binomial with 2 A.K.A. H-choose-2.

c2Ḣ+⁸’P¤× - Link 1: the x * (Triangle(H) + (H-1)*(W-1)): [H,W]; x    
S’×⁵_ðc2Ḣ+⁸’P¤× - Main link: [H,W], x
S               - sum                                    H+W
 ’              - decrement                              H+W-1
   ⁵            - program's fifth argument (3rd input)   n
  ×             - multiply                               (H+W-1)*n
     ð          - dyadic chain separation
      c2        - choose-2 (vectorises)                  [Triangle(H), Triangle(W)]
        Ḣ       - head                                   Triangle(H)
             ¤  - nilad followed by link(s) as a nilad:
           ⁸    -    chain's left argument               [H,W]
           ’    -    decrement (vectorises)              [H-1,W-1]
            P   -    product                             (H-1)*(W-1)
         +      - add                                    (H-1)*(W-1)+Triangle(H)
              × - multiply (by chain's right arg, x)     ((H-1)*(W-1)+Triangle(H))*x
    _           - subtract                               (H+W-1)*n-((H-1)*(W-1)+Triangle(H))*x

Python 3, 38 bytes

lambda x,W,H,n:~-H*(n-x*(W-1+H/2))+n*W

Try it online!

HyperNeutrino's arithmetic expression with improved grouping.


C# (.NET Core), 49 37 bytes


Try it online!

A direct formula avoiding the need for looping or recursion. Unfortunately I had to use 2d otherwise it was going to do integer division and truncate the fractional component. The y param is completely redundant and could be removed.


(n-x*(H-1))*W             // Calculates the Top of the T

(n-x*(H-1)) gets the cell index by subtracting from n the number of rows to go up multiplied by the grid width. Multiplying this by W gets the sum of the top bar of the T.

n*(H-1)-x*(H-2)*(H-1)/2   // Calculates the Stem of the T 

I got this by using mathematical induction by trying to calculate the sum of 1, 10, 19, 28 where n=28, H=4, x=9. Which can be written as:

28 + (28-9) + (28-9-9) + (28-9-9-9)
28-9*0 + 28-9*1 + 28-9*2 + 28-9*3
28*4 -9*(0+1+2+3)
n*H -x*(1+2+3)
n*H -x*(3*(3+1)/2)
n*H -x*((H-1)*((H-1)+1)/2)
n*H -x*((H-1)*H/2)

but because we don't want to include the top cell of the stem (included in the formula for the Top of the T), H needs to be H-1. Making the formula


Combining these two formulas gives

(n-x*(H-1))*W + n*(H-1)-x*((H-2)*(H-1)/2)

and simplifying it gives the formula used for the answer (though how it is rearranged can change how it looks).


Saved 12 bytes thanks to Kevin Cruijssen. Though I'm not sure if the ; should be included or not.

  • 1
    \$\begingroup\$ You can golf some things: =>{return ...} can be =>...; and (H-1) can be ~-H. So in total: (x,y,W,H,n)=>~-H*(x*(1-H/2d-W)+n)+W*n - 37 bytes \$\endgroup\$ Sep 7 '17 at 13:03
  • \$\begingroup\$ @KevinCruijssen Thanks, that's a huge saving, I'm not sure how I missed them... \$\endgroup\$
    – Ayb4btu
    Sep 7 '17 at 19:25

Pyth, 23 bytes


Try it here! (note the order of the inputs)


+-*KEQ**JEQctQ2*tE-K*Jt    Full program. Q is input, E is evaluated input (reads a new line)

  *KEQ                     The second input * the first input.
       *JEQ                The third input * the first input.
           ctQ2            Float division of the the first input decremented by 1, by 2.
      *                    Product.
 -                         Difference.
                tE         The fourth input.
                    *Jt    The product of the third input and the first input decremented by 1...
                 -K        ... Subtracted from the second input
               *           Product.
+                          Sum.
                           Output implicitly.

Jelly, 10 bytes

A construction technique, which ends up much like Luis Mendo's MATL answer


A full program taking H, x, n, W in that order (y may be appended if one so wishes).

Try it online!


Ḷ×ạ⁵µṪ×⁶+S - Main link: H, x
Ḷ          - lowered range                         [0,1,2,...,H-2,H-1]
 ×         - multiply (vectorises)                 [0,x,2x,...,(H-2)x,(H-1)x]
   ⁵       - program's 5th argument (3rd input)    n
  ạ        - absolute difference (vectorises)      [n,n-x,n-2x,...,n-(H-2)x,n-(H-1)x]
           -  (note: for an in-range T n>(H-1)x)   (cells of the stem)
    µ      - monadic chain separation (call this stem)
     Ṫ     - tail (pop from AND modify stem)       n-(H-1)x
           -                                       (cell at intersection of stem and top)
       ⁶   - program's 6th argument (4th input)    W
      ×    - multiply                              Wn-W(H-1)x
           -                                       (total value of the top)
         S - sum (the modified stem)               n+n-x+n-2x+...+n-(H-2)x
           -                                       (total value of the stem w/o top cell)
        +  - add (add top to the stem w.o top cell)

Japt, 14 bytes

I tried a few different solutions based on ETH's observations but the shortest I've come up with (so far) is a straight port.

Takes input in the order n,H,W,x.


Try it


Java (OpenJDK 8), 100 99 96 91 bytes

x->W->H->n->{int t=n,i=0,j=-W/2;for(;++i<H;t+=n-x*i);for(;j<W/2;t+=n-x*~-H+j++);return-~t;}

Try it online!

  • \$\begingroup\$ When using two parameters currying is shorter; when using three parameters currying is the same length; but when using four or more parameters currying is actually longer. So you can replace x->W->H->n-> with (x,W,H,n)->, like this. \$\endgroup\$ Sep 7 '17 at 14:44
  • \$\begingroup\$ Also, you can save some additional bytes by changing t+=(n-x*(H-1))+j++ to t+=n-x*~-H+j++ so you don't need the four parenthesis. And return t+1; can be return-~t; so you don't need the space. Here is the relevant codegolfing tip post for using -~ and ~- with more information. \$\endgroup\$ Sep 7 '17 at 14:46
  • \$\begingroup\$ @KevinCruijssen Thank you, I'm just using currying because I don't know if the bytes of the functional interface are counted \$\endgroup\$ Sep 7 '17 at 14:53

QBIC, 32 bytes


This takes parameters in the order h, n, x, w, and y is ignored.


Each time a variable is read from the cmd line in QBIC, using the : command, it is assigned a letter (a-z). For clarity, I will use h, n, x, w in the explanation instead of the b,c,d,e they would be assigned (a gets taken by the FOR-loop).

First, we want to know the total of the stem
[0,h-1|     FOR a = 0 to the height - 1
  i=n-(a*x)     helper i holds an increment value: this is n, minus the 
                grid-width once for every row we go up.
  g=g+i         helper g is incremented by i for each row
  }         NEXT
  ?         PRINT
   i*w          the center on the top bar times the width (ie the average)
   -i           corrected for that ione i we already counted in the stem
   +g           plus the stem itself

Java 8, 35 bytes


Port of @Ayb4btu .Net C# answer, after I golfed it a bit.

Try it here.

If y is a mandatory input-parameter, it will be 37 bytes instead (Try it here):


If outputting a double isn't allowed, it will be 40 bytes instead (Try it here):


If having a function instead of a full program isn't allowed, it will be 160 bytes instead (Try it here):

interface M{static void main(String[]a){Integer W=new Integer(a[2]),H=W.decode(a[3]),n=W.decode(a[4]);System.out.print(~-H*(W.decode(a[0])*(1-H/2d-W)+n)+W*n);}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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