输出欧拉数


28

给定非负整数输出欧拉数OEIS A122045)。n,nth

所有奇数索引的欧拉数均为可以使用以下公式计算偶数索引的欧拉数(表示虚数单位): 0.i1

E2n=ik=12n+1j=0k(kj)(1)j(k2j)2n+12kikk.

规则

  • n将是一个非负整数,因此欧拉数在您的语言可表示的整数范围内。nth

测试用例

0 -> 1
1 -> 0
2 -> -1
3 -> 0
6 -> -61
10 -> -50521
20 -> 370371188237525

1
@donbright您缺少一组括号:wolframalpha.com/input/…-这样,两个被加数均为-i/2-i在相加时产生。将其乘以i求和的外部,即可得到1
Mego

Answers:



13

J,10个字节

(1%6&o.)t:

在线尝试!

使用指数生成函数sech(x)的定义。


J是否进行符号分析以获得生成函数?即使对于n = 30,也不会遇到浮点错误。
orlp

@orlp我不确定它在内部做什么,但是J知道动词子集的泰勒级数。您可以使用这些动词的组合定义的任何函数将对gf和egf 有效,t.或者t:在gf和egf哪里有效。奇怪的是,不支持tan(x),但支持sin(x)/ cos(x)。
英里


11

Maple,5个字节

euler

为内置函数欢呼吗?


4
当mathematica对于数学问题过于冗长时,喜欢它...
–onlygusti

11

最大值,5字节/ 42字节

Maxima具有内置功能:

euler

在线尝试!

以下解决方案不需要上面的内置方法,而是使用最初定义欧拉数的公式。

我们基本上是在寻找1/cosh(t) = sech(t)(直到n!)的级数展开的n个系数

f(n):=coeff(taylor(sech(x),x,0,n)*n!,x,n);

在线尝试!


9

Mathematica,无内置,18字节

使用@ rahnema1的公式:

2Im@PolyLog[-#,I]&

21个字节:

Sech@x~D~{x,#}/.x->0&

5

Python 2.7,46个字节

使用scipy。

from scipy.special import*
lambda n:euler(n)[n]

5

Perl 6、78个字节

{(->*@E {1-sum @E».&{$_*2**(@E-1-$++)*[*](@E-$++^..@E)/[*] 1..$++}}...*)[$_]}

从这里使用迭代公式:

Eñ=1个-ķ=0ñ-1个[Ëķ2ñ-1个-ķñķ]

怎么运行的

常规结构是一个lambda,在该lambda中,将生成一个无限序列,该表达式将被反复调用并在变量中获取序列的所有先前值@E,然后使用lambda参数为该序列建立索引:

{ ( -> *@E {    } ... * )[$_] }

该序列的每个步骤所调用的表达式为:

1 - sum @E».&{              # 1 - ∑
    $_                      # Eₙ
    * 2**(@E - 1 - $++)     # 2ⁿ⁻ˡ⁻ᵏ
    * [*](@E - $++ ^.. @E)  # (n-k-1)·...·(n-1)·n
    / [*] 1..$++            # 1·2·...·k
}


4

JavaScript(Node.js)46 45字节

F=(a,b=a)=>a?(b+~a)*F(--a,b-2)+F(a,b)*++b:+!b

在线尝试!

EnF(n,i)F(n,i)nF(n,i)=(1)nF(n,i) where F is defined as below. Specifically, the recurrence formula for F is

F(n,i)=(in1)F(n1,i2)+(i+1)F(n1,i)

JavaScript (Node.js), 70 46 bytes

F=(a,b=a)=>a?-F(--a,b)*++b+F(a,b-=3)*(a-b):+!b

Try it online!

Surprised to find no JavaScript answer yet, so I'll have a try.

The code consists of only basic mathematics, but the mathematics behind the code requires calculus. The recursion formula is derived from the expansion of the derivatives of sech(x) of different orders.

Explanation

Here I'll use some convenient notation. Let Tn:=tanhn(t) and Sn:=sechn(t). Then we have

dnSdtn=i=0nF(n,i)TniSi+1

Since dTdt=S2 and dSdt=TS, we can deduce that

ddt(TaSb)=aTa1(S2)(Sb)+bSb1(TS)(Ta)=aTa1Sb+2bTa+1Sb

Let b=i+1 and a=ni, we can rewrite the relation above as

ddt(TniSi+1)=(ni)Tni1Si+3(i+1)Tni+1Si+1=(ni)T(n+1)(i+2)S(i+2)+1(i+1)T(n+1)iSi+1

That is, F(n,i) contributes to both F(n+1,i+2) and F(n+1,i). As a result, we can write F(n,i) in terms of F(n1,i2) and F(n1,i):

F(n,i)=(ni+1)F(n1,i2)(i+1)F(n1,i)

with initial condition F(0,0)=1 and F(0,i)=0 where i0.

The related part of the code a?-F(--a,b)*++b+F(a,b-=3)*(a-b):+!b is exactly calculating using the above recurrence formula. Here's the breakdown:

-F(--a,b)                // -F(n-1, i)                  [ a = n-1, b = i   ]
*++b                     // *(i+1)                      [ a = n-1, b = i+1 ]
+F(a,b-=3)               // +F(n-1, i-2)                [ a = n-1, b = i-2 ]
*(a-b)                   // *((n-1)-(i-2))              [ a = n-1, b = i-2 ]
                         // which is equivalent to *(n-i+1)

Since T(0)=0 and S(0)=1, En equals the coefficient of Sn+1 in the expansion of dnSdtn, which is F(n,n).

For branches that F(0,0) can never be reached, the recurrences always end at 0, so F(n,i)=0 where i<0 or i is odd. The latter one, particularly, implies that En=0 for all odd ns. For even is strictly larger than n, the recurrence may eventually allow 0in to happen at some point, but before that step it must reach a point where i=n+1, and the recurrence formula shows that the value must be 0 at that point (since the first term is multiplied by ni+1=n(n+1)+1=0, and the second term is farther from the "triangle" of 0in). As a result, F(n,i)=0 where i>n. This completes the proof of the validity of the algorithm.

Extensions

The code can be modified to calculate three more related sequences:

Tangent Numbers (46 bytes)

F=(a,b=a)=>a?F(--a,b)*++b+F(a,b-=3)*(a-b):+!~b

Secant Numbers (45 bytes)

F=(a,b=a)=>a?F(--a,b)*++b+F(a,b-=3)*(a-b):+!b

Euler Zigzag Numbers (48 bytes)

F=(a,b=a)=>a?F(--a,b)*++b+F(a,b-=3)*(a-b):!b+!~b

3

Befunge, 115 bytes

This just supports a hardcoded set of the first 16 Euler numbers (i.e. E0 to E15). Anything beyond that wouldn't fit in a 32-bit Befunge value anyway.

&:2%v
v@.0_2/:
_1.@v:-1
v:-1_1-.@
_5.@v:-1
v:-1_"="-.@
_"}$#"*+.@v:-1
8**-.@v:-1_"'PO"
"0h;"3_"A+y^"9*+**.@.-*8+*:*

Try it online!

I've also done a full implementation of the formula provided in the challenge, but it's nearly twice the size, and it's still limited to the first 16 values on TIO, even though that's a 64-bit interpreter.

<v0p00+1&
v>1:>10p\00:>20p\>010g20g2*-00g1>-:30pv>\:
_$12 0g2%2*-*10g20g110g20g-240pv^1g03:_^*
>-#1:_!>\#<:#*_$40g:1-40p!#v_*\>0\0
@.$_^#`g00:<|!`g01::+1\+*/\<
+4%1-*/+\2+^>$$10g::2>\#<1#*-#2:#\_$*\1

Try it online!

The problem with this algorithm is that the intermediate values in the series overflow much sooner than the total does. On a 32-bit interpreter it can only handle the first 10 values (i.e. E0 to E9). Interpreters that use bignums should do much better though - PyFunge and Befungee could both handle at least up to E30.


1

Python2, (sympy rational), 153 bytes

from sympy import *
t=n+2 
print n,re(Add(*map(lambda (k,j):I**(k-2*j-1)*(k-2*j)**(n+1)*binomial(k,j)/(k*2**k),[(c/t+1,c%t) for c in range(0,t**2-t)])))

This is very suboptimal but it's trying to use basic sympy functions and avoid floating point. Thanks @Mego for setting me straight on the original formula listed above. I tried to use something like @xnor's "combine two loops" from Tips for golfing in Python


1
You can do import* (remove the space in between) to save a byte. Also, you need to take the number as an input somehow (snippets which assume the input to be in a variable are not allowed).
FlipTack

1

CJam (34 bytes)

{1a{_W%_,,.*0+(+W%\_,,:~.*.+}@*W=}

Online demo which prints E(0) to E(19). This is an anonymous block (function).

The implementation borrows Shieru Akasoto's recurrence and rewrites it in a more CJam friendly style, manipulating entire rows at a time.

Dissection

{           e# Define a block
  1a        e#   Start with row 0: [1]
  {         e#   Loop...
    _W%     e#     Take a copy and reverse it
    _,,.*   e#     Multiply each element by its position
    0+(+    e#     Pop the 0 from the start and add two 0s to the end
    W%      e#     Reverse again, giving [0 0 (i-1)a_0 (i-2)a_1 ... a_{i-2}]
    \       e#     Go back to the other copy
    _,,:~.* e#     Multiply each element by -1 ... -i
    .+      e#     Add the two arrays
  }         e#
  @*        e#   Bring the input to the top to control the loop count
  W=        e#   Take the last element
}


0

Axiom, 5 bytes

euler

for OEIS A122045; this is 57 bytes

g(n:NNI):INT==factorial(n)*coefficient(taylor(sech(x)),n)

test code and results

(102) -> [[i,g(i)] for i in [0,1,2,3,6,10,20]]
   (102)
   [[0,1],[1,0],[2,- 1],[3,0],[6,- 61],[10,- 50521],[20,370371188237525]]

(103) -> [[i,euler(i)] for i in [0,1,2,3,6,10,20]]
   (103)
   [[0,1],[1,0],[2,- 1],[3,0],[6,- 61],[10,- 50521],[20,370371188237525]]

0

APL(NARS), 42 chars, 84 bytes

E←{0≥w←⍵:1⋄1-+/{(⍵!w)×(2*w-1+⍵)×E⍵}¨¯1+⍳⍵}

按照“ smls”中显示的公式进行测试:

  E 0
1
  E 1
0
  E 3
0
  E 6
¯61
  E 10
¯50521

最后一种情况作为结果返回一个大有理数,因为我输入20x(大有理数20/1)而不是20,因为我认为20.0浮动64位...

  E 20x
370371188237525 

如果一个很快返回0,会更快一些,但是会更长一些(50个字符):

  E←{0≥w←⍵:1⋄0≠2∣w:0⋄1-+/{(⍵!w)×(2*w-1+⍵)×E⍵}¨¯1+⍳⍵}
  E 30x
¯441543893249023104553682821 

如果使用有问题的定义,将会更快(并且会更长一些75个字符):

  f←{0≥⍵:1⋄0≠2∣⍵:0⋄0J1×+/{+/⍵{⍺÷⍨(0J2*-⍺)×(⍵!⍺)×(¯1*⍵)×(⍺-2×⍵)*n}¨0..⍵}¨⍳n←1+⍵}
  f 0
1
  f 1
0
  f 3
0
  f 6
¯61J0 
  f 10
¯50521J¯8.890242766E¯9 
  f 10x
¯50521J0 
  f 20x
370371188237525J0 
  f 30x
¯441543893249023104553682821J0 
  f 40x
14851150718114980017877156781405826684425J0 
  f 400x
290652112822334583927483864434329346014178100708615375725038705263971249271772421890927613982905400870578615922728
  107805634246727371465484012302031163270328101126797841939707163099497536820702479746686714267778811263343861
  344990648676537202541289333151841575657340742634189439612727396128265918519683720901279100496205972446809988
  880945212776281115581267184426274778988681851866851641727953206090552901049158520028722201942987653512716826
  524150450130141785716436856286094614730637618087804268356432570627536028770886829651448516666994497921751407
  121752827492669601130599340120509192817404674513170334607613808215971646794552204048850269569900253391449524
  735072587185797183507854751762384660697046224773187826603393443429017928197076520780169871299768968112010396
  81980247383801787585348828625J0 

上面的结果是一个只有实数的复数。

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.