Permalink
Browse files

Solved 5

  • Loading branch information...
1 parent f6c83c8 commit da8eb043e194f4548b2862db37c0a5d2489aa741 @ndbroadbent committed Oct 13, 2010
Showing with 35 additions and 0 deletions.
  1. +35 −0 5/solution.rb
View
@@ -0,0 +1,35 @@
+#!/usr/bin/env ruby
+# -----------------------------------------
+# | A solution to a Project Euler problem |
+# -----------------------------------------
+require 'rubygems'
+require 'pp'
+problem_no = File.dirname(File.expand_path(__FILE__)).split('/').last
+puts "=== Solving Project Euler problem ##{problem_no}\n\n"
+question = %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?
+
+}
+puts "? #{question.strip}\n\n"
+# --------------------------------------------------------------------
+
+# Process the divisible range in reverse, this will speed up the calculation.
+@range = (1..20).to_a.reverse
+
+def divisible_for_all?(x)
+ # Return false if there are any values for which a division leaves a remainder.
+ # True if none are detected.
+ !@range.detect{|i| x % i > 0 }
+end
+
+# Must be at least a multiple of 20.
+top = @range.first
+i = top
+while not divisible_for_all?(i)
+ i += top
+end
+
+puts i

0 comments on commit da8eb04

Please sign in to comment.