Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 53943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:08:33+00:00 2026-05-10T17:08:33+00:00

I read an interesting DailyWTF post today, Out of All The Possible Answers… and

  • 0

I read an interesting DailyWTF post today, ‘Out of All The Possible Answers…’ and it interested me enough to dig up the original forum post where it was submitted. This got me thinking how I would solve this interesting problem – the original question is posed on Project Euler as:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

To reform this as a programming question, how would you create a function that can find the Least Common Multiple for an arbitrary list of numbers?

I’m incredibly bad with pure math, despite my interest in programming, but I was able to solve this after a little Googling and some experimenting. I’m curious what other approaches SO users might take. If you’re so inclined, post some code below, hopefully along with an explanation. Note that while I’m sure libraries exist to compute the GCD and LCM in various languages, I’m more interested in something that displays the logic more directly than calling a library function 🙂

I’m most familiar with Python, C, C++, and Perl, but any language you prefer is welcome. Bonus points for explaining the logic for other mathematically-challenged folks out there like myself.

EDIT: After submitting I did find this similar question Least common multiple for 3 or more numbers but it was answered with the same basic code I already figured out and there’s no real explanation, so I felt this was different enough to leave open.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T17:08:34+00:00Added an answer on May 10, 2026 at 5:08 pm

    This problem is interesting because it doesn’t require you to find the LCM of an arbitrary set of numbers, you’re given a consecutive range. You can use a variation of the Sieve of Eratosthenes to find the answer.

    def RangeLCM(first, last):     factors = range(first, last+1)     for i in range(0, len(factors)):         if factors[i] != 1:             n = first + i             for j in range(2*n, last+1, n):                 factors[j-first] = factors[j-first] / factors[i]     return reduce(lambda a,b: a*b, factors, 1) 

    Edit: A recent upvote made me re-examine this answer which is over 3 years old. My first observation is that I would have written it a little differently today, using enumerate for example. A couple of small changes were necessary to make it compatible with Python 3.

    The second observation is that this algorithm only works if the start of the range is 2 or less, because it doesn’t try to sieve out the common factors below the start of the range. For example, RangeLCM(10, 12) returns 1320 instead of the correct 660.

    The third observation is that nobody attempted to time this answer against any other answers. My gut said that this would improve over a brute force LCM solution as the range got larger. Testing proved my gut correct, at least this once.

    Since the algorithm doesn’t work for arbitrary ranges, I rewrote it to assume that the range starts at 1. I removed the call to reduce at the end, as it was easier to compute the result as the factors were generated. I believe the new version of the function is both more correct and easier to understand.

    def RangeLCM2(last):     factors = list(range(last+1))     result = 1     for n in range(last+1):         if factors[n] > 1:             result *= factors[n]             for j in range(2*n, last+1, n):                 factors[j] //= factors[n]     return result 

    Here are some timing comparisons against the original and the solution proposed by Joe Bebel which is called RangeEuclid in my tests.

    >>> t=timeit.timeit >>> t('RangeLCM.RangeLCM(1, 20)', 'import RangeLCM') 17.999292996735676 >>> t('RangeLCM.RangeEuclid(1, 20)', 'import RangeLCM') 11.199833288867922 >>> t('RangeLCM.RangeLCM2(20)', 'import RangeLCM') 14.256165588084514 >>> t('RangeLCM.RangeLCM(1, 100)', 'import RangeLCM') 93.34979585394194 >>> t('RangeLCM.RangeEuclid(1, 100)', 'import RangeLCM') 109.25695507389901 >>> t('RangeLCM.RangeLCM2(100)', 'import RangeLCM') 66.09684505991709 

    For the range of 1 to 20 given in the question, Euclid’s algorithm beats out both my old and new answers. For the range of 1 to 100 you can see the sieve-based algorithm pull ahead, especially the optimized version.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 70k
  • Answers 70k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer The problem is that you are trying to join to… May 11, 2026 at 1:02 pm
  • added an answer How I solved my problem A Perl port of pickle.py… May 11, 2026 at 1:02 pm
  • added an answer It is not that they behave differently, but that almost… May 11, 2026 at 1:02 pm

Related Questions

I read an interesting DailyWTF post today, Out of All The Possible Answers... and
Recently, I read an article entitled SATA vs. SCSI reliability . It mostly discusses
A good while ago, I read an article by the creator of viemu ,
I have read an example and tried to duplicate it's methods but with weird
How do I programmatically read an incoming email with .NET. I need a method
I ran into an interesting (and very frustrating) issue with the equals() method today
I've been working with providers a fair bit lately, and I came across an
I am writing a website in my spare time for an educational facility and
How are you instrumenting your UI's? In the past I've read that people have

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.