Find the smallest positive integer which has all integers from 1 to n as factors The question's pretty much described by the title: write a program or function that takes a positive integer n as input, and returns the smallest positive output which has all integers from 1 to n as factors. (Another way of looking at this is that you're looking for the least common multiple of the integers from 1 to n.)

This is a code-golf challenge, so the shortest entry in bytes wins.

Inspired by this challenge. (This challenge is basically the inverse of that one.)

Test cases


Input: 1; Output: 1
Input: 6; Output: 60
Input: 19; Output: 232792560
Input: 22; Output: 232792560


You don't need to worry about integer precision issues (unless your language has integers so small that this would trivialise the problem). If your language does have bignums, though, you might also want to test your program on this case, just for fun:


Input: 128; Output: 13353756090997411579403749204440236542538872688049072000
Python, 46 bytes



g=lambda n,c=0:n&lt;1or(c%n&lt;1)*c or g(n,c+g(n-1))


Take that, past xnor!



50 bytes:

g=lambda n,i=1:n&lt;1or(i*n%g(n-1)&lt;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&lt;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&lt;1or g(n-1)*i*(g(n-1)*i%n&lt;1)or g(n,i+1)

g=lambda n,i=1:n&lt;1or(i*g(n-1)%n&lt;1)*i*g(n-1)or g(n,i+1)

g=lambda n,r=1,c=1:r if n&lt;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&lt;1)or r%n and g(n,r+c,c)or g(n-1,r,r)

g=lambda n,i=1:n&lt;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&lt;1or g(n-1)*i%n and g(n,i+1)or g(n-1)*i

g=lambda n:n&lt;1or min(range(n,3**n,n),key=g(n-1).__rmod__)

g=lambda n,i=1:n&lt;1or(i*n%g(n-1)&lt;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&gt;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!