Note that there are some explanatory texts on larger screens.

plurals
  1. POFinding the LCM of a range of numbers
    text
    copied!<p>I read an interesting DailyWTF post today, <a href="http://thedailywtf.com/Articles/Out-of-All-the-Possible-Answers.aspx" rel="nofollow noreferrer">"Out of All The Possible Answers..."</a> and it interested me enough to dig up the original <a href="http://forums.thedailywtf.com/forums/t/10030.aspx" rel="nofollow noreferrer">forum post</a> where it was submitted. This got me thinking how I would solve this interesting problem - the original question is posed on <a href="http://projecteuler.net/index.php?section=problems&amp;id=5" rel="nofollow noreferrer">Project Euler</a> as: </p> <blockquote> <p>2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.</p> <p>What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?</p> </blockquote> <p>To reform this as a programming question, <strong>how would you create a function that can find the Least Common Multiple for an arbitrary list of numbers?</strong></p> <p>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 :-) </p> <p>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.</p> <p><strong>EDIT</strong>: After submitting I did find this similar question <a href="https://stackoverflow.com/questions/147515">Least common multiple for 3 or more numbers</a> 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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload