| Bytes | Lang | Time | Link |
|---|---|---|---|
| 013 | Japt | 170907T084110Z | Shaggy |
| 035 | Java 8 | 170907T131841Z | Kevin Cr |
| 037 | C# .NET Core | 170907T102602Z | Ayb4btu |
| 032 | QBIC | 170907T175047Z | steenber |
| 091 | Java OpenJDK 8 | 170907T133858Z | Roberto |
| 038 | Python 3 | 170906T211610Z | hyperneu |
| 012 | MATL | 170906T213415Z | Luis Men |
| 038 | Python 3 | 170907T032439Z | xnor |
| 052 | C# Visual C# Compiler | 170906T203603Z | Kamil Dr |
| 010 | Jelly | 170906T235311Z | Jonathan |
| 015 | Jelly | 170906T203413Z | Jonathan |
| 023 | Pyth | 170906T221549Z | Mr. Xcod |
| 052 | Perl 5 | 170906T214729Z | Xcali |
| 030 | JavaScript ES6 | 170906T201616Z | ETHprodu |
Japt, 14 13 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.
´V?U+ßXnU:U*W
´V?U+ßXnU:U*W :Implicit input of integers U=n, V=H, W & X=x
´V :Prefix decrement V
? :If truthy (not zero)
U+ : Add U to
ß : Recursive call with arguments
XnU : Subtract X from U and pass as U (V, W & X are implicit)
:U*W :Else return U*W
Java 8, 35 bytes
(x,W,H,n)->~-H*(x*(1-H/2d-W)+n)+W*n
Port of @Ayb4btu .Net C# answer, after I golfed it a bit.
If y is a mandatory input-parameter, it will be 37 bytes instead (Try it here):
(x,y,W,H,n)->~-H*(x*(1-H/2d-W)+n)+W*n
If outputting a double isn't allowed, it will be 40 bytes instead (Try it here):
(x,W,H,n)->~-H*(x*(int)(1-H/2d-W)+n)+W*n
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);}}
C# (.NET Core), 49 37 bytes
(x,y,W,H,n)=>~-H*(x*(1-H/2d-W)+n)+W*n
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.
Explanation
(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
n*(H-1)-x*((H-2)*(H-1)/2)
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).
Acknowledgements
Saved 12 bytes thanks to Kevin Cruijssen. Though I'm not sure if the ; should be included or not.
QBIC, 32 bytes
[0,:-1|i=:-(a*:)┘g=g+i}?i*:+g-i
This takes parameters in the order h, n, x, w, and y is ignored.
Explanation
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 (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;}
Python 3, 38 bytes
lambda x,W,H,n:~-H*(x*(1-H/2-W)+n)+W*n
-4 bytes thanks to Jonathan Allan
-4 bytes thanks to Kevin Cruijssen/Ayb4btu
MATL, 12 bytes
:q*-t0)iq*hs
Inputs are: H, x, n, W.
Explanation
: % 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*(n-x*(W-1+H/2))+n*W
HyperNeutrino's arithmetic expression with improved grouping.
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;}
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.
Jelly, 10 bytes
A construction technique, which ends up much like Luis Mendo's MATL answer
Ḷ×ạ⁵µṪ×⁶+S
A full program taking H, x, n, W in that order (y may be appended if one so wishes).
How?
Ḷ×ạ⁵µṪ×⁶+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)
Jelly, 18 16 15 bytes
-1 byte thanks to Erik the Outgolfer (use of chain separator)
S’×⁵_ðc2Ḣ+⁸’P¤×
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).
How?
The total is:
nmultiplied by the number of squares used in the T (which isW+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
Pyth, 23 bytes
+-*KEQ**JEQctQ2*tE-K*Jt
Try it here! (note the order of the inputs)
How?
+-*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.
JavaScript (ES6), 32 30 bytes
Saved 2 bytes thanks to @Shaggy
(w,h,x)=>Q=n=>--h?n+Q(n-x):n*w
Takes input in a curried format: f(W,H,x,y)(n)
let f =
(w,h,x)=>Q=n=>--h?n+Q(n-x):n*w;
console.log(
f(3, 4, 9, 9)(32)
);
How?
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.