Problem 1 from Project Euler . net (“set”-based solution)


This time, I fill a hash map for step values of 3 and of 5.  No noticeable difference from my first try, until you start trying to find results for much larger than stopping at 999, at which point amount of memory consumed/allocated makes this method way more inefficient.

x=0

h = {}
3.step(999, 3) {
 |i|
 h[i] = 1
}
5.step(999, 5) {
 |i|
 h[i] = 1
}

h.each { |k,v|
 x=x+k
}
print x,"n"


Leave a Reply

%d bloggers like this: