[Question]
  [
Let's define **fn(k)** as the sum of the first **k** terms of the natural numbers **[1, ∞)** where each number is repeated **n** times.
```
**k**       | **0 1 2 3 4 5 6 7 8 9**
--------+-------------------------------------------------
**f\_1(k)**  | 0    1    3    6    10   15   21   28   36   45
**deltas**  |   +1   +2   +3   +4   +5   +6   +7   +8   +9
--------+-------------------------------------------------
**f\_2(k)**  | 0    1    2    4    6    9    12   16   20   25
**deltas**  |   +1   +1   +2   +2   +3   +3   +4   +4   +5
--------+-------------------------------------------------
**f\_3(k)**  | 0    1    2    3    5    7    9    12   15   18
**deltas**  |   +1   +1   +1   +2   +2   +2   +3   +3   +3
```
The anti-diagonals of this as a square array is similar to [OEIS sequence A134546](https://oeis.org/A134546 "A134546 - OEIS").
## Challenge
Write a **program/function** that takes **two non-negative integers n and k** and outputs **fn(k)**.
### Specifications
* [Standard I/O rules](https://codegolf.meta.stackexchange.com/questions/2447/default-for-code-golf-input-output-methods "Default for Code Golf: Input/Output methods") **apply**.
* [Standard loopholes](https://codegolf.meta.stackexchange.com/questions/1061/loopholes-that-are-forbidden-by-default "Loopholes that are forbidden by default") are **forbidden**.
* Your solution **can either be 0-indexed or 1-indexed for n and/or k** but please specify which.
* This challenge is not about finding the shortest approach in all languages, rather, it is about finding the **shortest approach in each language**.
* Your code will be **scored in bytes**, usually in the encoding UTF-8, unless specified otherwise.
* Built-in functions that compute this sequence are **allowed** but including a solution that doesn't rely on a built-in is encouraged.
* Explanations, even for "practical" languages, are **encouraged**.
### Test cases
In these test cases, **n** is 1-indexed and **k** is 0-indexed.
```
**n k fn(k)**
1   2      3
2   11     36
11  14     17
14  21     28
21  24     27
24  31     38
31  0      0
```
In a few better formats:
```
1 2
2 11
11 14
14 21
21 24
24 31
31 0
1, 2
2, 11
11, 14
14, 21
21, 24
24, 31
31, 0
```
### Reference implementation
This is written in [Haskell](https://www.haskell.org/ "Haskell Language").
```
f n k = sum $ take k $ replicate n =<< [1..]
```
[Try it online!](https://tio.run/##jZAxbsMwDEV3n@IPWQo0BuR6TE5SdGBtqhZsyYFEI1foKXK4XMSl46QoBLjt30S@/ymyo9TzMMyzRUCPI9LksYNQz/rcIfJpcA0Ja/t4OODVlOXbvN/jwf9PLVsXGAQ7hUbcGNQvHcltUIKcR1D8mDwHSRpMoUVf6JSH1l/9pYUabVmWP62rvhfakHQM62IShYSjT1tBm7pdZg0KJFOkAWHy7xyTtp5x/bw8/R6Yn/quc8eRwdR09zy4tLCsZKuoOM@pKDw5taEdi8V0ii6IbmthUGWVCsbkkIGp81qNKucqTcu5qsaLmb8A "Haskell – Try It Online")
[This challenge was sandboxed.](https://codegolf.meta.stackexchange.com/a/14260/ "Sandbox for Proposed Challenges")
  
[Answer]
# [Ruby](https://www.ruby-lang.org/), ~~32 28~~ 23 bytes
```
->n,k{k.step(0,-n).sum}
```
[Try it online!](https://tio.run/##FYkxDoQgFAV7T0GHax7E/6VdL0J@oYk2RkMWKTbq2RGqmcn80vzP6zeb8cB2bTaeS2h7mONjY9qf7D2BBZ5BVEAEcpUOXJvLrc0OA4nYfQrXPWG@m5DOqHxRaDNqqLW6SKeVbp78Ag "Ruby – Try It Online")
### Explanation
Let's visualize the sum as the area of a triangle, for example with n=3 and k=10:
```
*
*
*
**
**
**
***
***
***
****
```
Then we sum by column instead of row: the first column is `k`, then `k-n`, `k-2n` and so on.
[Answer]
# [Python 2](https://docs.python.org/2/), ~~34~~ 28 bytes
```
lambda n,k:(k+k%n)*(k/n+1)/2
```
[Try it online!](https://tio.run/##FYvBCoMwEER/JRcx0Smya05C@yNpDoqIknYN4qVfn2ZPM483k3/3fgqX7fkun/m7rLMRpMmmPjXiOpsG6ckNdXBeaswhJgQCRwQGUQ0ikNf0YGWuVpk9Ropxytcht37RPl4tNlurK38 "Python 2 – Try It Online")
Thanks Martin Ender, Neil and Mr Xcoder for helping.
[Answer]
## [Husk](https://github.com/barbuz/Husk), 4 bytes
```
Σ↑ṘN
```
[Try it online!](https://tio.run/##yygtzv7//9ziR20TH@6c4ff//38jk//GhgA "Husk – Try It Online")
### Explanation
This just ends up being a direct translation of the reference implementation in the challenge:
```
   N  Start from the infinite sequence of all natural numbers.
  Ṙ   Replicate each element n times.
 ↑    Take the first k values.
Σ     Sum them.
```
[Answer]
# [APL (Dyalog)](https://www.dyalog.com/), ~~12~~ ~~10~~ 8 bytes
```
+/∘⌈÷⍨∘⍳
```
[Try it online!](https://tio.run/##SyzI0U2pTMzJT///P@1R2wRt/UcdMx71dBze/qh3BYjZu/n/fw0gaWigqQDk66UpGOiAuQA "APL (Dyalog Unicode) – Try It Online")
`n` on the left, `k` (0 indexed) on the right.
[Answer]
# Mathematica, 40 bytes
```
Tr@Sort[Join@@Range@#2~Table~#][[;;#2]]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P6TIITi/qCTaKz8zz8EhKDEvPdVB2aguJDEpJ7VOOTY62tpa2Sg2Vu1/QFFmXolDWrSRiY6xYex/AA "Wolfram Language (Mathematica) – Try It Online")
```
Tr[Range@(s=⌊#2/#⌋)]#+#2~Mod~#(s+1)&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P6QoOigxLz3VQaPY9lFPl7KRvvKjnm7NWGVtZaM63/yUOmWNYm1DTbX/AUWZeSUOadFGJjrGhrH/AQ "Wolfram Language (Mathematica) – Try It Online")
# Mathematica, 18 bytes
**by Martin Ender**
```
Tr@Range[#2,0,-#]&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P6TIISgxLz01WtlIx0BHVzlW7X9AUWZeiUNatJGJjrFh7H8A "Wolfram Language (Mathematica) – Try It Online")
```
n~Sum~{n,#2,0,-#}&
```
[Try it online!](https://tio.run/##y00syUjNTSzJTE78n2b7P68uuDS3rjpPR9lIx0BHV7lW7X9AUWZeiUNatKGOUex/AA "Wolfram Language (Mathematica) – Try It Online")
[Answer]
# [MATL](https://github.com/lmendo/MATL), ~~12~~ 11 bytes
```
:iY"Ys0h1G)
```
[Try it online!](https://tio.run/##y00syfn/3yozUimy2CDD0F3z/39DQy4jAA "MATL – Try It Online")
`k` is 0-indexed.
Takes input in reverse order.
Saved 1 byte thanks to @Giuseppe
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 5 bytes
```
Rxḣ³S
```
One more byte than @Mr.Xcoder's Jelly solution but this is my first ever submission in Jelly and I'm still confused about how the tacitness of Jelly chooses operands so I'm still satisfied. Note the order of the inputs are `k` then `n`.
**Explanation**
```
Rxḣ³S
R           Range: [1,2,...,k]
 x          Times: repeat each element n times: [1,1,1,2,2,2,...,n,n,n]
  ḣ³        Head: take the first k elements. ³ returns the first argument.
    S       Sum
```
[Try it online!](https://tio.run/##y0rNyan8/z@o4uGOxYc2B////9/Q5L@hIQA "Jelly – Try It Online")
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 4 bytes
**1-indexed**
```
Ḷ:‘S
```
[Try it online!](https://tio.run/##y0rNyan8///hjm1WjxpmBP///9/Q8L8RAA "Jelly – Try It Online") or see a [test suite](https://tio.run/##y0rNyan8///hjm1WjxpmBP@PjjbUMYrViTbSMTQEUoaGOoYmINpExwjENwLKgvhGJjrGhrGxoYfb9XVUHjWtcQPiUPf/AA).
[Answer]
# JavaScript (ES6), ~~24~~ 21 bytes
Takes input in currying syntax `(n)(k)`. Returns [`false` instead of `0`](https://codegolf.meta.stackexchange.com/questions/9064/should-booleans-be-allowed-where-a-number-is-required/9067#9067).
```
n=>g=k=>k>0&&k+g(k-n)
```
### Test cases
```
let f =
n=>g=k=>k>0&&k+g(k-n)
console.log(f(1 )(2 )) // 3
console.log(f(2 )(11)) // 36
console.log(f(11)(14)) // 17
console.log(f(14)(21)) // 28
console.log(f(21)(24)) // 27
console.log(f(24)(31)) // 38
```
### How?
```
n =>             // main unamed function taking n
  g = k =>       // g = recursive function taking k
    k > 0 &&     // if k is strictly positive:
      k +        //   add k to the final result
      g(k - n)   //   subtract n from k and do a recursive call
```
This is similar to [@GB's Ruby answer](https://codegolf.stackexchange.com/a/149355/58563).
The challenge describes how to build the 'staircase' from left to right, whereas this recursive function builds it from bottom to top. With **n = 2** and **k = 11**:
[![staircase](https://i.stack.imgur.com/3HRFi.png)](https://i.stack.imgur.com/3HRFi.png)
[Answer]
## Batch, 34 bytes
```
@cmd/cset/a(%2+%2%%%1)*(%2/%1+1)/2
```
A closed-form formula that I found. First argument `n` is 1-indexed, second argument `k` is 0-indexed.
[Answer]
# [Python 2](https://docs.python.org/2/), 29 bytes
```
lambda n,k:sum(range(k,0,-n))
```
[Try it online!](https://tio.run/##FYtBDoIwEEWv0h1t8kmcsSsSvUjtogSrBBlIwYWnr53V@y8vf/@d70245tujftI6TskIluH4rrYkeT3tggt6ca7mrWgys5gQCBwRGEQNRCCv9GB1blWdPa4U47CXWU79ouvvHbJt09U/ "Python 2 – Try It Online")
Thanks to [totallyhuman](https://codegolf.stackexchange.com/users/68615/totallyhuman) for -3 bytes!
---
### [Python 2](https://docs.python.org/2/), 30 bytes
```
f=lambda n,k:k>0and k+f(n,k-n)
```
[Try it online!](https://tio.run/##HYxBCoQwEAS/kpsrtuCMOQmbj4QcIhKU7I4iuezrsxlPRVF0X7@yn8K1pvcnftctGkFespuibCYP6dV0lL6m89ZiDjHeEzjAM4gaiEBWacHq3Ko6W8wUwnLdhxTdohtdh@exr38 "Python 2 – Try It Online")
[Answer]
# [Haskell](https://www.haskell.org/), 28 bytes
```
n#k|m<-k`mod`n=sum[m,m+n..k]
```
[Try it online!](https://tio.run/##y0gszk7Nyfn/P085uybXRjc7ITc/JSHPtrg0NzpXJ1c7T08vO/Z/bmJmnoKtQko@lwIQFBRl5pUoqCgYKigrGKGIGAFFDA1RFYFUGZqgipmAdKKqMwKbhqrOCKTO2PA/AA "Haskell – Try It Online")
An approach I found just by screwing around with some range parameters. Most definitely not the shortest but it's pretty cool how there's so many different approaches.
[Answer]
# C, ~~38~~ 34 bytes
Recursive definition.
*-4 bytes thanks to [Steadybox](https://codegolf.stackexchange.com/users/61405/steadybox).*
```
f(n,k){return k--?1+f(n,k)+k/n:0;}
```
[Try it online!](https://tio.run/##RcpBCsIwEIXhdXOKISDMMCk2bgRT8SwSjaTBWGK6Kj17HHHh6oP/Pd8/vG8tYDaJ1nKvS8mQ@v5i@dc47fNpcFuLucLzGjOSWlUXXgXwmyY4g3XCCEeB2cC81DdqTaS6/y/Kb3DCCPYgMhPMRZaAencDbSDgZCASObW1Dw "C (gcc) – Try It Online")
---
**32 bytes by [Mr. Xcoder](https://codegolf.stackexchange.com/users/59487/mr-xcoder), [G B](https://codegolf.stackexchange.com/users/18535/g-b)**
```
f(n,k){return(k+k%n)*(k/n+1)/2;}
```
[Try it online!](https://tio.run/##RcpBCsIwEIXhdXOKIVCYcSI13XSRehiJRtLgWGK6Kj17jCtXH7z3@/PT@1oDikm050fZsmDi1AudMA3ClobRHTVKgdctCpLaVRfeGfA3LXAF6xozTA1mA@tWPqg1ker@XWzdxTVmsGOTmWDN7Qmo@ztoAwEXA5HIqaN@AQ "C (gcc) – Try It Online")
[Answer]
# [R](https://www.r-project.org/), ~~37~~ ~~33~~ 31 bytes
-6 bytes thanks to Giuseppe
```
function(n,k)sum(rep(1:k,,k,n))
```
[Try it online!](https://tio.run/##K/qfZvs/rTQvuSQzP08jTydbs7g0V6MotUDD0CpbRydbJ09T839yYomGQpqGkY6hoaaOkoKtQlq8kQaQHZOnpKDJBZU1NtQxgMkaG2oYoMoamegYIzSbAFVD5P8DAA "R – Try It Online")
Nothing fancy. ~~The `[0:k]` handles the case when k=0.~~
[Answer]
# C++, 53 bytes
Just use the formula. `n` is 1-indexed and `k` is 0-indexed.
```
[](int n,int k){return k/n*(k/n+1)/2*n+k%n*(k/n+1);};
```
[Try it online!](https://tio.run/##RY3LDoIwFETX9ismMQZqMYgbFy3@iHHRlELK40KgXRG/HYsL3cyZm5zcMdN0aYzZdPAjapTb85U68qBsz46vs/VhJnQ5ndMYouD57UyiO/1u@Zbb0ZHpQ2Wh3Lj42erhwVhYHDUgPdhl0sZi8ZVkbP87aEcpZys71OOM72CLEoWMULhHCJHBjMFDKViqes4Of9dF9yojFIpbpBD8J9dpm8HxvSZIJHtvHw "C++ (gcc) – Try It Online")
[Answer]
# [J](http://jsoftware.com/), 13 bytes
```
1#.]{.(#1+i.)
```
How it works:
The left argument is n, the right is k.
`i.` generates a list 0..k-1
`1+` adds one to each number of the list, yealding 1,2,...,k
`#` forms a hook with the above, so n copies of each elements of the list are copied. 
`]{.` take the first n of them
`1#.` find their sum by base conversion.
[Try it online!](https://tio.run/##y/qfVmyrp2CgYKVg8N9QWS@2Wk9D2VA7U0/zvyaXkp6CepqtnrqCjkKtlUJaMRdXanJGvoKhQpqCEYRpBGQaGkKFQeKGJlCOCUgRVMYIrAMqYwSSMTb8DwA "J – Try It Online")
[Answer]
# [Retina](https://github.com/m-ender/retina), ~~29~~ 26 bytes
```
\d+
$*
(?=.*?(1+)$)\1
$'
1
```
[Try it online!](https://tio.run/##HYk5DoAwEAN7vyLFInKskLykRflICpCgoKFA/D8Eqpmx7@M5r60NfvV1T0HdD4ipsHWFRPiyTLF4piChEjKCrVGdwdSRYHdmMPeJsO/JsF4zXw "Retina – Try It Online") Link includes test cases and header to reformat them to its preferred input (0-indexed `k` first, 1-indexed `n` second). I was inspired by @GB's Ruby answer. Explanation:
```
\d+
$*
```
Convert to unary.
```
(?=.*?(1+)$)\1
$'
```
Match every string of `n` within `k`, and replace the match with everything after the match. This is `k-n`, `k-2n`, `k-3n`, but `n` is also after the match, so you get `k`, `k-n`, `k-2n` etc. This also matches `n`, which is simply deleted (it's no longer needed).
```
1
```
Sum the results and convert back to decimal.
[Answer]
# [Pyth](https://github.com/isaacg1/pyth), 5 bytes
```
s%_ES
```
**[Try it here!](https://pyth.herokuapp.com/?code=s%25_ES&input=11%0A2&debug=0)**
Port of G B's Ruby answer. A port of my Jelly one would be 6 bytes: `+s/Rvz`
[Answer]
# [Perl 6](https://perl6.org), 39 bytes
```
->\n,\k{(0,{|($_+1 xx n)}...*)[^k].sum}
```
[Test it](https://tio.run/##NY5BT4NAEIXv8yveodpdoVsWDG1C2nj17k1so7BNGmAh3a3BIL/Mm38MWaiTTPK9vHkz06hLGQ9Xo/AZiyyh6gv3WZ0r7IbVPtV@WnQs8Ltvtjh6Em0LzXshxAN/PRRvwlyrfhgjT1YZix3Ks1aG8d8fkdXVB1unubd26lnbhNyRl3EuoaZ81/CmUEKn@nLLr/ZYHMEwnkVajK3aRmVW5eDoCDgbuNcYRr@ABwnu43/GhzhVli3vIrPk1A8SQIipInIg5SxiciQfJyU35CicvXBLjsLZCzfkKLrltuQomFcGfw "Perl 6 – Try It Online")
***n*** and ***k*** are both 1 based
## Expanded:
```
-> \n, \k { # pointy block lambda with two parameters ｢n｣ and ｢k｣
  ( # generate the sequence
    0,         # seed the sequence (this is why ｢k｣ is 1-based)
    {          # bare block lambda with implicit parameter ｢$_｣
      |(       # slip this into outer sequence
        $_ + 1 # the next number
        xx n   # repeated ｢n｣ times (this is why ｢n｣ is 1-based)
      )
    }
    ...        # keep doing that until
    *          # never stop
  )[ ^k ]      # get the first ｢k｣ values from the sequence
  .sum         # sum them
}
```
[Answer]
# [Kotlin](https://kotlinlang.org), 40 bytes
```
{n:Int,k:Int->Array(k,{i->i/n+1}).sum()}
```
[Try it online!](https://tio.run/##NY1BCoMwEEX3nmIQFxk6tk10Ja3QZVct9ATZREJ0LFELRTx7qtLMYng8HnzXj63l8NEtmGuYubrzSG77eX3zXn@Fo9nmtT3xQS54HKZO4BLMxNBpy0L7ZqhgLy@v0VtuaoQ5gfVM70EIJodgGfSWPIx4auuFJIW0kyIp/yglyTJySSp6tdbRq5KK6AtJZ0SE97o6tizSjClzeZ3NZl9dUkyW8AM "Kotlin – Try It Online")
[Answer]
# [Java (OpenJDK 8)](http://openjdk.java.net/), 23 bytes
```
n->k->(k+k%n)*(k/n+1)/2
```
[Try it online!](https://tio.run/##VY9La8MwEITv@RVLwCAntlMruaUx9FIopukh9FRCUf0I8kMW0jpgSn67u3ZciA9Cq@Gb0U4hrsJvdKaKtOxlrRuDUJAWtCirIG9VgrJRwWq/0O1PJRNIKmEtvAup4HcBYFEgqddGpoCZRSYVwtGD4YrdEQF4U/g6JT3T/KmE6T50ZgQ2JoL80Cs/Kv2IlevSUe6KlRu1Dt0N7/ejfcgymW0rhAPkgdC66tjRvQ8vlhJZ7N7RU2cxq4OmxUAb8uVsmX87KXNSl7xO6qilN6wXe1Pi6LvRmeo99qmpJDsh5Vy@ziDMxf4XGpuGHp9@HZ/cgzB8FMKQlN1M2XnAZwwnhs8YTsx2xmyJeZr2vPV/ "Java (OpenJDK 8) – Try It Online")
Port of [G B's Python 2 answer](https://codegolf.stackexchange.com/a/149365/16236).
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E), 9 bytes
```
FL`}){I£O
```
[Try it online!](https://tio.run/##MzBNTDJM/f/fzSehVrPa89Bi////jbgMDQE "05AB1E – Try It Online")
**Explanation**
```
F           # n times do
 L`         # pop top of stack (initially k), push 1 ... topOfStack
   }        # end loop
    )       # wrap stack in a list
     {      # sort the list
      I£    # take the first k elements
        O   # sum
```
[Answer]
# [Python 2](https://docs.python.org/2/), 44 bytes
```
f=lambda n,k:sum(sorted(range(1,k+1)*n)[:k])
```
[Try it online!](https://tio.run/##K6gsycjPM/r/P802JzE3KSVRIU8n26q4NFejOL@oJDVFoygxLz1Vw1AnW9tQUytPM9oqO1bzf0FRZl6JQhpQ2EiTC8Yx0jE0RPAMDXUMTZC4JjpGSLJGQJ1IskYmOsaGmv8B "Python 2 – Try It Online")
[Answer]
# [Python 2](https://docs.python.org/2/), 38 bytes
```
lambda n,k:sum(i/n+1for i in range(k))
```
[Try it online!](https://tio.run/##FYuxDsIwEEN/JVsTYYTuyFQJfiRkCIJAVHqt0jLw9SE32X6219/@XoRbvtzaJ833RzKCady@sy0nOVBeqimmiKlJXk87OdcU9Y3CEAgcERhEXYhAXtWDNXNvNbPHmWIc11pk1y@G43VAtt269gc "Python 2 – Try It Online")
[Answer]
## Clojure, 54 bytes
```
#(nth(reductions +(for[i(rest(range))j(range %)]i))%2)
```
2nd argument `k` is 0-indexed, so `(f 14 20)` is 28.
[Answer]
# APL+WIN, 13 bytes
```
+/⎕↑(⍳n)/⍳n←⎕
```
Prompts for screen input for n and then for k. Index origin = 1.
[Answer]
# [Brain-Flak](https://github.com/DJMcMayhem/Brain-Flak), 78 bytes
```
(({}<>)<{<>(({})<>){({}[()]<(({}))>)}{}({}[()])}{}<>{}>)({<({}[()])><>{}<>}{})
```
[Try it online!](https://tio.run/##SypKzMzTTctJzP7/X0OjutbGTtOm2sYOxNQEsquBdLSGZqwNWEDTTrO2uhYqBGLa2FXX2mlqVNvAxOxAIjZ2QCnN//9NFUwA "Brain-Flak – Try It Online")
I'm certain this can be done better, but it's a start.
[Answer]
# [Japt](https://github.com/ETHproductions/japt/), ~~7~~ 6 bytes
Originally inspired by [GB's solution](https://codegolf.stackexchange.com/a/149355/58974) and evolved to a port!
Takes `k` as the first input and `n` as the second.
```
õ1Vn)x
```
[Try it](https://ethproductions.github.io/japt/?v=1.4.5&code=9TFWbil4&input=MzEsMjQ=)
---
## Explanation
Implicit input of integers `U=k` & `V=n`. Generate an array of integers (`õ`) from `1` to `U` with a step of `V` negated (`n`) and reduce it by addition (`x`).
[Answer]
# [R](https://www.r-project.org/), 27 bytes
Anonymous function that takes `k` and `n` in that order. Creates a list of length `k` (third argument to `rep`) that is composed of `1` through `k` (first argument to `rep`), repeating each element `n` times (fourth argument to `rep`). Then takes the sum of that list.
`n` is 1-indexed and `k` is 0-indexed. Returns an error for `n<1`.
```
pryr::f(sum(rep(1:k,,k,n)))
```
[Try it online!](https://tio.run/##K/r/v6CossjKKk2juDRXoyi1QMPQKltHJ1snT1NT8/9/AA "R – Try It Online")
[Answer]
# Befunge, 27 Bytes
```
&::00p&:10p%+00g10g/1+*2/.@
```
[Try It Online](https://tio.run/##S0pNK81LT/3/X83KysCgQM3K0KBAVdvAIN3QIF3fUFvLSF/P4f9/Q0MFIwA)
Takes k then n as input. Uses G B's answer as its mathematical basis.
]