quote:
shoe = (1..1000).to_a
i = 2
max = 34
while i <= max
shoe.each {|variable|
if variable%i = 0
shoe[variable] = 0
end}
i = i + 1
end
lover = gets
code:Ruminating some, while a sieve will never be efficient, you could make it much more efficient by switching the loops. That way you would only check each member of the 'shoe' for being divisible until you found something that divided it, at which point you would set it to zero and exit the inner loop. With the current implementation, everything in the shoe is looped over for every divisor, even when it has already been eliminated (you could add yet another check, but its still inefficient).shoe = (1..1000).to_a
min = 2
max = 34
(min..max).each { |divisor|
shoe.each_index { |index|
current = shoe[index]
if current != divisor and current % divisor == 0
shoe[index] = 0
end
}
}
puts shoe
code:General tips for debugging short scripts like this (without using a 'proper' debugger): lots and lots of statements printing variable values.shoe = (1..1000).to_a
min = 2
max = 34
shoe.each_index{ |index|
current = shoe[index]
(min..max).each{ |divisor|
if current != divisor and current % divisor == 0
shoe[index] = 0
break
end
}
}
code:I think that's right.shoe = shoe.reject{|number| if number != 2 and number%2 == 0}
#Alternatively...
shoe = shoe.select{|number| if number == 2 or number%2 != 0}
quote:but I don't understand the next step:
Make your application
Create your application skeleton and start the server:
quote:What's the ruby script/server line mean?
rails path/to/your/new/application
cd path/to/your/new/application
ruby script/server