Created
December 20, 2013 17:35
-
-
Save lgrains/8058427 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Euler | |
class Prob1 | |
def self.sum(upper_limit, num1, num2) | |
::Prob1.new(upper_limit, num1, num2).sum | |
end | |
def initialize(upper_limit, num1, num2) | |
puts "#{upper_limit} | #{num1} #{num2}" | |
@upper_limit = upper_limit | |
@num1 = num1 | |
@num2 = num2 | |
end | |
def sum | |
#find the sum of all natural numbers below upper_limit that are multiples of 3 or 5 | |
puts "in sum @num1 is #{@num1} and @num2 is #{@num2}" | |
set1 = get_multiples(@num1) | |
puts "set1 from get_multiples is #{set1}" | |
set1a = (@num1...@upper_limit).inject([]){|arr, n| arr << n if (n % @num1 == 0); arr} | |
puts "set1a is #{set1a}" | |
set2 = get_multiples(@num2) | |
puts "set2 is #{set2}" | |
merged_set = set1.to_set + set2.to_set | |
puts "merged_set is #{merged_set}" | |
merged_set.inject(0){ |sum, num| sum += num; sum } | |
end | |
# def get_multiples(base_num) | |
# ret_value = (base_num...@upper_limit).inject([]){|arr, n| arr << n if (n % base_num == 0); arr} | |
# puts "in get_multiples where ret_value is #{ret_value}" | |
# ret_value | |
# end | |
def get_multiples(base_num) | |
ret_arr = [] | |
(base_num...@upper_limit).each do |n| | |
ret_arr << n if n%base_num == 0 | |
end | |
puts "ret_arr in get_multiples is #{ret_arr}" | |
ret_arr | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment