Project Euler Problem 1


/ Published in: Ruby
Save to your folder(s)



Copy this code and paste it in your HTML
  1. class Euler1
  2.  
  3. # ===================================================================================
  4. # Private Members Private Members
  5. # ===================================================================================
  6.  
  7. private
  8.  
  9. def initialize ceiling, naturals
  10. @ceiling_number = ceiling
  11. @natural_numbers_array = naturals
  12. @current_number = 0
  13. @sum = 0
  14.  
  15. determine_sum_of_naturals
  16. end
  17.  
  18.  
  19. def determine_sum_of_naturals
  20. while @current_number < @ceiling_number
  21. if divisible_by_natural_numbers? @current_number
  22. @sum += @current_number
  23. end
  24.  
  25. @current_number += 1
  26. end
  27. end
  28.  
  29.  
  30. def divisible_by_natural_numbers? number_in_question
  31. @natural_numbers_array.each do |natural|
  32. if number_in_question/natural == (number_in_question/natural).floor
  33. return true
  34. end
  35. end
  36. end
  37.  
  38. # ===================================================================================
  39. # Public Members Public Members
  40. # ===================================================================================
  41.  
  42. public
  43.  
  44. def get_sum
  45. @sum
  46. end
  47.  
  48. end
  49.  
  50. xxx = Euler1.new 1000, [3,5]
  51. puts xxx.get_sum

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.