Skip to main content
deleted 357 characters in body
Source Link
G B
  • 22.8k
  • 1
  • 20
  • 53

Python 2, 3434 28 bytes

lambda n,k:(2*k-(k/n)*nk+k%n)*-~(k/nn+1)/2

Try it online!

###A different approach:Try it online!

I did not want to port my Ruby answer to Python (I knew somebody else would)Thanks Martin Ender, so I started looking for a general formula:

If we call w=k/n then we can decompose the sum into the sum of integers from 1 to n: w*(w+1)/2 repeated n timesNeil and the rest (w+1)*(k-w*n)Mr Xcoder for helping. Add the two terms and simplify: we have the formula!

Python 2, 34 bytes

lambda n,k:(2*k-(k/n)*n)*-~(k/n)/2

Try it online!

###A different approach:

I did not want to port my Ruby answer to Python (I knew somebody else would), so I started looking for a general formula:

If we call w=k/n then we can decompose the sum into the sum of integers from 1 to n: w*(w+1)/2 repeated n times and the rest (w+1)*(k-w*n). Add the two terms and simplify: we have the formula!

Python 2, 34 28 bytes

lambda n,k:(k+k%n)*(k/n+1)/2

Try it online!

Thanks Martin Ender, Neil and Mr Xcoder for helping.

Source Link
G B
  • 22.8k
  • 1
  • 20
  • 53

Python 2, 34 bytes

lambda n,k:(2*k-(k/n)*n)*-~(k/n)/2

Try it online!

###A different approach:

I did not want to port my Ruby answer to Python (I knew somebody else would), so I started looking for a general formula:

If we call w=k/n then we can decompose the sum into the sum of integers from 1 to n: w*(w+1)/2 repeated n times and the rest (w+1)*(k-w*n). Add the two terms and simplify: we have the formula!