g | x | w | all
Bytes Lang Time Link
011APLNARS251001T145913ZRosario
009Dyalog APL250930T203320ZAaron
027AWK250930T152851Zxrs
005MATL160901T123609ZLuis Men
017TIBasic220515T145415ZYouserna
014Ly220514T180301Zcnamejj
043Python 3220514T133541ZJoao-3
004Vyxal220428T002034ZnaffetS
010K ngn/k220507T034032Zoeuf
020Demsos220506T164849ZnaffetS
005Japt201221T104233ZShaggy
012APL Dyalog Unicode201221T092538ZKamila S
064W d200114T063259Zuser8505
006Keg200111T215015Zlyxal
012Burlesque200113T142648ZDeathInc
011GolfScript200111T131828Zuser8505
020Excel170927T135312ZWernisch
034Fourier160901T110752ZBeta Dec
nanJulia 1718 bytes 13 as program with terminal inputs160903T084633ZTasos Pa
030Clojure/Clojurescript160902T203708ZMattPutn
041Scala160902T152947ZAmazingD
040Java 7160901T130358ZKevin Cr
063Java160901T120616Zpeech
025Java 8160902T123342ZShaun Wi
020C#160901T142305Zadrianmp
018Haskell160901T132221ZDamien
051C++160901T120514ZVolAnd
036Forth160901T213408Zmbomb007
021Perl 6160901T194034ZBrad Gil
006Pyth160901T190552ZMaltysen
004Jelly160901T190105ZDennis
019Maple160901T185736ZDSkoog
007Jelly160901T183624ZJonathan
033PHP160901T160027ZJör
033C89160901T152751ZYSC
038php160901T154000Zuser5917
018Javascript ES6160901T112636ZArnauld
022Perl160901T115850ZTon Hosp
011Pyth160901T131155ZTheBikin
017Brachylog160901T121436ZFatalize
015Labyrinth160901T122755ZMartin E
023Python 2160901T115852ZJonathan
014R160901T114137ZJDL
021Haskell160901T120952ZThreeFx
015Mathematica160901T120813ZMartin E
00305AB1E160901T115524ZEmigna
006Pyke160901T115343ZBlue
008CJam160901T113437ZMartin E
080S.I.L.O.S160901T111921Zbetseg
030Batch160901T110753ZNeil

APL(NARS), 11 chars

{+/⍺,-⍺+⍳⍵}

test:

  f←{+/⍺,-⍺+⍳⍵}
  2 f 3
¯10
  100 f 5
¯415
  42 f 0
42
  0 f 3
¯6
  0 f 0
0

Dyalog APL, 9 bytes

Slightly different than the other APL solution, just got to avoid an extra set of parens. N on the left, P on the right

+/⊣,-⍤+∘⍳­⁡​‎‎⁡⁠⁢⁡‏⁠‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣⁡‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁣‏⁠‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁡‏⁠‎⁡⁠⁢‏‏​⁡⁠⁡‌­
    -⍤     # ‎⁡Negate after sending both args to these next functions for preprocessing
      +∘   # ‎⁢  add after sending the just the right arg to the next function for preprocessing
        ⍳  # ‎⁣    get range of 1..P, P being the right arg
  ⊣,       # ‎⁤Prepend the left arg to this list
+/         # ‎⁢⁡and sum
💎

Created with the help of Luminespire.

AWK, 27 bytes

1,$0=$1-($1*$2+$2*($2+1)/2)

Attempt This Online!

MATL, 5 bytes

:y+s-

Inputs are P and then N.

Try it at MATL Online!

Explanation

:     % Take P implicitly. Range [1 2 ... P]
      %     Stack: [1 2 ... P]
y     % Take N implicitly at the bottom of the stack, and push another copy
      %     Stack: N, [1 2 ... P], N
+     % Add the top two arrays in the stack , element-wise
      %     Stack: N, [N+1 N+2 ... N+P]
s     % Sum of array
      %     Stack: N, N+1+N+2+...+N+P
-     % Subtract the top two numbers
      %     Stack: N-(N+1+N+2+...+N+P)
      % Implicitly display

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

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

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

Vyxal, 4 bytes

+ṡƒ-

Try it Online!

Port of Jelly answer.

How?

+ṡƒ-
+ṡ   # Inclusive range [a, a + b]
  ƒ- # Reduce by subtraction

K (ngn/k), 10 bytes

{-/x+!y+1}

Try it online!

Quick.

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

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

APL (Dyalog Unicode), 12 bytes

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

Try it online!

W d, 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

Keg, -hr, 8 6 bytes

+ɧ^∑$-

Try it online!

No formula, just a for loop!

Burlesque, 12 bytes

perz?+{.-}r[

Try it online!

pe     #Read input as P, N
rz     #Range (0,N)
?+     #Add P to each
{.-}r[ #Reduce via subtraction

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

Excel, 20 bytes

Subtract the next B1 integers from A1:

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

Fourier, 34 bytes

I~No~OI~P>0{1}{@P+N(N^~NO-N~ON)Oo}

Try it online!

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

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

Clojure/Clojurescript, 30 bytes

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

The straightforward approach is shorter than the mathematically clever ones.

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

Java 7, 43 40 bytes

int c(int n,int p){return n-p*n+p*~p/2;}

Java 8, 19 bytes

(n,p)->n-p*n+p*~p/2

Shamelessly stolen from @JonathanAllan's amazing Python 2 formula.

Original answer (61 60 bytes):

int c(int n,int p){int r=n,i=1;for(;i<p;r-=n+++i);return r;}

Ungolfed & test cases:

Try it here.

class M{
  static int c(int n, int p){
    return n - p*n + p*~p / 2;
  }

  public static void main(String[] a){
    System.out.println(c(2, 3));
    System.out.println(c(100, 5));
    System.out.println(c(42, 0));
    System.out.println(c(0, 3));
    System.out.println(c(0, 0));
  }
}

Output:

-10
-415
42
-6
0

Java, 67, 63 bytes

Golfed:

int x(int n,int p){return-((p%2<1)?p*p/2+p:p/2*(p+2)+1)+n-p*n;}

Ungolfed:

int x(int n, int p)
{
    return -((p%2<1) ? p*p/2+p : p/2 * (p+2) + 1) + n - p*n;
}

Basically I did some math on the formula. The n - p*n part takes care of the all n's in the formula. Then I used a super fun property of summing together linearly increasing set of integers (arithmetic series): I used the sum of first and last integer and then multiply it by set.length / 2 (I also check for the parity and handle it appropriately).

Try it: https://ideone.com/DEd85A

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));
}

C#, 21 20 bytes

Edit: Saved one byte thanks to TheLethalCoder

N=>P=>N-P++*(N+P/2);

Try it online!

Full source, including test cases:

using System;

namespace substract
{
    class Program
    {
        static void Main(string[] args)
        {
            Func<int,Func<int,int>>s=N=>P=>N-P++*(N+P/2);
            Console.WriteLine(s(2)(3));     //-10
            Console.WriteLine(s(100)(5));   //-415
            Console.WriteLine(s(42)(0));    //42
            Console.WriteLine(s(0)(3));     //-6
            Console.WriteLine(s(0)(0));     //0

        }
    }
}

Haskell, 19 18 bytes

n#p=n+sum[-n-p..n]

Previous 19 bytes solutions

n#p=n-n*p-(p*p+p)/2
n#p=n-sum[n+1..n+p]

C++, 54 51 bytes

  [](int N,int P){int F=N;while(P--)F-=++N;return F;}

[](int N,int P){int F;for(F=N;P;F-=++N,P--);return F;}

Test:

#include <iostream>
int main(void)
{
    int N, P;
    std::cin >> N >> P;
    auto f = [](int N,int P){int F=N;while(P--)F-=++N;return F;};
    std::cout << f(N,P) << std::endl;
    return 0;
}

Forth, 36 bytes

Simple computation of n - (p*n + (p^2+p) / 2)

: f 2dup dup dup * + 2/ -rot * + - ;

Try it online

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)
}

Pyth - 6 bytes

-F}Q+E

Test Suite.

Jelly, 4 bytes

r+_/

Try it online!

How it works

r+_/  Main link. Arguments: n, p

 +    Yield n+p.
r     Range; yield [n, ..., n+p].
  _/  Reduce by subtraction.

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

Jelly, 7 bytes

RS+×_×-

Arguments are P, N
Test it on TryItOnline

How?

RS+×_×-  - takes two arguments: P, N
R        - range(P): [1,2,3, ... ,P]
 S       - sum: 1+2+3+ ... +P
   ×     - multiply: P*N
  +      - add: 1+2+3+ ... +P + P*N
    _    - subtract: 1+2+3+ ... +P + P*N - N
      -  - -1
     ×   - multiply: (1+2+3+ ... +P + P*N - N)*-1
                   = -1-2-3- ... -P - P*N + N
                   = N - (N+1) - (N+2) - (N+3) - ... - (N+P)

PHP, 33 Bytes

$n-=$n*$p+array_sum(range(0,$p));

C89, 38, 35, 33 bytes

h(n,p,r)int*r;{*r=n-p++*(n+p/2);}

Test it on Coliru.

php, 38 bytes

<?=$argv[1]*(1-$a=$argv[2])-$a++*$a/2;

Javascript (ES6), 20 19 18 bytes

n=>p=>n+p*(~p/2-n)

Saved 1 byte by currying, as suggested by Zwei
Saved 1 byte thanks to user81655

Test

let f =
n=>p=>n+p*(~p/2-n)

console.log(f(2)(3))
console.log(f(100)(5))
console.log(f(42)(0))
console.log(f(0)(3))
console.log(f(0)(0))

Perl, 23 22 bytes

Includes +1 for -p

Give n and p (in that order) on separate lines of STDIN:

subtract.pl
2
3
^D

subtract.pl:

#!/usr/bin/perl -p
$_-=eval"+2+\$_++"x<>

(using '' quotes to save the \ invokes a 2 byte penalty because it can't be combined with -e)

Same idea and length:

#!/usr/bin/perl -p
$_+=eval"-1-++\$_"x<>

Surprisingly doing the actual calculation is shorter than using the direct formula (these $'s really hurt for arithmetic)

Pyth, 11 bytes

Ms+Gm_+GdSH

A function g that takes input of n and p via argument and prints the result. It can be called in the form gn p.

Try it online

How it works

Ms+Gm_+GdSH  Function g. Inputs: G, H
M            g=lambda G,H:
         SH   1-indexed range, yielding [1, 2, 3, ..., H]
    m_+Gd     Map lambda d:-(G+d) over the above, yielding [-(G+1), -(G+2), -(G+3),
              ..., -(G+H)]
  +G          Add G to the above, yielding [G, -(G+1), -(G+2), -(G+3), ..., -(G+H)]
 s            Reduce on addition with base case 0, yielding G-(G+1)-(G+2)-(G+3)...
              -(G+H)
              Implicitly print

Brachylog, 19 17 bytes

hHyL,?+y:Lx+$_:H+

Explanation

hH                  Input = [H, whatever]
 HyL,               L = [0, …, H]
     ?+             Sum the two elements in the Input
       y            Yield the range from 0 to the result of the sum
        :Lx         Remove all elements of L from that range
           +        Sum the remaining elements
            $_      Negate the result
              :H+   Add H

Labyrinth, 15 bytes

?:?:}*-{:)*#/-!

or

??:}`)*{:)*#/-!

Uses the closed form solution n - n*P - P*(P+1)/2.

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

R, 17 14 bytes

N-N*P-sum(0:P)

Thanks to billywob for golfing away 3 bytes. Previous answer:

N-sum(N+if(P)1:P)

Note that 1:0 expands to the vector (1,0) so we need the if(P) condition (or to use seq_len, but that is more bytes). Without the condition, we would get the wrong output if P=0.

If P is zero, then the sum expands to sum(N+NULL), then to sum(numeric(0)), which is zero.

Haskell, 21 bytes

a#b=foldl1(-)[a..a+b]

Mathematica, 15 bytes

#2-##-#(#+1)/2&

An unnamed function that receives P and n as its parameters in that order.

Uses the closed form solution n - n*p - p(p+1)/2.

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

Pyke, 6 bytes

m+mhs-

Try it here!

m+     -    map(range(input_2), +input_1)
  mh   -   map(^, +1)
    s  -  sum(^)
     - - input_1 - ^

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).

S.I.L.O.S, 80 bytes

GOTO b
lbla
n+1
m-n
i-1
GOTO d
lblb
readIO
n=i
m=n
readIO
lbld
if i a
printInt m

Try it online with test cases:
2,3
100,5
42,0
0,3
0,0

Batch, 30 bytes

@cmd/cset/a%1-(%1*2+%2+1)*%2/2

Takes n and p as command-line parameters and prints the result without a trailing newline.