posted
a) as much as I hate to do this, anyone know of any good Ruby forums? I know I can get good help here, but this is a general subject forum.
b) I'm trying to make a program to find all the prime numbers to 1000. I decided to start by eliminating all non-prime numbers by using a sieve. It ain't working. There's a lot of stuff going on around me so I can't focus properly to debug it. Any thoughts?
Thanks! ^_^
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
posted
Use code instead of quote, and you'll get indentation, I believe.
Your problem in this case arises because you are confusing values in an array with their indices. Your array goes from 1 to 1000, but after you see if 1 is divisible by 2 (which it is not), you set shoe[1] to be 0 . . . but 1 is in shoe[0]. The correct behavior can be obtained by using each_index; I have made the code a little more ruby-like as well (though I rarely code ruby, so it might be possible to make it much more ruby-like).
Actually, it might also have been because you were using = instead of == in the if statement. I'm not sure how ruby would treat that.
Your code also won't quite work because everything is divisible by itself. See the additional clause in the if statement below.
A good way to debug this script would be to start with a smaller array and add statements printing the entire array periodically throughout.
Good variable names are your friend, btw.
code:
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
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).
code:
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 } }
General tips for debugging short scripts like this (without using a 'proper' debugger): lots and lots of statements printing variable values.
Also, don't write so much. Write the smallest part and verify that is working. You wrote too much before you looked for this problem, making it much harder to find.
Rarely does one find a bug just by looking, unless it is obviously the result of some behavior in the code. Bugs are found by testing things, either with test cases or by running over and over again with slight code modifications.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Yeah. Heh, its funny that both of us decide to contribute little optimizations, when if you want something optimal you just shouldn't be searching for prime numbers using this method.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Well, your optimization was much more impactful, since it hits the "long pole", That algorithm is O(n^2), right? You didn't (theoretically) improve that, but in the average case, you've improved it by a fair bit, since most numbers aren't prime, and you stop working on a candidate much earlier.
posted
Thanks for the kind contributions, everyone, and thanks particularly to fugu13 for the good programing advice. The each_index method looks to be useful.
Again, any ideas for a good Ruby forum? While I appreciate the advice you guys have given me, I don't want to spam this forum with programing threads.
Posts: 3060 | Registered: Nov 2003
| IP: Logged |
posted
First, feel free to just keep reposting questions in this thread .
Second, I have no idea on Ruby forums. I'm not much of a Ruby programmer, and have never really gone in for programming forums of any kind. In fact, I'm very suspicious of Ruby . I like a lot of aspects about it, I just like other languages better. But there's certainly a lot of fervor surrounding it, and a lot of people are learning programming because of it, both of which make me glad.
Just don't let yourself just learn ruby, you'll be a much stronger programmer if you pick up one or two other languages (at least), preferably crossing at least one other major language category.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
That's the command to run. When you're in the directory they've directed you to cd to, running that command will start the server.
Posts: 15770 | Registered: Dec 2001
| IP: Logged |
posted
Ah, ok. Got it to work -- didn't realize that you had to be inside the DOS command prompt and was confused by the fact that before it wanted me to specify but there was generic.
Great!
Now onto figuring out what to do next .
Posts: 3060 | Registered: Nov 2003
| IP: Logged |
posted
I dunno. It wouldn't surprise me if there were some conspiracy. Jack Ruby certainly knew enough people in organized crime, and he made statements before his death hinting at a larger conspiracy. I guess we'll never know, huh?
Posts: 13680 | Registered: Mar 2002
| IP: Logged |