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...
 
Insights auto threads is broken atm, so I'm manually creating these for new Insight articles. In Dirac’s Principles of Quantum Mechanics published in 1930 he introduced a “convenient notation” he referred to as a “delta function” which he treated as a continuum analog to the discrete Kronecker delta. The Kronecker delta is simply the indexed components of the identity operator in matrix algebra Source: https://www.physicsforums.com/insights/what-exactly-is-diracs-delta-function/ by...
Suppose ,instead of the usual x,y coordinate system with an I basis vector along the x -axis and a corresponding j basis vector along the y-axis we instead have a different pair of basis vectors ,call them e and f along their respective axes. I have seen that this is an important subject in maths My question is what physical applications does such a model apply to? I am asking here because I have devoted quite a lot of time in the past to understanding convectors and the dual...
Back
Top