18
\$\begingroup\$

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.

Challenge

Write a program/function that takes two non-negative integers n and k and outputs fn(k).

Specifications

  • Standard I/O rules apply.
  • Standard loopholes 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.

f n k = sum $ take k $ replicate n =<< [1..]

Try it online!

This challenge was sandboxed.

\$\endgroup\$
  • \$\begingroup\$ Do you think that my edit improves formatting, or is it just on my browser? \$\endgroup\$ – user202729 Nov 28 '17 at 10:54
  • \$\begingroup\$ @user202729 Heh... it looks off on my browser but I doubt my formatting looked good on most browsers... I'll just keep it like that, it doesn't lose any meaning. Just looks weird. :P \$\endgroup\$ – totallyhuman Nov 28 '17 at 11:06
  • \$\begingroup\$ Do we need to handle the case f_n(0) = 0 for k 0-indexed? \$\endgroup\$ – Cinaski Nov 28 '17 at 12:41
  • 8
    \$\begingroup\$ "cool untitled sequence thingy" Lol, I'm not the only one having a hard time coming up with names for sequences I made up I see.. ;) \$\endgroup\$ – Kevin Cruijssen Nov 28 '17 at 13:17
  • 3
    \$\begingroup\$ @Fabian No, you only sum the first k terms from the list of repeated natural numbers, not the first n*k terms. \$\endgroup\$ – Martin Ender Nov 28 '17 at 18:53

31 Answers 31

0
\$\begingroup\$

Jelly, 4 bytes

R÷ĊS

Try it online!

Takes input in order k, n. Creates an array of ints from 1 to k, divides each by n and then rounds up, and sums.

\$\endgroup\$

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.