Try it online!
Valid for all values (as required), but not for generally (outputs for odd s.) The code is modified to reduce one byte by changing the output to where is defined as below. Specifically, the recurrence formula for is
Try it online!
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 of different orders.
Here I'll use some convenient notation. Let and . Then we have
Since and , we can deduce that
Let and , we can rewrite the relation above as
That is, contributes to both and . As a result, we can write in terms of and :
with initial condition and where .
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 and , equals the coefficient of in the expansion of , which is .
For branches that can never be reached, the recurrences always end at 0, so where or is odd. The latter one, particularly, implies that for all odd s. For even s strictly larger than , the recurrence may eventually allow to happen at some point, but before that step it must reach a point where , and the recurrence formula shows that the value must be 0 at that point (since the first term is multiplied by , and the second term is farther from the "triangle" of ). As a result, where . This completes the proof of the validity of the algorithm.
The code can be modified to calculate three more related sequences:
Tangent Numbers (46 bytes)
Secant Numbers (45 bytes)
Euler Zigzag Numbers (48 bytes)