Subtract the next numbers
Python 2, 26 24 23 bytes
-2 bytes thanks to @Adnan (replace p*(p+1)/2
with p*-~p/2
)
-1 byte thanks to @MartinEnder (replace -p*-~p/2
with +p*~p/2
lambda n,p:n-p*n+p*~p/2
Tests are on ideone
05AB1E, 5 3 bytes
Saved 2 bytes thanks to Adnan
Ý+Æ
Explanation
Takes P then N as input.
# implicit input, ex 5, 100
Ý # range(0,X): [0,1,2,3,4,5]
+ # add: [100,101,102,103,104,105]
Æ # reduced subtraction: 100-101-102-103-104-105
CJam, 8 bytes
{),f+:-}
Test suite.
Too bad that the closed form solution is longer. :|
Explanation
), e# Get range [0 1 ... P].
f+ e# Add N to each value to get [N N+1 ... N+P].
:- e# Fold subtraction over the list, computing N - (N+1) - (N+2) - ... - (N+P).