# 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. Sep 1, 2016 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, 2016 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.) Sep 1, 2016 at 11:51
• for the Input: N=0,P=3 example, your expansion has some extraneous double-negatives Sep 1, 2016 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. Sep 1, 2016 at 15:12

# Maple, 19 bytes

n-sum(i,i=n+1..n+p)


Usage:

> f:=(n,p)->n-sum(i,i=n+1..n+p);
> f(2, 3);
-10
> f(100,5);
-415
> f(42,0);
42


# Perl 6, 21 bytes

{$^n-[+]$n^..$n+$^p}


## Explanation:

# bare block lambda with two placeholder parameters ｢$n｣ and ｢$p｣
{
$^n - # reduce using ｢&infix:<+>｣ [+] # a Range that excludes ｢$n｣ and has ｢$p｣ values after it$n ^.. ($n +$^p)
}


# 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=. Sep 3, 2016 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. Sep 3, 2016 at 13:16

# Excel, 20 bytes

Subtract the next B1 integers from A1:

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


# GolfScript, 11 bytes

Formula stolen elsewhere...

~1$1$)2/+*-


Try it online!

## Explanation

This just boils down to

A-B*(A+(B+1)/2)


Converted to postfix.

# GolfScript, 15 bytes

~),{1$+}%{-}*\;  Try it online! ## Explanation ~ # Evaluate the input ), # Generate inclusive range {1$+}%       # Add each item by the other input
{-}*   # Reduce by difference
\; # Discard the extra other input


# Keg, -hr, 8 6 bytes

+ɧ^∑\$-


Try it online!

No formula, just a for loop!

## Wd, 6 4 bytes

Surprised to see that it's not short at all. (Although it at least ties with Jelly...)

7▄i←


## Explanation

Uncompressed:

+.@-R
+     % Generate b+a
.    % Generate range(a,b+a)
@-R % Reduce via subtraction


# Japt, 5 bytes

ôV r-


Try it

ôV r-     :Implicit input of integers U=N & V=P
ôV        :Range [U,U+V]
r-     :Reduce by subtraction


# Demsos, 20 bytes

f(n,p)=n-p(p/2+.5+n)


Other 20-byters:

f(n,p)=n-pn-p(p+1)/2
f(n,p)=n-pp/2-.5p-np
f(n,p)=n-.5p(2n+p+1)
f(n,p)=n-pn-.5pp-.5p


Try it on Desmos!

With the sum function, it's 27 bytes:

f(n,p)=n-∑_{x=n+1}^{n+p}x


# K (ngn/k), 10 bytes

{-/x+!y+1}


Try it online!

Quick.

# TI-Basic, 17 bytes

Prompt N,P
N-NP-.5(P²+P


Alternatives:

Prompt N,P
N-P(P/2+.5+N

Prompt N,P
N-NP-.5P(P+1

Prompt N,P
N-.5P(2N+P+1


# APL (Dyalog Unicode), 12 bytes

⊣-(+/(⍳⊢)+⊣)


Try it online!

• ⊣-1⊥+∘⍳ uses 1⊥ to sum and rearranges (⍳⊢)+⊣ to +∘⍳ Dec 21, 2020 at 17:09
• @rak1507 at first I thought {⍺-+/⍺+⍳⍵}, but yours seems smaller Dec 21, 2020 at 17:11

# Python 3, 43 bytes

lambda n,p:n-sum([n+i+1 for i in range(p)])


Try it online!

Explanation:

lambda n,p:                                  # Lambda header
[n+i+1 for i in range(p)]   # Next P numbers
sum(                          ) # Sum each number
n-                                # Subtract from N for final result

• My initial answer was 46 bytes, then i shaved off 2 bytes by changing the comprehension, then 1 more byte was shaved off by turning -s into +s and vice versa. May 14 at 13:41
• I think you can remove the brackets May 16 at 15:56

# Ly, 14 bytes

n0ns-0Rl0I*-&+


Try it online!

This relies on the fact that for X=starting number and Y=iterator, the answer is:

X - (X*Y) - sum([1..Y])

It uses the R command to generate the range of negative numbers.

n              - read starting number onto the stack
0n            - push "0", read iteration count onto the stack
s           - save iteration count
-          - convert to negative number
0         - push "0" to set ending number
R        - generate a range of negative numbers
l       - load the iterator count
0I     - copy the starting number
*    - multiple to get base number common to all decrements
-   - convert to negative (relies on "0" from range)
&+ - sum the stack, answer prints automatically