Given a non-negative integer \$n ,\$ output the \$n^{\text{th}}\$ Euler number (OEIS A122045).

All odd-indexed Euler numbers are \$0 .\$ The even-indexed Euler numbers can be computed with the following formula (\$i \equiv \sqrt{-1}\$ refers to the imaginary unit): $$ E_{2n} = i \sum_{k=1}^{2n+1}{ \sum_{j=0}^{k}{ \left(\begin{array}{c}k \\ j \end{array}\right) \frac{{\left(-1\right)}^{j} {\left(k-2j\right)}^{2n+1}}{2^k i^k k} } } \,. $$


  • \$n\$ will be a non-negative integer such that the \$n^{\text{th}}\$ Euler number is within the representable range of integers for your language.

Test Cases

0 -> 1
1 -> 0
2 -> -1
3 -> 0
6 -> -61
10 -> -50521
20 -> 370371188237525
  • 1
    \$\begingroup\$ @donbright You're missing a set of parentheses: wolframalpha.com/input/… - with that, the two summands are both -i/2, which yield -i when added. Multiply that by the i outside of the summation, and you get 1. \$\endgroup\$ – user45941 Jan 22 '17 at 21:01

16 Answers 16


Mathematica, 6 bytes



  • 9
    \$\begingroup\$ When I saw the title, I immediately thought "Surely Mathematica must have a builtin for this". \$\endgroup\$ – HyperNeutrino Jan 21 '17 at 2:48
  • 12
    \$\begingroup\$ That applies to pretty much every title... even detecting goats in images \$\endgroup\$ – Luis Mendo Jan 21 '17 at 16:19
  • \$\begingroup\$ GoatImageQ is underappreciated \$\endgroup\$ – Greg Martin Jan 21 '17 at 19:34
  • 1
    \$\begingroup\$ Can you explain this? Edit: this was a joke. \$\endgroup\$ – Magic Octopus Urn Jan 24 '17 at 21:30

J, 10 bytes


Try it online!

Uses the definition for the exponential generating function sech(x).

  • \$\begingroup\$ Does J do symbolic analysis to get the generating function? It doesn't run into floating point errors even for n=30. \$\endgroup\$ – orlp Jan 21 '17 at 0:49
  • \$\begingroup\$ @orlp I'm not sure what it does internally, but J knows the Taylor series for a subset of verbs. Any function you can define using a combination of those verbs will be valid for t. or t: where are g.f. and e.g.f. A curious note is that tan(x) is not supported but sin(x)/cos(x) is. \$\endgroup\$ – miles Jan 21 '17 at 0:57

Pari/GP, 32 bytes


Try it online!

  • 1
    \$\begingroup\$ The built-in function is eulerfrac. \$\endgroup\$ – Jeppe Stig Nielsen Dec 19 '20 at 12:16

Maple, 5 bytes


Hurray for builtins?

  • 4
    \$\begingroup\$ Love it when mathematica is too verbose for a maths problem... \$\endgroup\$ – theonlygusti Jan 21 '17 at 7:48

Maxima, 5 bytes / 42 bytes

Maxima has a built in:


Try it online!

The following solution does not require the built in from above, and uses the formula that originally defined the euler numbers.

We are basically looking for the n-th coefficient of the series expansion of 1/cosh(t) = sech(t) (up to the n!)


Try it online!


Mathematica, without built-in, 18 bytes

Using @rahnema1's formula:


21 bytes:


Python 2.7, 46 bytes

Using scipy.

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

Perl 6, 78 bytes

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

Uses the iterative formula from here:

$$E_n = 1 - \sum_{k=0}^{n-1} \left[ E_k \cdot 2^{(n-1-k)} \cdot \binom{n}{k} \right]$$

How it works

The general structure is a lambda in which an infinite sequence is generated, by an expression that is called repeatedly and gets all previous values of the sequence in the variable @E, and then that sequence is indexed with the lambda argument:

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

The expression called for each step of the sequence, is:

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

JavaScript (Node.js), 70 46 44 bytes


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 \$\mathrm{sech}(x)\$ of different orders.


Here I'll use some convenient notation. Let \$T^n:=\mathrm{tanh}^n(t)\$ and \$S^n:=\mathrm{sech}^n(t)\$. Then we have


Since \$\frac{dT}{dt}=S^2\$ and \$\frac{dS}{dt}=-TS\$, we can deduce that

$$ \begin{equation} \begin{aligned} \frac{d}{dt}(T^aS^b)&=aT^{a-1}(S^2)(S^b)+bS^{b-1}(-TS)(T^a) \\ &=aT^{a-1}S^{b+2}-bT^{a+1}S^b \end{aligned} \end{equation} $$

Let \$b=i+1\$ and \$a=n-i\$, we can rewrite the relation above as

$$ \begin{equation} \begin{aligned} \frac{d}{dt}(T^{n-i}S^{i+1})&=(n-i)T^{n-i-1}S^{i+3}-(i+1)T^{n-i+1}S^{i+1}\\ &=(n-i)T^{(n+1)-(i+2)}S^{(i+2)+1}-(i+1)T^{(n+1)-i}S^{i+1} \end{aligned} \end{equation} $$

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(n-1,i-2)\$ and \$F(n-1,i)\$:


with initial condition \$F(0,0)=1\$ and \$F(0,i)=0\$ where \$i\neq 0\$.

The related part of the code a?F(--a,b)*~b+F(a,b-=2)*(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   ]
+F(a,b-=2)               // +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\$, \$E_n\$ equals the coefficient of \$S^{n+1}\$ in the expansion of \$\frac{d^nS}{dt^n}\$, 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 \$E_n=0\$ for all odd \$n\$s. For even \$i\$s strictly larger than \$n\$, the recurrence may eventually allow \$0\leq i\leq n\$ 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 \$n-i+1=n-(n+1)+1=0\$, and the second term is farther from the "triangle" of \$0\leq i\leq n\$). As a result, \$F(n,i)=0\$ where \$i > n\$. This completes the proof of the validity of the algorithm.

enter image description here


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

Tangent Numbers (46 bytes)


Secant Numbers (45 bytes)


Euler Zigzag Numbers (48 bytes)


Maxima, 29 bytes


Try It Online!

Two times imaginary part of polylogarithm function of order -n with argument i [1]


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.


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.

_$12 0g2%2*-*10g20g110g20g-240pv^1g03:_^*

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.


Python2, (sympy rational), 153 bytes

from sympy import *
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
    \$\begingroup\$ 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). \$\endgroup\$ – FlipTack Jan 25 '17 at 21:20

Axiom, 5 bytes


for OEIS A122045; this is 57 bytes


test code and results

(102) -> [[i,g(i)] for i in [0,1,2,3,6,10,20]]
   [[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]]
   [[0,1],[1,0],[2,- 1],[3,0],[6,- 61],[10,- 50521],[20,370371188237525]]

CJam (34 bytes)


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.


{           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

Wolfram Language (Mathematica), 47 46 bytes

Without using any special functions:


Try it online!


APL(NARS), 42 chars, 84 bytes


Follow the formula showed from "smls", test:

  E 0
  E 1
  E 3
  E 6
  E 10

the last case return one big rational as result because i enter 20x (the big rational 20/1) and not 20 as i think 20.0 float 64 bit...

  E 20x

It would be more fast if one return 0 soon but would be a little more long (50 chars):

  E 30x

it would be more fast if it is used the definition on question (and would be a little more long 75 chars):

  f 0
  f 1
  f 3
  f 6
  f 10
  f 10x
  f 20x
  f 30x
  f 40x
  f 400x

The result above it is one complex number that has only the real part.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy