Skip to content

Instantly share code, notes, and snippets.

@pjc
Created July 1, 2012 22:26
Show Gist options
  • Save pjc/3029856 to your computer and use it in GitHub Desktop.
Save pjc/3029856 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 1
y = 0
(1..999).each { |x| y = y + x if (x % 3 == 0) || (x % 5 == 0) }
puts y
@jasonnoble
Copy link

(1..999).select{|x| (x % 3 == 0) || (x % 5 == 0) }.reduce(:+)

@dylanerichards
Copy link

def multiple_of_three?(n)
  n % 3 == 0
end

def multiple_of_five?(n)
  n % 5 == 0
end

def acceptable(n)
  multiple_of_three?(n) || multiple_of_five?(n)
end

p (0..999).select { |n| acceptable(n) }.reduce(:+)

@suyesh
Copy link

suyesh commented Aug 5, 2015

Here is my code sum = (1...1000).select {|x| x if x % 3 == 0 || x % 5 == 0}.reduce(:+)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment