27
1
Description
Subtract the next P numbers from a N number. The next number of N is N + 1.
Look at the examples to get what I mean.
Examples:
Input: N=2,P=3
Calculate: n - (n+1) - (n+2) - (n+3) //Ending with 3, because P=3
Calculate: 2 - 2+1 - 2+2 - 2+3 //Replacing N with 2 from Input
Calculate: 2 - 3 - 4 - 5
Output: -10
Input: N=100,P=5
Calculate: n - (n+1) - (n+2) - (n+3) - (n+4) - (n+5)
Calculate: 100- 101 - 102 - 103 - 104 - 105
Output: -415
Input: N=42,P=0
Calculate: n
Calculate: 42
Output: 42
Input: N=0,P=3
Calculate: n - (n+1) - (n+2) - (n+3)
Calculate: 0 - 1 - 2 - 3
Output: -6
Input: N=0,P=0
Calulate: n
Calculate: 0
Output: 0
Input:
N: Integer, positive, negative or 0
P: Integer, positive or 0, not negative
Output:
Integer or String, leading 0 allowed, trailing newline allowed
Rules:
- No loopholes
- This is code-golf, so shortest code in bytes wins
- Input and Output must be as described
1The essential challenge here is calculating triangle numbers. – Peter Taylor – 2016-09-01T11:41:27.687
4There's more to this than just triangular numbers; the start point is arbitrary as well as the number of subtractions, which may be zero. – JDL – 2016-09-01T11:45:41.440
Also, for triangular numbers it's possible that doing the actual sum is shorter than using the closed form, whereas you can't just compute arbitrary polygonal numbers by summing a range from 0 to N. (I'd agree with the close vote if the other challenge just asked for triangular numbers.) – Martin Ender – 2016-09-01T11:51:01.323
1for the
Input: N=0,P=3
example, your expansion has some extraneous double-negatives – turbulencetoo – 2016-09-01T14:36:21.1001@JDL, the part which is "more than just triangle numbers" is a simple multiplication:
N * (P-1)
. That's virtually the definition of trivial. – Peter Taylor – 2016-09-01T15:12:56.523why are there double minuses for N=0,P=3? Typo, right? – Tasos Papastylianou – 2016-09-02T09:52:57.540
@MartinEnder, someone on meta found an even closer duplicate.
– Peter Taylor – 2016-09-05T13:39:18.783