# 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
• The essential challenge here is calculating triangle numbers. – Peter Taylor Sep 1 '16 at 11:41
• There'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 Sep 1 '16 at 11:45
• 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 Sep 1 '16 at 11:51
• for the Input: N=0,P=3 example, your expansion has some extraneous double-negatives – turbulencetoo Sep 1 '16 at 14:36
• @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 Sep 1 '16 at 15:12

# Java 8, 25 bytes

(n,p)->n-(p*n+p*(p+1)/2);


# Ungolfed test program

public static void main(String[] args) {

BiFunction<Integer, Integer, Integer> f = (n, p) -> n - (p * n + p * (p + 1) / 2);
System.out.println(f.apply(100, 5));
}


# Scala, 41 bytes

def?(n:Int,p:Int)=n-(1 to p).map{n+_}.sum


Testing code:

println(?(2,3))
println(?(100,5))
println(?(42,0))
println(?(0,3))
println(?(0,0))

// Output
-10
-415
42
-6
0


# Clojure/Clojurescript, 30 bytes

#(reduce -(range %(+ 1 % %2)))


The straightforward approach is shorter than the mathematically clever ones.

# Julia: 17-18 bytes (13 as program with terminal inputs)

As per suggestion in comments that "function or program" form is needed:

• as function: 17 characters, 18 bytes if you count ∘ as multibyte

n∘r=2n-sum(n:n+r)


usage: 5∘3 outputs -16

• as program, passed initial parameters from terminal: 13 bytes:

2n-sum(n:n+r)


usage: julia -E 'n,r=5,3;include("codegolf.jl")'

• Nice solution, but all submissions need to be callable functions or full programs not just snippets. I think the shortest fix would be to define it as a binary operator by prepending n\r=. – Martin Ender Sep 3 '16 at 9:04
• Thanks, I edited to that effect. \  is a bad choice because it needs to be explicitly imported, although I suppose this is the kind of thing that could fall under a gray area. (but if I were allowed to do that, I might as well say Σ(n:n+r) where Σ is an alias for sum :p ). An "initialise and call" from the terminal feels like a bit of a cheat, but again it's a gray area, since other submissions do this without problem since it's considered the only way to call the program. – Tasos Papastylianou Sep 3 '16 at 13:16

# Excel, 20 bytes

Subtract the next B1 integers from A1:

=A1-B1*(A1+(B1+1)/2)