Which Day of Christmas is it?
Python, 27 bytes
lambda n:int((n*6)**.33359)
Try it online!
A direct formula with some curve-fitting, same as the original one found by Level River St.
The shifted equation i**3-i==n*6
is close to i**3==n*6
for large i
. It solves to i=(n*6)**(1/3)
. Taking the floor the rounds down as needed, compensating for the off-by-one.
But, there are 6 inputs on boundaries where the error takes it below an integer it should be above. All of these can be fixed by slightly increasing the exponent without introducing further errors.
Python, 38 bytes
f=lambda n,i=1:i**3-i<n*6and-~f(n,i+1)
The formula n=i*(i+1)*(i+2)/6
for tetrahedral numbers can be more nicely written in i+1
as n*6=(i+1)**3-(i+1)
. So, we find the lowest i
for which i**3-i<n*6
. Each time we increment i
starting from 1, the recursive calls adds 1
to the output. Starting from i=1
rather than i=0
compensates for the shift.
J, 12 bytes
2>.@-~3!inv]
There might be a golfier way to do this, but this is a lovely opportunity to use J's built-in function inversion.
Try it online!
How it works
2>.@-~3!inv] Monadic verb. Argument: n
] Right argument; yield n.
3 Yield 3.
!inv Apply the inverse of the ! verb to n and 3. This yields a real number.
x!y computes Π(y)/(Π(y-x)Π(x)), where Π is the extnsion of the
factorial function to the real numbers. When x and y are non-negative
integers, this equals yCx, the x-combinations of a set of order y.
>.@-~ Combine the ceil verb (>.) atop (@) the subtraction verb (-) with
swapped arguments (~).
2 Call it the combined verbs on the previous result and 2.
Python, 22 bytes
lambda n:n**.3335//.55
Heavily inspired by @xnor's Python answer.
Try it online!