SmallestMultiple.java 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. /* 2520 is the smallest number that can be divided by each of the numbers
  2. from 1 to 10 without any remainder.
  3. What is the smallest positive number that is evenly divisible by all of the
  4. numbers from 1 to 20?
  5. */
  6. import java.util.ArrayList;
  7. public class SmallestMultiple {
  8. // To see if a given number is evenly divisible by 1-20
  9. private static boolean checkIfNumIsSmallestMultiple(long num) {
  10. ArrayList<Boolean> divisibleBy1to20 = new ArrayList<Boolean>();
  11. for (long i = 1; i <= 20; i++) {
  12. if (num % i == 0)
  13. divisibleBy1to20.add(true);
  14. else
  15. divisibleBy1to20.add(false);
  16. }
  17. /* If there is even one "false" in the ArrayList, then the number is not
  18. divisible by 1-20, and the method should return false */
  19. if ( divisibleBy1to20.contains(false) )
  20. return false;
  21. else
  22. return true;
  23. }
  24. public static long getSmallestMultiple() {
  25. long num = 1;
  26. while (true) {
  27. if ( checkIfNumIsSmallestMultiple(num) )
  28. return num;
  29. else
  30. num++;
  31. }
  32. }
  33. }