Skip to content

Instantly share code, notes, and snippets.

@pjc
Created July 3, 2012 00:04
Show Gist options
  • Save pjc/3036526 to your computer and use it in GitHub Desktop.
Save pjc/3036526 to your computer and use it in GitHub Desktop.
Euler Project in Ruby - Problem 10
x = 2_000_000
def prime? x
(2...x).each { |i| return false if x % i == 0 }
true
end
def primes upto
a = []
(2..upto).each { |i| a << i if prime? i ; puts i }
a
end
def array_sum a
sum = 0
a.each { |i| sum += i }
sum
end
puts "#{array_sum primes x}"
@blockloop
Copy link

array_sum can be written simply as

a.reduce(:+)

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