Tuesday, January 8, 2013

Problem 5: Smallest multiple


Q)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 positive number that is evenly divisible by all of the numbers from 1 to 20
Solution:
Java code:
there are faster solutions to this one , but I am not giving it away, Here's the hint to it: "Prime Numbers"



public class Prob5 {

 
 public static void main(String[] args) {
  long mul =1;
  long small = 0;
  for (long i =1; i<21; i++)
  {
   mul *= i;
  }
  
 boolean flag;
  long k =1;
 
  for(long j = 1; j < mul /1000000 ; j++)
  {
   
   if (mul % j == 0)
   {
   k = 1;
   flag = true;
   while (k <21)
   { 
    if(j % k !=0)
    {
     flag = false;
     break;
    }
    
    k++;
   }
   if (flag)
   {
    small = j;
    break;
   }
  }
   
  }
  System.out.println(small);
 }

}

No comments:

Post a Comment