1 package com.sam.twisters.euler; 2 /* Problem 5 : Euler 3 * 2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. 4 * What is the smallest number that is evenly divisible (divisible without reminder) by all of the numbers from 1 to 20? 5 */ 6 7 public class prog5 { 8 /*Simple function to check if the divisor completely divides the divident*/ 9 public static boolean isDivisble(int divident, int divisor) { 10 if(divident%divisor == 0){ 11 return true; 12 } 13 else{ 14 return false; 15 } 16 } 17 18 public static void main(String[] args) 19 { 20 long startTime = System.nanoTime(); 21 boolean va = true; 22 23 /* Start looping though all the numbers and see if we can find one divisible by all numbers 24 * from 1 to 20. 25 */ 26 int i = 1; 27 do{ 28 va = true; //Assume that i is a valid answer to the puzzle 29 //Optimization -->for(int d=1;d<=20;d++){ 30 /*(1 char, start from 11 and not 1). 31 This works since atleast one of the numbers from 11 to 20 has each of the numbers 1-10 as a factor, 32 so for example when we test if a number is divisble by 12 - we also indirectly test that the 33 number is divisible by 2,3,4 and 6 34 */ 35 for(int d=11;d<=20;d++){ 36 if (!isDivisble(i, d)){ //Yikes my assumption was wrong! 37 va = false; 38 //Optimization -->break; (6 char) 39 break; 40 } 41 } 42 i++; 43 }while(!va); 44 System.out.println(i); 45 long estimatedTime = System.nanoTime() - startTime; 46 System.out.println((float)estimatedTime/1000000000); 47 } 48 }