首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算欧拉数

计算欧拉数
EN

Code Golf用户
提问于 2016-10-19 15:44:14
回答 8查看 1.8K关注 0票数 17

欧拉数 A(n, m)[1, 2, ..., n]的排列数,确切地说,m元素大于前一个元素。这些也被称为上升。例如,如果是n = 3,则[1, 2, 3]的排列数为3!=6

代码语言:javascript
复制
1 2 3
 < <  2 elements are greater than the previous

1 3 2
 < >  1 ...

2 1 3
 > <  1 ...

2 3 1
 < >  1 ...

3 1 2
 > <  1 ...

3 2 1
 > >  0 ...

因此,A(3, m)[0, 1, 2, 3]中的m输出将是

代码语言:javascript
复制
A(3, 0) = 1
A(3, 1) = 4
A(3, 2) = 1
A(3, 3) = 0

另外,这是OEIS序列A173018

规则

  • 这是密码-高尔夫,所以最短的代码获胜。
  • 输入的n将是非负整数,而m将是范围[0, 1, ..., n]中的整数。

测试用例

代码语言:javascript
复制
n   m   A(n, m)
0   0   1
1   0   1
1   1   0
2   0   1
2   1   1
2   2   0
3   0   1
3   1   4
3   2   1
3   3   0
4   0   1
4   1   11
4   2   11
4   3   1
4   4   0
5   1   26
7   4   1191
9   5   88234
10  5   1310354
10  7   47840
10  10  0
12  2   478271
15  6   311387598411
17  1   131054
20  16  1026509354985
42  42  0
EN

回答 8

Code Golf用户

回答已采纳

发布于 2016-10-19 15:55:53

果冻,8 字节数

代码语言:javascript
复制
Œ!Z>2\Sċ

在网上试试! (需要一段时间)或验证较小的测试用例

是如何工作的

代码语言:javascript
复制
Œ!Z>2\Sċ  Main link. Arguments: n, m

Œ!        Generate the matrix of all permutations of [1, ..., n].
  Z       Zip/transpose, placing the permutations in the columns.
   >2\    Compare columns pairwise with vectorizing greater-than.
          This generates a 1 in the column for each rise in that permutation.
      S   Compute the vectorizing sum of the columns, counting the number of rises.
       ċ  Count how many times m appears in the computed counts.
票数 9
EN

Code Golf用户

发布于 2016-10-19 20:49:52

马蒂尔,10字节

代码语言:javascript
复制
:Y@!d0>s=s

在网上试试!

解释

以输入n=3m=1为例。您可以放置一个%符号,从那时起注释掉代码,从而看到中间结果。例如,链接显示第一步之后的堆栈。

代码语言:javascript
复制
:      % Input n implicitly. Push [1 2 ... n]
       % STACK: [1 2 ... n]
Y@     % Matrix of all permutations, one on each row
       % STACK: [1 2 3; 1 3 2; 2 1 3; 2 3 1; 3 1 2; 3 2 1]
!      % Transpose
       % STACK: [1 1 2 2 3 3; 2 3 1 3 1 2; 3 2 3 1 2 1]
d      % Consecutive differences along each column
       % STACK: [1 2 -1 1 -2 -1; 1 -1 2 -2 1 -1]
0>     % True for positive entries
       % STACK: [1 1 0 1 0 0; 1 0 1 0 1 0]
s      % Sum of each column
       % STACK: [2 1 1 1 1 0]
=      % Input m implicitly. Test each entry for equality with m
       % STACK: [0 1 1 1 1 0]
s      % Sum. Implicitly display
       % STACK: 4
票数 4
EN

Code Golf用户

发布于 2016-10-20 04:51:23

Python,53字节

代码语言:javascript
复制
t=lambda n,k:n and(n-k)*t(n-1,k-1)-~k*t(n-1,k)or k==0

来自OEIS的递归。将布尔True输出为1 (当n==k时)。

票数 3
EN
页面原文内容由Code Golf提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codegolf.stackexchange.com/questions/96747

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档