Last active
May 30, 2018 01:47
-
-
Save zben/4c821d8a7ef3c37aa253d44d6d94bfe6 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
require 'test/unit' | |
extend Test::Unit::Assertions | |
class FibonacciAggregator | |
class << self | |
def sum_of_even_numbers(limit: 4_000_000, debug_mode: false) | |
sum_of_numbers(limit: limit, debug_mode: debug_mode) do |number| | |
number % 2 == 0 | |
end | |
end | |
private | |
#For extensibility with other conditions for summing | |
def sum_of_numbers(limit:, debug_mode:, &block) | |
sum, lower, higher = 0, 1, 2 | |
while lower <= limit | |
sum += lower if block.call(lower) | |
puts ["Fibonacci number:", lower, "Sum of even numbers:", sum].join("\t") if debug_mode | |
lower, higher = higher, lower + higher | |
end | |
sum | |
end | |
end | |
end | |
#tests | |
assert_equal(FibonacciAggregator.sum_of_even_numbers(limit: 5), 2) | |
assert_equal(FibonacciAggregator.sum_of_even_numbers(limit: 10), 2 + 8) | |
assert_equal(FibonacciAggregator.sum_of_even_numbers(limit: 100), 2 + 8 + 34) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment