Python, 46 bytes
g=lambda n,c=0:n<1or(c%n<1)*c or g(n,c+g(n-1))
Take that, past xnor!
50 bytes:
g=lambda n,i=1:n<1or(i*n%g(n-1)<1)*i*n or g(n,i+1)
I apparently golfed this problem 5 months ago, and I don't remember how this code works or why I golfed it. I think I have a golfing problem.
(Edit: It's from this answer. Thanks to Dennis for inspiring the solution and saving 4 bytes.)
Apparently, this code recursively finds lcm(1..n)
as lcm(lcm(1..n-1),n)
. So, the function g
expresses g(n)
as the smallest positive multiple i*n
of n
that's also a multiple of g(n-1)
. (It could instead search multiples of g(n-1)
for multiples of n
, but this is golfier because g(n-1)
only needs to be referenced once. Thanks to Dennis for this improvement.)
Here's the rest of the file, full of other golfing attempts:
f=lambda a,b:a and f(b%a,a)or b
g=lambda n:reduce(lambda a,b:a*b/f(a,b),range(1,n+1))
f=lambda a,b:a and f(b%a,a)or b
g=lambda n:n==1 or n*g(n-1)/f(n,g(n-1))
import math
g=lambda n:n==1 or n*g(n-1)/math.gcd(n,g(n-1))
g=lambda n,k=1:k*all(k%~i==0for i in range(n))or g(n,k+1)
g=lambda n,k=1:min(k%~i for i in range(n))and g(n,k+1)or k
l=lambda a,b:a%b and l(b,a%b)*a/(a%b)or a
g=lambda n:n<1or l(n,g(n-1))
g=lambda n,i=1:n==0 or (g(n-1)*i if g(n-1)*i%n==0 else g(n,i+1))
g=lambda n,i=1:n<1or g(n-1)*i*(g(n-1)*i%n<1)or g(n,i+1)
g=lambda n,i=1:n<1or(i*g(n-1)%n<1)*i*g(n-1)or g(n,i+1)
g=lambda n,r=1,c=1:r if n<2 else (g(n-1,r,r)if r%n==0 else g(n,r+c,c))
g=lambda n,r=1,c=1:r*(n<1)or r%n and g(n,r+c,c)or g(n-1,r,r)
g=lambda n,i=1:n<1or(i*g(n-1)%n or i)*g(n-1)or g(n,i+1)
def g(n):
if n==0:return 1
a=b=g(n-1)
while b%n:b+=a
return b
g=lambda n,i=1:n<1or g(n-1)*i%n and g(n,i+1)or g(n-1)*i
g=lambda n:n<1or min(range(n,3**n,n),key=g(n-1).__rmod__)
g=lambda n,i=1:n<1or(i*n%g(n-1)<1)*i*n or g(n,i+1)
r=n=input()
while n:
c=r
while r%n:r+=c
n-=1
print r
r=n=input()
while n:
c=r
while r%n:r+=c
n-=1
print r
r=1
for n in range(input()):
c=r
while r%~n:r+=c
print r
r=n=input()
while n:
exec"r+=c*(r%n>0);"*n
n-=1;c=r
print r
r=n=input();exec("r+=r%n and c;"*n+"n-=1;c=r;")*n;print r
It's eerie looking at my own past work. In trying to golf it, I keep thinking "maybe I can save some bytes by ..." and then seeing there's already a piece of code that attempted to do just that.
You think you've thought of everything, past xnor? Well, I'll show you!