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

# 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

• Ahhh, I almost wanted to post my solution haha. Also, for three bytes: Ý+Æ :). Commented Sep 1, 2016 at 11:56
• It only switches the input (P goes first) Commented Sep 1, 2016 at 11:56
• @Adnan: I didn't even know 05AB1E had Ý... I thought only 1-based range existed. Commented Sep 1, 2016 at 11:58
• In which character encoding is that only 3 Bytes? ;-) Commented Sep 2, 2016 at 14:14
• @yankee: CP-1252 Commented Sep 2, 2016 at 14:16

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


## Haskell, 21 bytes

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


## Javascript (ES6), 2019 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))

• You can save a byte by currying the function. n=>p=>... and calling the function with f(n)(p)
– Zwei
Commented Sep 1, 2016 at 11:34
• (n,p)=>n-p*(++p/2+n) will also works in C#. Commented Sep 1, 2016 at 11:36
• n-p*(++p/2+n) is equivalent to n+p*(~p/2-n). Commented Sep 1, 2016 at 14:38

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


# 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#, 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

}
}
}

• use currying N=>P=> instead of (N,P)=> to save 1 byte Commented Sep 2, 2016 at 10:23

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

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

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

• Welcome to PPCG! Unfortunately, all submissions need to be programs or callable functions, whereas this is just a snippet that assumes the input to be stored in pre-defined variables and stores the output in another. Commented Sep 1, 2016 at 12:06
• @MartinEnder I have changed to C++ with lambda. Is it acceptable? Commented Sep 1, 2016 at 12:25
• Yes, lambdas are fine. :) Commented Sep 1, 2016 at 12:25
• You can do this in C with 40 bytes f;g(n,p){f=n;while(p--)f-=++n;return f;} using your algorithm Commented Sep 1, 2016 at 13:35
• @cleblanc Thanks for tip - global variable and declaration without an explicit type are really useful. What a pity that C99 standard removed implicit int Commented Sep 2, 2016 at 4:57

## Pyke, 6 bytes

m+mhs-


Try it here!

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


# 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


# Forth, 36 bytes

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

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


Try it 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


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

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

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


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

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

• Not sure if this qualifies as a full program because it requires N and P to be already defined. Either way using n-n*p-sum(0:p) would be shorter anyways :) Commented Sep 1, 2016 at 11:50
• My interpretation of the problem is that N and P are already defined (other answers seem to take this line as well). Golfing point taken though.
– JDL
Commented Sep 1, 2016 at 12:19
• Unless specified otherwise submissions need to be full programs or callable functions not just snippets. Which other answers make the assumption that the variables are already defined? Commented Sep 1, 2016 at 12:30
• I'm not a javascript expert, but it looks like the javascript solution is taking the variables as already defined. That could be my own misunderstanding though. Since N and P were named as such in the problem, I took that as "specified otherwise". If not, then we need a wrapper function(N,P){...} or N=scan();P=scan();...
– JDL
Commented Sep 1, 2016 at 12:47
• @JDL the javascript entry doesn't take predefined variabled
– Blue
Commented Sep 1, 2016 at 14:08

# PHP, 33 Bytes

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

• I think you need to use <?php or short <? for PHP-Code. Please edit your answer. Commented Sep 1, 2016 at 16:16
• php.net/manual/de/features.commandline.usage.php not from the command line Commented Sep 1, 2016 at 17:40
• Sorry, forget what said. I have seen many answers with this, and therefore thought, that there is a rule for that, which is not the case. There should be one, to avoid discussions like this one. Commented Sep 1, 2016 at 18:00

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


# Pyth - 6 bytes

-F}Q+E


# 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

• You can remove the space between int n,int p to save a byte. Also, you can change the p%2==0 to p%2<1 to save another byte. -- I wasn't aware you had already posted a Java answer when I posted my shorter variant with for-loop. I like your mathematical formula, though, so +1 from me. :) Commented Sep 1, 2016 at 13:40
• Great formula! Using p%2>0 and switching the order in the ternary you can save a character. Commented Sep 1, 2016 at 18:40
• Oh and also p/2 *(p+2) is equal to p*p/2+p Commented Sep 1, 2016 at 18:48
• Hehe great improvements :) actually this formula comes from a funny anecdote :) @KevinCruijssen nice answer, definitely better than mine :) +1 Commented Sep 2, 2016 at 9:16

# 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

• What about this requires Java 7? Commented Sep 2, 2016 at 13:15
• @mbomb007 int c(int n,int p){...}. If it would have been Java 8 (or 9) it could have been (n,p)->n-p*n+p*~p/2 (19 bytes) Commented Sep 2, 2016 at 13:16
• Then do that to save those bytes. Commented Sep 2, 2016 at 13:19

# Fourier, 34 bytes

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


Try it online!

## Labyrinth, 15 bytes

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


or

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


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

# php, 38 bytes

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


# 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


# Vyxal, 4 bytes

+ṡƒ-


Try it Online!

Port of Jelly answer.

## How?

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


# 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


# 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
`