| Bytes | Lang | Time | Link |
|---|---|---|---|
| 131 | Python | 240906T174427Z | NikoNyrh |
| 087 | Clojure | 240906T143614Z | NikoNyrh |
| 008 | Vyxal | 240812T231156Z | emanresu |
| 047 | Ruby | 240815T111420Z | G B |
| 6860 | Wolfram Language Mathematica | 240820T025226Z | 138 Aspe |
| 050 | JavaScript | 240814T040231Z | noodle p |
| 048 | Python 2 | 240812T234108Z | Lucenapo |
| 024 | K ngn/k | 240814T142209Z | att |
| 082 | R | 240813T202411Z | int 21h |
| 051 | R | 240814T114338Z | Dominic |
| 052 | APL+WIN | 240813T170209Z | Graham |
| 013 | 05AB1E | 240813T091741Z | Kevin Cr |
| 033 | Charcoal | 240813T002546Z | Neil |
| 044 | JavaScript Node.js | 240812T195454Z | emanresu |
| 066 | JavaScript Node.js | 240812T195248Z | l4m2 |
Python, 131 bytes
import numpy
lambda a,b,c,n:numpy.arange(b*c*n).reshape((-1,b,c)).transpose((0,2,1)).reshape((-1,a,b)).sum(axis=(1,2)).flatten()[n]
TIO. Unlike the other Python answer, this re-constructs the entire grid and uses Numpy's ndarray manipulation to squeeze out the correct numbers. You get the complete summed grid if you remove .sum(axis=(1,2)).flatten()[n]. It was very helpful when debuggin :)
Interesting how this is longer than my Clojure solution, while here I'm using specially-built Numpy operations and Clojure uses general-purose partition.
Clojure, 87 bytes
#(for[P[partition]p(P %2(P(quot %3 %1)(P %1(range))))r(apply map concat p)](apply + r))
TIO. Yay, Clojure is quite good with partitioning and "transposing" lists. This doesn't take any mathematical short-cuts, as you can see. apply map concat was a bit tricky to get right. This anonymous function evaluates into an infinite sequence of sums.
Vyxal, 8 bytes
Þ:ẇẇṠfẇṠ
Try it Online! Outputs an infinite list given w,y,x.
Þ: # 0...infinity
ẇ # Cut into chunks of length w
ẇṠ # Sum groups of length y
fẇṠ # Flatten, and sum groups of length x
Ruby, 46 47 bytes
->w,x,y,n{(n*x/w*w*y*2+n*x%w*2+y*w+~w+x)*x*y/2}
Short explanation: first get the first and last number of a chunk, then sum and multiply by x"y/2
Thanks @emanresu A for +1 byte. Actually, for fixing an oversimplification which led to a wrong result in some cases.
Wolfram Language (Mathematica), 68 60 bytes
Saved 8 bytes thanks to @noodle person
f[w_,x_,y_,n_]:=x*y(y*w⌊n*x/w⌋+Mod[n*x,w]+(y*w-w+x-1)/2)
JavaScript, 50 bytes
(n,x,y,w)=>x*y*(y*w*(x*n/w|0)+(w*~-y+x-1)/2+x*n%w)
This uses a very similar method to emanresu A's solution, though I found it independently.
Explanation: We can view the problem as dealing with arithmetic series.
Here's the first chunk from the example in the question:
+----------+
| 0 1 2 |
| 12 13 14 |
| 24 25 26 |
| 36 37 38 |
+----------+
Take the vertical sums of each column:
0+12+24+36 = 72
1+13+25+37 = 76
2+14+26+38 = 80
This is an arithmetic sequence with first term 72 and common difference of 4. The 4 comes from the value of y, and the 72 comes from an arithmetic series of y terms with first term zero and common difference w.
Except, the first term of that inner arithmetic series isn't always zero. Look at the first chunk of the second column:
+----------+
| 48 49 50 |
| 60 61 62 |
| 72 73 74 |
| 84 85 86 |
+----------+
Here, the first term isn't 0 - it's 48. We get this 48 by multiplying y, w, and the column number of the cell. In JS, this is written as y*w*(x*n/w|0) (where |0 floors a number).
Now we can get the sum of any cell in the first column, but we still need to account for the difference between rows.
The sums of the first row are 228, 264, 300, and 336. The difference between each sum is 36, which is \$ x^2y \$. We multiply that by the row number, giving x*n%w*x*y. Here, x*n%w gives us the row number multiplied by x.
Okay, that's all we need to write this all out in JS. Remember that the formula for an arithmetic series is \$ \frac{n}{2}(2a+(n-1)d) \$, where n is the number of terms, a is the first term, and d is the common difference.
x/2*(2*y/2*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y
Now reduce this expression to its final golfed form:
x/2*(2*y/2*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y
x/2*(y*(2*w*y*(x*n/w|0)+(y-1)*w)+(x-1)*y)+x*n%w*x*y // 2*y/2 = y
x/2*(y*(2*w*y*(x*n/w|0)+~-y*w)+~-x*y)+x*n%w*x*y // (y-1) = ~-y
x*(y*(2*w*y*(x*n/w|0)+~-y*w)+~-x*y)/2+x*n%w*x*y // rearrange
x*y*(2*w*y*(x*n/w|0)+~-y*w+~-x)/2+x*n%w*x*y // factor out y
x*y*((2*w*y*(x*n/w|0)+~-y*w+~-x)/2+x*n%w) // factor out x*y
x*y*(w*y*(x*n/w|0)+(~-y*w+~-x)/2+x*n%w) // factor out 2
Python 2, 58 54 53 51 49 48 bytes
lambda x,y,w,n:x*y*~(x*~(n*y*2-(n%w*2-w)*~-y))/2
Here w means the number of x-wide chunks in a row.
K (ngn/k), 24 bytes
{+/y/(!x)+x*(0,_y%x 1)\}
Input [y x;w]. Returns a function that computes the \$n\$th term of the sequence.
I think the dumb approach is shorter.
R, 85 82 bytes
-3 bytes thanks to pajonk
\(x,y,w,`[`=matrix,s=colSums)repeat show(s(s(((y*w*F):(y*w*(F=F+1)-1))[x])[y,,T]))
Just to see the source and the bytecount: Attempt This Online!
A user-friendly version: Test cases at TIO
A function that takes x,y and w as an input and outputs indefinetely the sequence of the sums.
Explanation (a naive approach):
Let's take \$X=3\$, \$Y=4\$, \$W=12\$ as an example. First, we obtain a matrix with the dimensions X × W*Y/X:
[,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15] [,16]
[1,] 0 3 6 9 12 15 18 21 24 27 30 33 36 39 42 45
[2,] 1 4 7 10 13 16 19 22 25 28 31 34 37 40 43 46
[3,] 2 5 8 11 14 17 20 23 26 29 32 35 38 41 44 47
Calculate the column-sums:
3 12 21 30 39 48 57 66 75 84 93 102 111 120 129 138
Rearrange them into the matrix Y × W/X:
[,1] [,2] [,3] [,4]
[1,] 3 12 21 30
[2,] 39 48 57 66
[3,] 75 84 93 102
[4,] 111 120 129 138
Calculate the column-sums:
228 264 300 336
Proceed with the next part of the sequence Y*W to 2*Y*W-1 etc.
R, 52 51 bytes
Edit: -1 byte by taking g directly as a parameter
\(n,x,y,g)y*x*((y*g-g+1)*x/2+n%/%g*g*x*y+n%%g*x-.5)
Started from:
xychunks=
function(n,l,w,h){
startpos=n%%(l/w)*w+n%/%(l/w)*h*l
increment=outer(1:w-1,(1:h-1)*l,`+`)
sum(startpos+increment)
}
and gradually re-arranged from there.
The final golfed form seems quite similar to some answers in other languages, and I suspect that different initial approaches will converge to similar shortest re-arranged structures...
R + pryr, 44 bytes
f(y*x*((y*g-g+1)*x/2+n%/%g*g*x*y+n%%g*x-.5))
Same as above, but the using the pryr package to 'guess' the function arguments from the code.
APL+WIN, 52 bytes
Prompts for a vector of n,x,y and w/x. Outputs first n terms. Index origin = 0
(n x y w)←⎕⋄n↑,+⌿[1] +/¨(n,y,w)⍴(+\0=x|i)⊂i←⍳n×x×y×w
05AB1E, 14 13 bytes
∞<IôIô€øO˜IôO
-1 byte thanks to @emanresuA.
Inputs in the order \$W,Y,X\$. Outputs the infinite sequence.
Explanation:
∞ # Push an infinite positive list: [1,2,3,...]
< # Decrease each by 1 to a non-negative infinite list: [0,1,2,...]
Iô # Split it into groups of the first input W
Iô # Split those into groups of the second input Y
€ # Map over each inner matrix:
ø # Zip/transpose; swapping rows/columns
O # Sum each inner-most list
˜ # Flatten the infinite list of lists
Iô # Split it into groups of the third input X
O # Sum each inner list again
# (after which the infinite list is lazily output implicitly)
Charcoal, 38 33 bytes
NθNηNζNεI÷××θη⊖×θ⊕⁻⊗×εη×⁻⊗﹪εζζ⊖η²
Try it online! Link is to verbose version of code. Takes X, Y, W/X, n as inputs. Explanation: Looks like I've found the same closed formula. Really, verbose mode says it all, except of course the variables have to be called q, h, z, and e instead, just in case you want to save 8 bytes by requiring input in JSON format. Edit: Saved 5 bytes by porting @xnor's golf to @Lucenaposition's answer.
JavaScript (Node.js), 48 47 46 43 42 48 46 45 44 bytes
(n,x,y,w)=>x*y*(n*x*y+(--y*w+x-1)/2-n*x%w*y)
Try it online! -3ish thanks to Arnauld, -1 thanks to xnor, hopefully now actually works. There might be a shorter version taking w/x instead.
Simple numeric formula. They say a picture speaks a thousand words:
JavaScript (Node.js), 66 bytes
(x,y,w,n)=>(g=i=>i&&(i/w/x/y^n/w|i/x%w^n%w?0:i)+g(i-1))(x*w*~y*~n)
Input W ÷ X
Silly.
