| Bytes | Lang | Time | Link |
|---|---|---|---|
| 011 | APLNARS | 251001T145913Z | Rosario |
| 009 | Dyalog APL | 250930T203320Z | Aaron |
| 027 | AWK | 250930T152851Z | xrs |
| 005 | MATL | 160901T123609Z | Luis Men |
| 017 | TIBasic | 220515T145415Z | Youserna |
| 014 | Ly | 220514T180301Z | cnamejj |
| 043 | Python 3 | 220514T133541Z | Joao-3 |
| 004 | Vyxal | 220428T002034Z | naffetS |
| 010 | K ngn/k | 220507T034032Z | oeuf |
| 020 | Demsos | 220506T164849Z | naffetS |
| 005 | Japt | 201221T104233Z | Shaggy |
| 012 | APL Dyalog Unicode | 201221T092538Z | Kamila S |
| 064 | W d | 200114T063259Z | user8505 |
| 006 | Keg | 200111T215015Z | lyxal |
| 012 | Burlesque | 200113T142648Z | DeathInc |
| 011 | GolfScript | 200111T131828Z | user8505 |
| 020 | Excel | 170927T135312Z | Wernisch |
| 034 | Fourier | 160901T110752Z | Beta Dec |
| nan | Julia 1718 bytes 13 as program with terminal inputs | 160903T084633Z | Tasos Pa |
| 030 | Clojure/Clojurescript | 160902T203708Z | MattPutn |
| 041 | Scala | 160902T152947Z | AmazingD |
| 040 | Java 7 | 160901T130358Z | Kevin Cr |
| 063 | Java | 160901T120616Z | peech |
| 025 | Java 8 | 160902T123342Z | Shaun Wi |
| 020 | C# | 160901T142305Z | adrianmp |
| 018 | Haskell | 160901T132221Z | Damien |
| 051 | C++ | 160901T120514Z | VolAnd |
| 036 | Forth | 160901T213408Z | mbomb007 |
| 021 | Perl 6 | 160901T194034Z | Brad Gil |
| 006 | Pyth | 160901T190552Z | Maltysen |
| 004 | Jelly | 160901T190105Z | Dennis |
| 019 | Maple | 160901T185736Z | DSkoog |
| 007 | Jelly | 160901T183624Z | Jonathan |
| 033 | PHP | 160901T160027Z | Jör |
| 033 | C89 | 160901T152751Z | YSC |
| 038 | php | 160901T154000Z | user5917 |
| 018 | Javascript ES6 | 160901T112636Z | Arnauld |
| 022 | Perl | 160901T115850Z | Ton Hosp |
| 011 | Pyth | 160901T131155Z | TheBikin |
| 017 | Brachylog | 160901T121436Z | Fatalize |
| 015 | Labyrinth | 160901T122755Z | Martin E |
| 023 | Python 2 | 160901T115852Z | Jonathan |
| 014 | R | 160901T114137Z | JDL |
| 021 | Haskell | 160901T120952Z | ThreeFx |
| 015 | Mathematica | 160901T120813Z | Martin E |
| 003 | 05AB1E | 160901T115524Z | Emigna |
| 006 | Pyke | 160901T115343Z | Blue |
| 008 | CJam | 160901T113437Z | Martin E |
| 080 | S.I.L.O.S | 160901T111921Z | betseg |
| 030 | Batch | 160901T110753Z | Neil |
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.
MATL, 5 bytes
:y+s-
Inputs are P and then N.
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*-&+
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)])
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
+ṡƒ-
Port of Jelly answer.
How?
+ṡƒ-
+ṡ # Inclusive range [a, a + b]
ƒ- # 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
With the sum function, it's 27 bytes:
f(n,p)=n-∑_{x=n+1}^{n+p}x
Japt, 5 bytes
ôV r-
ôV r- :Implicit input of integers U=N & V=P
ôV :Range [U,U+V]
r- :Reduce by subtraction
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
Burlesque, 12 bytes
perz?+{.-}r[
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/+*-
Explanation
This just boils down to
A-B*(A+(B+1)/2)
Converted to postfix.
GolfScript, 15 bytes
~),{1$+}%{-}*\;
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)
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∘3outputs-16as program, passed initial parameters from terminal: 13 bytes:
2n-sum(n:n+r)usage:
julia -E 'n,r=5,3;include("codegolf.jl")'
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:
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);
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 * + - ;
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)
}
Jelly, 4 bytes
r+_/
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));
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.
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-
m+ - map(range(input_2), +input_1)
mh - map(^, +1)
s - sum(^)
- - input_1 - ^
CJam, 8 bytes
{),f+:-}
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
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.