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!
Could you add some test cases? – Zgarb – 2017-01-04T10:57:33.487
2I remember golfing this precise task and have code already written for it in a file created in August, but I can't find a dupe. – xnor – 2017-01-04T11:02:12.543
@Zgarb: I've added some cases as you requested. – None – 2017-01-04T11:02:59.830
@xnor Related
– Zgarb – 2017-01-04T11:03:51.820I take it the input is positive? – xnor – 2017-01-04T11:05:44.360
@xnor: Edited to clarify. (The question doesn't make much sense with negative/zero inputs anyway.) – None – 2017-01-04T11:08:25.263
Was this from this sandbox post
– FlipTack – 2017-01-04T15:14:25.947This was "sandboxed" in chat, rather than on Meta. I searched for duplicates and failed to find them, although this is partly because a) I wasn't thinking of it as a lowest common multiple question when I asked it, and b) the original duplicate question doesn't actually use the words "lowest" (or "least") common multiple (it's hyphenated). I agree it's a duplicate, though; at this point the correct fix is probably an answer merge, but I don't have the permissions to do that so I'll close it as duplicate in the meantime. – None – 2017-01-04T17:03:31.057
I've now edited the older post asking the same question so that it actually contains words that would make it show up on searches. (Insert famous quote about how users keep finding ways to ask the same question in two different ways with no words in common…) – None – 2017-01-04T17:07:02.283
@FlipTack I made that sandbox post, but felt it was a duplicate of Mego's question. I was wrong, thought I should post it anyway, and then found this. Turns out it IS a dupe, but not of Mego's Q.
– steenbergh – 2017-01-04T17:58:51.257