Basic Structure
The project has a very simple structure. There is a batch file in the \bin folder that allows you to run a simple console with all of the problems I have completed. Selecting a problem will give the result and how many seconds it took to generate that result. Every problem will have a corresponding specification, and possibly a number of utility classes (which will have their own specifications in turn).Problem #1
From the Project Euler website:If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.The trivial solution for this problem is actually quite easy. Simply loop through all of the number from 1 to 1000 (exclusive) and add all of the numbers that are divisible by 3 or 5. It might look something like this:
Find the sum of all the multiples of 3 or 5 below 1000.
def sum (3...1000).inject do |sum,value| if value % 3 == 0 or value % 5 == 0 sum + value else sum end end end
How do we iterate over only the multiples of 3 and 5 though? Well, the first step is to notice that the multiples are spaced in a cyclical pattern. Take a look at this:
| 3 | 5 | 6 | 9 | 10 | 12 | 15 | 18 | 
| 18 | 20 | 21 | 24 | 25 | 27 | 30 | 33 | 
| 33 | 35 | 36 | 39 | 40 | 42 | 45 | 48 | 
| +2 | +1 | +3 | +1 | +2 | +3 | +3 | 
class Problem001
 include Enumerable
 ADD_VALUES = [2,1,3,1,2,3,3]  
 def each    
  n = 3    
  i = 0    
  while (true) do      
   yield n      
   n += ADD_VALUES[i]      
   i = (i + 1) % ADD_VALUES.length    
  end  
 end
 def sum(maximum)
  inject { |sum,value| break if value > maximum; sum + value; }
 end
end
No comments:
Post a Comment