求第n个交叉备用总和


17

给定单个正整数的输入,则输出对应于该整数的“交叉替代总和”。

以输入为例n=5。要查找交叉替换的总和,请首先创建一个宽度和高度的正方形网格,该网格n从左至右,从上至下依次从1每个位置开始并增加一个:

 1  2  3  4  5
 6  7  8  9 10
11 12 13 14 15
16 17 18 19 20
21 22 23 24 25

然后,从构成“十字”(即,两个对角线合并)的网格中求和:

 1           5
    7     9
      13
   17    19
21          25

1 5 7 9 13 17 19 21 25

最后,取该序列的交替总和:

1+5-7+9-13+17-19+21-25

-11

另一个示例,for n=6(只是为了显示偶数编号的十字架的样子n):

 1  2  3  4  5  6
 7  8  9 10 11 12
13 14 15 16 17 18
19 20 21 22 23 24
25 26 27 28 29 30
31 32 33 34 35 36

 1              6
    8       11
      15 16
      21 22
   26       29
31             36

1+6-8+11-15+16-21+22-26+29-31+36

20

由于这是,因此以字节为单位的最短代码将获胜。

以下是n=1to 的正确输出n=100,您可以将其用作测试用例:

1
4
-3
10
-11
20
-23
34
-39
52
-59
74
-83
100
-111
130
-143
164
-179
202
-219
244
-263
290
-311
340
-363
394
-419
452
-479
514
-543
580
-611
650
-683
724
-759
802
-839
884
-923
970
-1011
1060
-1103
1154
-1199
1252
-1299
1354
-1403
1460
-1511
1570
-1623
1684
-1739
1802
-1859
1924
-1983
2050
-2111
2180
-2243
2314
-2379
2452
-2519
2594
-2663
2740
-2811
2890
-2963
3044
-3119
3202
-3279
3364
-3443
3530
-3611
3700
-3783
3874
-3959
4052
-4139
4234
-4323
4420
-4511
4610
-4703
4804
-4899
5002

8
尼特·皮克(Nit pick):这不是一个交替的数字。您要添加前两个术语。
丹尼斯

Answers:


26

果冻,21 19 11 10 7字节

²~³¡H+2

在线尝试!

理念

假设最后一笔的第一项是减去而不是加一秒钟。

n为正整数。

偶数情况

 1              6
    8       11
      15 16
      21 22
   26       29
31             36

行的下半部分对角元素之间的差是前n÷2个奇数自然数。由于1 + 3 + 5 +…+(2k +1)= k 2,它们的总和为(n÷2)2 = n 2 ÷4

在这个例子中

- 1 + 6 - 8 + 11 - 15 + 16 - 21 + 22 - 26 + 29 - 31 + 36  =
-(1 - 6)-(8 - 11)-(15 - 16)-(21 - 22)-(26 - 29)-(31 - 36) =
(    5   +   3    +    1  )+(   1    +    3    +    5   ) =
             9             +              9               = 18

因此,总和为2×n 2 ÷4 = n 2 ÷2

奇怪的情况

 1           5
    7     9
      13
   17    19
21          25

上下对应行上的对角元素之间的差异(15,和21257以及9,和1719)是相同的,因此它们将在交替和中抵消。

在这个例子中

- 1 + 5 - 7 + 9 - 13 + 17 - 19 + 21 - 25  =
-(1 - 5)-(7 - 9)- 13 +(17 - 19)+(21 - 25) =
    4   +   2   - 13 -    2    -    4     = -13

剩下的就是中心元素的负数,即第一个和最后一个数字的算术平均值,因此可以将其计算为-(n 2 +1)÷2

一般情况

由于〜x =-(x + 1)是二进制补码(表示按位非),因此奇数情况的公式可以重写为〜n 2 ÷2

另外,由于原始和的第一项(1)被添加而不是被减去,因此上述公式留下了误差2,必须对其进行校正。

因此,第n 交叉交替总和是Ñ 2 ÷2 + 2如果Ñ是偶数,并且〜n的2 ÷2 + 2,如果它为奇数。

最后,按位NOT不是对合,即,对于所有x~~ x = x。这样~~~ X =〜X~~~~ X = X,并且,在一般情况下,Ñ X(意味着施加Ñ倍)是X如果Ñ是偶数且〜X,如果它为奇数。

因此,我们可以重写我们的一般公式ñ ñ 2 ÷2 + 2的所有正整数ñ

²~³¡H+2    Main link. Input: n

²          Yield n².
 ~         Apply bitwise NOT to n²...
  ³¡           n times.
    H      Halve the result.
     +2    Add 2.

1
我知道这里有一个简单的公式,但是您的解释和执行真是太神奇了。+1
ETHproductions 2016年

5

JavaScript,40 38 22字节

使用那种新奇的,花哨的封闭表格解决方案真是风靡一时!

n=>(n%2?3-n*n:4+n*n)/2

多亏了ThomasKwa,我可以省去我昂贵的递归函数。


如果n%2,则只需要按位进行一次。实际上,我认为在JS中做起来可能会更短(n%2?3-n*n:4+n*n)/2
lirtosiast '16



3

Minkolang 0.1526个 15 13字节

使用丹尼斯的疯狂算法,多亏了他,他又打了两个字节。那家伙负责减少字节数!

n2;d[~]4+2:N.

在这里尝试!

说明

@VoteToClose n^ 2,按位进行NOT运算n,加四,减半。– Thomas Kwa 7分钟前

有关为何有效的说明,请参见丹尼斯的答案。在对此答案的评论中,他提出了另一个可行的改进,因为它:是整数除法,因此我可以取反堆栈的顶部,而不用担心二进制补码的+1。此外,n和n ^ 2具有相同的奇偶校验,从而无需交换。

n                Take number from input - n
 2;              n**2
   d             Duplicates top of stack
    [~]          For loop that negates the top of stack n times
       4+        Add 4
         2:      Divide by 2
           N.    Output as number and stop.

2

GolfScript,12个字节

~2?.{~}*2/2+

这使用了我的Jelly答案中的算法。在线尝试!

怎么运行的

~            # Evaluate the input.
 2?          # Square it.
   .         # Push a copy of the square.
    {~}      # Push a code block that applies bitwise NOT.
       *     # Execute it n² times. Since n² and n have the same parity,
             # this is equivalent to executing in only n times.
        2/   # Halve the result.
          2+ # Add 2.

2

ES7,17个字节

n=>-1**n*n*n+4>>1

@Dennis的Python 2答案的简单端口。

在编写此答案时,我也设法将ES6端口打入17个字节!

n=>(n*n^-n%2)/2+2


2

纯净重击,28岁

现在,@ Dennis向我们展示了所有方法,这需要更新:

echo $[-1**$1*($1*$1+1)/2+2]

先前的答案:

Bash + GNU实用程序,77

这是一个开始:

a=$1
(seq 1 $[a+1] $[a*a]
seq $1 $[a>1?a-1:1] $[a*a])|sort -un|paste -sd+-|bc

N作为命令行参数传递。

paste在这里产生交替和真的很方便。该-d选项允许循环使用分隔符列表。


$[-1**$1*$1*$1+4>>1]更短。
尼尔

2

朱莉娅41 40 25 19 16字节

n->-n%2$n^2÷2+2

这是一个接受整数并返回整数的匿名函数。要调用它,请将其分配给变量。

由丹尼斯(Dennis)设计的方法如下。首先,我们获得n的奇偶校验,即n(模2),并将其取反。对于偶数输入,这给了我们0;对于奇数输入,这给了我们-1。然后,我们对n 2进行按位XOR运算。当n为偶数时,这只是n 2,因为与0的XOR只是数字。当n为奇数时,与-1的XOR与按位求反相同。所以在这一点上,我们要么有ñ 2或按位NOT ñ 2。我们将其除以2并加2得到结果。

在以前的版本中,由于Sp3000节省了一个字节,而在此版本中,由于Dennis节省了9个字节!



1

Python 2,24字节

lambda n:(-1)**n*n*n/2+2

这使用了我的Jelly答案中的算法,并做了一些小的修改:

我们不应用~ n次,而是应用- n次(乘以(-1)n)。这是等效的,因为〜x = -x-1和Python中的整数除数,因此〜x / 2 =(-x-1)/ 2 = -x / 2


1

Pyth,11个字节

+2/u_GQ*QQ2

Pyth编译器中在线尝试。

怎么运行的

这使用了我的Jelly答案中的算法,并做了一些小的修改:

我们不应用~ n次,而是应用- n次(乘以(-1)n)。这是等效的,因为〜x = -x-1且整数分位数为Pyth,所以〜x / 2 =(-x-1)/ 2 = -x / 2

+2/u_GQ*QQ2  Evaluated input: Q

       *QQ   Yield Q².
   u  Q      Set G = Q². For each non-negative integer below Q:
    _G         Set G = -G.
             Return G.
  /       2  Halve the result.
+2           Add 2.

1

直流电,17

使用来自丹尼斯的相同的久经考验的公式:

?dd*1+r_1r^*2/2+p

在线尝试 哦,为什么Ideone bash沙箱不包含dc

命令行测试:

for i in {1..100}; do echo $i | dc -e '?dd*1+r_1r^*2/2+p'; done 

?2^1+2~2*1-*2+p保存两个字节。
丹尼斯

1

GS2,9个字节

V,@!α2+''

这使用了我的Jelly答案中的算法。在线尝试!

V,@e 7+''

同样短,但明显不包含非ASCII字符。

怎么运行的

V          Parse the input as an integer n.
 ,         Compute n².
  @        Push a copy of n².
   !       Bitwise NOT.
    α      Make a block of the previous instruction.
     2     Execute the block n² times. Since n² and n have the same parity,
           this is equivalent to executing in only n times.
      +    Halve the result.
       ''  Increment twice.


0

Lua,33个字节(在线尝试

i=(...)print((-1)^i*i*i/2-.5*i%2)

怎么运行的:

i=(...)print((-1)^i*i*i/2-.5*i%2)
i=(...)                           Take input and store to i
       print(
             (-1)^i               Raise (-1) to the i-th power: -1 if odd, 1 if even
                   *i*i/2         Multiply by i squared and halved.
                         -.5*i%2  i%2 is the remainder when i is divided by 2
                                  if i is odd, then i%2 will be 1, and this expression
                                  will evaluate to -0.5
                                  but if i is even, then i%2 will be 0, which makes
                                  this expression evaluate to 0
                                )

0

Dyalog APL,13个字节

⌊2+.5××⍨ׯ1*⊢

这使用与我的果冻答案相同的算法。在TryAPL上进行测试

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.