减去下一个数字


27

描述

从N数中减去下一个P数。N的下一个数字是N + 1。

查看示例以了解我的意思。

例子:

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

输入:

N:整数,正,负或0

P:整数,正或0,不为负

输出:

整数或字符串,允许前导0,允许尾随换行符

规则:

  • 没有漏洞
  • 这是代码高尔夫球,因此以字节为单位的最短代码获胜
  • 输入和输出必须符合描述

1
这里的主要挑战是计算三角形数。
彼得·泰勒

4
除了三角数之外,还有更多其他功能。起点是任意的,减法的数目也可以是零。
JDL

另外,对于三角数,实际总和可能比使用封闭形式短,而您不能仅通过将0到N的范围求和来计算任意多边形。(如果其他挑战只是要求提供三角数。)
Martin Ender

1
对于Input: N=0,P=3例如,你的扩展有一些外来的双重否定
turbulencetoo

1
@JDL是“不仅仅是三角数”的部分,是一个简单的乘法:N * (P-1)。这实际上是琐碎定义
彼得·泰勒

Answers:


15

05AB1E5 3字节

感谢Adnan,节省了2个字节

Ý+Æ

说明

取P,然后取N作为输入。

       # 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

4
啊,我几乎想发布我的解决方案哈哈。另外,对于三个字节:Ý+Æ:)。
阿德南

它仅切换输入(P第一位)
Adnan

@Adnan:我什至不知道05AB1E有Ý...我以为仅存在基于1的范围。
Emigna '16

只有3个字节是哪种字符编码?;-)
洋基

1
@yankee:CP-1252
Emigna '16

16

Python 2,26 24 23字节

-2字节由于@Adnan(替换p*(p+1)/2p*-~p/2
-1由于@MartinEnder字节(替换-p*-~p/2+p*~p/2

lambda n,p:n-p*n+p*~p/2

测试在ideone上


11

CJam,8个字节

{),f+:-}

测试套件。

太糟糕了,封闭式解决方案更长。:|

说明

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


10

Javascript(ES6),20 19 18字节

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

根据Zwei的建议,通过
curry节省了1个字节感谢user81655节省了1个字节

测试

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


您可以通过使用函数来保存一个字节。n=>p=>...并使用f(n)(p)
Zwei

(n,p)=>n-p*(++p/2+n)也将在C#中工作。
aloisdg说,请恢复莫妮卡(Monica)

1
n-p*(++p/2+n)等同于n+p*(~p/2-n)
user81655'9


7

Haskell,19个 18字节

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

前19个字节的解决方案

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

7

C#,21 20字节

编辑:由于TheLethalCoder而节省了一个字节

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

在线尝试!

完整的源代码,包括测试用例:

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

        }
    }
}

1
使用currying N=>P=>而不是(N,P)=>保存1个字节
TheLethalCoder

5

Mathematica,15个字节

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

一个未命名的函数,该函数按此顺序接收Pn作为其参数。

使用封闭式解决方案n - n*p - p(p+1)/2


5

Perl,23 22字节

包括+1的 -p

在STDIN的不同行上分别给出n和p(按顺序):

subtract.pl
2
3
^D

subtract.pl

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

(使用''引号保存\调用会导致2字节的罚款,因为它不能与结合使用-e

相同的想法和长度:

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

出乎意料的是,实际计算比使用直接公式要短(这$对算术确实很不利)


5

C ++,54 51字节

  [](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--);返回F;}

测试:

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

2
欢迎来到PPCG!不幸的是,所有提交都必须是程序或可调用函数,而这只是一个片段,假定输入要存储在预定义变量中,而输出要存储在另一个变量中。
Martin Ender

1
@MartinEnder我已将lambda更改为C ++。可以接受吗?
VolAnd

1
是的,lambda很好。:)
Martin Ender

您可以f;g(n,p){f=n;while(p--)f-=++n;return f;}使用算法在C中使用40个字节来完成此操作
cleblanc

@cleblanc感谢您的提示-全局变量和没有显式类型的声明确实很有用。C99标准消除了隐含的可惜int
VolAnd


4

Brachylog19 17字节

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

说明

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

4

MATL,5个字节

:y+s-

输入为P,然后输入N

在MATL在线上尝试一下!

说明

:     % 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

3

批处理,30个字节

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

nand p作为命令行参数,并在不带换行符的情况下打印结果。



3

R,17 14字节

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

感谢billywob打高尔夫球3个字节。先前的答案:

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

请注意,1:0扩展为向量(1,0),因此我们需要if(P)条件(或使用seq_len,但更多字节)。如果没有该条件,那么如果P = 0,我们将得到错误的输出。

如果P为零,则总和扩展为sum(N+NULL),然后扩展为,sum(numeric(0))即零。


3
不确定是否符合完整程序的要求,因为它要求已经定义N和P。无论哪种方式使用n-n*p-sum(0:p)都会更短:)
Billywob '16

我对问题的解释是已经定义了N和P(其他答案似乎也是如此)。虽然打高尔夫球。
JDL

3
除非另有说明,否则提交内容不仅应是摘要,还必须是完整程序或可调用函数。还有哪些答案假设变量已经定义?
Martin Ender

我不是javascript专家,但看来javascript解决方案正在采用已定义的变量。不过那可能是我自己的误会。由于在问题中N和P都是这样命名的,因此我将其称为“否则指定”。如果没有,那么我们需要一个包装器function(N,P){...}N=scan();P=scan();...
JDL

@JDL的JavaScript项不采取预定义variabled


3

果冻,7 个字节

RS+×_×-

参数正在TryItOnlineP, N
上进行测试

怎么样?

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)



3

Java,67,63字节

打高尔夫球:

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

取消高尔夫:

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

基本上我对公式做了一些数学运算。该n - p*n部分照顾所有n公式。然后,我使用了一个超级有趣的属性,将线性增加的整数集(算术序列)加在一起:我使用了第一个和最后一个整数的和,然后乘以set.length / 2(我也检查奇偶校验并适当地处理它)。

试试看:https : //ideone.com/DEd85A


您可以删除它们之间的空间int n,int p以保存一个字节。此外,您还可以改变p%2==0,以p%2<1挽救另一个字节。-当我发布带有for-loop的较短变体时,我不知道您已经发布了Java答案。不过,我喜欢您的数学公式,所以我+1。:)
Kevin Cruijssen '16

很棒的配方!p%2>0在三元组中使用和切换顺序可以保存字符。
Frozn

哦,也p/2 *(p+2)等于p*p/2+p
Frozn

呵呵取得了很大的进步:)实际上,这个公式来自一个有趣的轶事 :) @KevinCruijssen不错的答案,绝对比我的要好:) +1
peech

3

Java 7,43 40字节

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

Java 8,19字节

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

@JonathanAllan惊人的Python 2公式被无耻地偷了。

原始答案(61 60字节):

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

非高尔夫球和测试用例:

在这里尝试。

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

输出:

-10
-415
42
-6
0

那么这需要Java 7吗?
mbomb007'9

@ mbomb007 int c(int n,int p){...}。如果可能是Java 8(或9),则可能是(n,p)->n-p*n+p*~p/219个字节
Kevin Cruijssen

然后执行该操作以保存这些字节。
mbomb007'9




1

Pyth,11个字节

Ms+Gm_+GdSH

该函数g接受np通过参数输入并输出结果。可以形式调用gn p

在线尝试

怎么运行的

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


1

Maple,19个字节

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

用法:

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

1

Perl 6,21个字节

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

说明:

# 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)
}
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.