How to find the number that's evenly divisible by a set of numbers from 1 to n?

  • Thread starter Thread starter newb
  • Start date Start date
  • Tags Tags
    Numbers Set
AI Thread Summary
To find the smallest number that is evenly divisible by all integers from 1 to n, the problem can be framed as calculating the least common multiple (LCM) of that range. The LCM can be derived using the formula LCM(a, b) = |a*b|/GCD(a, b), where GCD is easily computed using the Euclidean algorithm. For multiple numbers, the LCM can be found iteratively by calculating LCM of pairs, such as LCM(a, b), then LCM(LCM(a, b), c), and so on. While brute force methods can work for smaller n, they are inefficient for larger values like n=20. Utilizing the LCM and GCD approach is a more elegant and efficient solution.
newb
Messages
10
Reaction score
0
How can you go about finding the smallest number (x) that is evenly divisible by a set of numbers from 1 to n?

For example:

If n = 3, x=6
n = 4, x=12
n=5, x=60
n=6, x=60


This is easy to do when n is a small number, however how would one go about finding the answer if n=20 for example?

I have written a program that tries to brute force the answer, but I'm almost certain there's a much more elegant way to do it. Any advice?
 
Mathematics news on Phys.org
Here's the program I wrote by the way, incase anyone is interested:
Code:
class Problem5 {
 static int[] theArray;
static int multiplyer = 11 ; 
 static boolean test= false;
static int current;


 //Makes array
 void maker (int number){
   for(int i=number; i>0; i--){
     theArray[i-1]=i;
     
   }
  }   
   
  public static void main (String[] args){
 
   Problem5 instance1 = new Problem5();
   theArray = new int[multiplyer];
   instance1.maker(multiplyer);
   current = theArray[theArray.length-1];
   System.out.println (current);
   
   while (test == false){
     for(int i = theArray.length; i>0; i--){
     if (current % theArray[i-1]>0){
       current = current + theArray[multiplyer -1];
       i=0;
       test = false;
     } else{
       System.out.println(current+" is divisible by "+ theArray[i-1]);
       test = true;
     }
    }  
   }
     
  }
     
}
 
Last edited by a moderator:
Phrased another way, your problem is to find the lowest common multiple (LCM) of a set of numbers from 1,...,n . Luckily, there is an easy formula to get the LCM for two numbers a and b from the greatest common divisor (GCD): LCM(a,b) = |a*b|/GCD(a,b) . I say luckily because finding the GCD is easy - just use the Euclidean algorithm. There's probably a snazzy way to deal with this for more than two numbers but you could adopt it by finding LCM(a,b), then LCM(LCM(a,b),c), etc. Check out http://en.wikipedia.org/wiki/Least_common_multiple

If you stick with a brute force approach, remember that apart from {1}, each set contains 2. Think about what this means for the LCM...
 
Seemingly by some mathematical coincidence, a hexagon of sides 2,2,7,7, 11, and 11 can be inscribed in a circle of radius 7. The other day I saw a math problem on line, which they said came from a Polish Olympiad, where you compute the length x of the 3rd side which is the same as the radius, so that the sides of length 2,x, and 11 are inscribed on the arc of a semi-circle. The law of cosines applied twice gives the answer for x of exactly 7, but the arithmetic is so complex that the...
Is it possible to arrange six pencils such that each one touches the other five? If so, how? This is an adaption of a Martin Gardner puzzle only I changed it from cigarettes to pencils and left out the clues because PF folks don’t need clues. From the book “My Best Mathematical and Logic Puzzles”. Dover, 1994.
Thread 'Imaginary Pythagoras'
I posted this in the Lame Math thread, but it's got me thinking. Is there any validity to this? Or is it really just a mathematical trick? Naively, I see that i2 + plus 12 does equal zero2. But does this have a meaning? I know one can treat the imaginary number line as just another axis like the reals, but does that mean this does represent a triangle in the complex plane with a hypotenuse of length zero? Ibix offered a rendering of the diagram using what I assume is matrix* notation...
Back
Top