The Lehmer-Comtet sequence is a sequence such that a(n) is the nth derivative of f(x) = xx with respect to x as evaluated at x = 1.
Task
Take a non-negative integer as input and output the nth term of the Lehmer-Comtet sequence.
This is code-golf so you should minimize the file size of your source code.
Test Cases
Here are the first couple terms in order (copied from the OEIS)
1, 1, 2, 3, 8, 10, 54, -42, 944, -5112, 47160, -419760, 4297512, -47607144, 575023344, -7500202920, 105180931200, -1578296510400, 25238664189504, -428528786243904, 7700297625889920, -146004847062359040, 2913398154375730560, -61031188196889482880
10 Answers 10
Haskell, (削除) 77 (削除ここまで) 75 bytes, no differentiation builtins
x@(a:b)&y@(c:d)=a*c:zipWith(+)(b&y)(x&d)
s=1:s&(1:scanl(*)1[-1,-2..])
(s!!)
How it works
We represent a function as its infinite list of Taylor series coefficients about \$x = 1\$:
$$ f(x) = \sum_{n=0}^\infty \frac{f^{(n)}(1)}{n!} (x - 1)^n $$
is represented by \$[f(1), f'(1), f''(1), ...]\$.
The &
operator multiplies two such functions using the product rule. This lets us recursively define the function \$s(x) = x^x\$ in terms of itself using the differential equation
$$ s(1) = 1, \\ s'(x) = s(x) \cdot (1 + \ln x), $$
where
$$ \ln x = \sum_{n=1}^\infty \frac{(-1)^{n-1}(n - 1)!}{n!}(x - 1)^n. $$
Mathematica, 19 bytes
D[x^x,{x,#-1}]/.x->1&
-18 bytes from @Not a tree
-
9\$\begingroup\$ Unless I'm missing something, you can get this a lot shorter:
D[x^x,{x,#}]/.x->1&
, 19 bytes. \$\endgroup\$Not a tree– Not a tree2017年07月03日 00:13:56 +00:00Commented Jul 3, 2017 at 0:13 -
\$\begingroup\$ actually 21 bytes.. but yes! a lot shorter! \$\endgroup\$ZaMoC– ZaMoC2017年07月03日 06:52:25 +00:00Commented Jul 3, 2017 at 6:52
-
\$\begingroup\$ I don't think you need the
-1
— the sequence from OEIS starts at n = 0. \$\endgroup\$Not a tree– Not a tree2017年07月03日 07:11:02 +00:00Commented Jul 3, 2017 at 7:11 -
1\$\begingroup\$ ok then! 19 bytes it is \$\endgroup\$ZaMoC– ZaMoC2017年07月03日 07:15:49 +00:00Commented Jul 3, 2017 at 7:15
Octave with Symbolic Package, (削除) 36 (削除ここまで) 32 bytes
syms x
@(n)subs(diff(x^x,n),x,1)
The code defines an anonymous function which outputs a symbolic variable with the result.
Haskell, 57 bytes
f 0=1
f n=f(n-1)-foldl(\a k->f(k-1)/(1-n/k)-a*k)0[1..n-1]
No built-ins for differentiating or algebra. Outputs floats.
Python with SymPy, (削除) 77 (削除ここまで) (削除) 75 (削除ここまで) (削除) 58 (削除ここまで) 57 bytes
1 byte saved thanks to @notjagan
17 bytes saved thanks to @AndersKaseorg
from sympy import*
lambda n:diff('x^x','x',n).subs('x',1)
-
1\$\begingroup\$
lambda n:diff('x**x','x',10).subs('x',1)
doesn’t requiresympy.abc
. \$\endgroup\$Anders Kaseorg– Anders Kaseorg2017年07月02日 22:42:13 +00:00Commented Jul 2, 2017 at 22:42 -
1\$\begingroup\$ Ummm ... where do you use
n
? \$\endgroup\$Adalynn– Adalynn2017年07月02日 23:05:33 +00:00Commented Jul 2, 2017 at 23:05 -
\$\begingroup\$ @ZacharyT thanks! coincidentally I tested anders' proposal right with n=10, so it gave the same result :) fixed now \$\endgroup\$Uriel– Uriel2017年07月02日 23:13:13 +00:00Commented Jul 2, 2017 at 23:13
-
\$\begingroup\$ -1 byte by replacing
x**x
withx^x
. \$\endgroup\$notjagan– notjagan2017年07月03日 01:17:01 +00:00Commented Jul 3, 2017 at 1:17
Python 3, 150 bytes
lambda n:0**n or sum(L(n-1,r)for r in range(n))
L=lambda n,r:0<=r<=n and(0**n or n*L(n-2,r-1)+L(~-n,r-1)+(r-~-n)*L(~-n,r)if r else n<2or-~-n*L(n-1,0))
Exponential runtime complexity. Uses the formula given in the OEIS page.
-
\$\begingroup\$
n>=r>=0
saves a byte. \$\endgroup\$2017年07月03日 03:00:35 +00:00Commented Jul 3, 2017 at 3:00 -
\$\begingroup\$ You can also save a byte by putting
0**n
aftersum(...)
. \$\endgroup\$2017年07月03日 03:05:13 +00:00Commented Jul 3, 2017 at 3:05 -
\$\begingroup\$ You can rearrange your addition to save a third byte. \$\endgroup\$2017年07月03日 03:09:19 +00:00Commented Jul 3, 2017 at 3:09
-
\$\begingroup\$ Save two bytes by using bitwise or \$\endgroup\$2017年07月03日 03:12:58 +00:00Commented Jul 3, 2017 at 3:12
-
\$\begingroup\$ Save two more bytes by using
n<1
instead of0**n
\$\endgroup\$2017年07月03日 03:23:53 +00:00Commented Jul 3, 2017 at 3:23
Python 3, (削除) 288 (削除ここまで) 261 bytes
Differentiation without differentiation built-in.
p=lambda a,n:lambda v:v and p(a*n,n-1)or a
l=lambda v:v and p(1,-1)
e=lambda v:v and m(e,a(p(1,0),l))or 1
a=lambda f,g:lambda v:v and a(f(1),g(1))or f(0)+g(0)
m=lambda f,g:lambda v:v and a(m(f(1),g),m(g(1),f))or f(0)*g(0)
L=lambda n,f=e:n and L(n-1,f(1))or f(0)
How it works
Each of the first five lines define functions and their derivatives and their results when evaluated at 1
. Their derivatives are also functions.
p
is power i.e.a*x^n
l
is logarithm i.e.ln(x)
e
is exponential i.e.exp(x)
a
is addition i.e.f(x)+g(x)
m
is multiplication i.e.f(x)*g(x)
Usage: for example, exp(ln(x)+3x^2)
would be represented as e(l()+p(3,2))
. Let x=e(l()+p(3,2))
. To find its derivative, call x(1)
. To find its result when evaluated at 1
, call x(0)
.
Bonus: symbolic differentiation
-
\$\begingroup\$ You can save a lot of bytes by using
exec
compression. Try it online! \$\endgroup\$2017年07月03日 04:08:06 +00:00Commented Jul 3, 2017 at 4:08
Python3+mpmath 52 bytes
from mpmath import*
lambda n:diff(lambda x:x**x,1,n)
-3 bytes, Thanks @Zachary T
-
1\$\begingroup\$ You should change the language to python3+mpmath, since mpmath is not a standard library. \$\endgroup\$2017年07月02日 22:42:12 +00:00Commented Jul 2, 2017 at 22:42
-
2\$\begingroup\$ You can change your first line to
from mpmath import*
, and the second todiff(lambda x:x**x,1,n)
. (just removing unnecessary spaces) \$\endgroup\$Adalynn– Adalynn2017年07月02日 23:04:15 +00:00Commented Jul 2, 2017 at 23:04