/ Published in: Ruby
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
class Euler1 # =================================================================================== # Private Members Private Members # =================================================================================== private def initialize ceiling, naturals @ceiling_number = ceiling @natural_numbers_array = naturals @current_number = 0 @sum = 0 determine_sum_of_naturals end def determine_sum_of_naturals while @current_number < @ceiling_number if divisible_by_natural_numbers? @current_number @sum += @current_number end @current_number += 1 end end def divisible_by_natural_numbers? number_in_question @natural_numbers_array.each do |natural| if number_in_question/natural == (number_in_question/natural).floor return true end end end # =================================================================================== # Public Members Public Members # =================================================================================== public def get_sum @sum end end xxx = Euler1.new 1000, [3,5] puts xxx.get_sum