Overview

 

Question

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?

click for answer

232792560

Solutions

 

Java

 divisible.java https://raw.github.com/addamh/euler/master/005/divisible.java download


public class divisible {
   public static void main(String[] args) {
      final long MAX = 20;
      find: for (long i = MAX; ; i++) {
         for(long j = 2; j <= MAX; j++) if(i % j != 0) continue find;
         System.out.println(i);
         break;
      }
   }
}
$ time bash divisible.java
real	0m5.519s
user	0m6.041s
sys	0m0.102s

Ruby

 divisible.rb https://raw.github.com/addamh/euler/master/005/divisible.rb download
#!/usr/bin/env ruby

require "rational"

def smallest_divisor_for_range(start, stop)
  numbers = Range.new(start, stop).to_a
  numbers.inject(numbers.shift) { |lcm, n| lcm.lcm(n) }
end

puts smallest_divisor_for_range(1, 20)
$ time ruby divisible.rb
real	0m0.023s
user	0m0.019s
sys	0m0.003s