This is topic Ruby on Rails! in forum Books, Films, Food and Culture at Hatrack River Forum.


To visit this topic, use this URL:
http://www.hatrack.com/ubb/main/ultimatebb.php?ubb=get_topic;f=2;t=048994

Posted by Phanto (Member # 5897) on :
 
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



[ June 28, 2007, 02:43 PM: Message edited by: Phanto ]
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by Bokonon (Member # 480) on :
 
I'd also "cheat" and immediately eliminate any even number above 2. Something like:

code:
shoe = shoe.reject{|number| if number != 2 and number%2 == 0}
#Alternatively...
shoe = shoe.select{|number| if number == 2 or number%2 != 0}

I think that's right.

That is a simple O(n) algorithm that will assure that your main loop algorithm is always working on m items, where m = n/2 + 1.

-Bok
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by Bokonon (Member # 480) on :
 
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.

-Bok
 
Posted by Phanto (Member # 5897) on :
 
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.
 
Posted by fugu13 (Member # 2859) on :
 
First, feel free to just keep reposting questions in this thread [Smile] .

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 [Wink] . 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.
 
Posted by Phanto (Member # 5897) on :
 
Thanks [Smile] .

Well, the current problem is in setting up the Rails framework.

http://www.rubyonrails.org/down

Tells me all the steps, and I've got all the way up to
quote:

Make your application

Create your application skeleton and start the server:

but I don't understand the next step:

quote:
rails path/to/your/new/application
cd path/to/your/new/application
ruby script/server

What's the ruby script/server line mean?

Thanks ^_6.
 
Posted by fugu13 (Member # 2859) on :
 
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.
 
Posted by Bokonon (Member # 480) on :
 
Specifically it will start the built-in WebBrick Ruby on Rails Development web server. It runs on port 3000 by default.

-Bok
 
Posted by Phanto (Member # 5897) on :
 
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 [Razz] .
 
Posted by Bokonon (Member # 480) on :
 
Use aptana as an IDE. It's built on eclipse and is a nice way to develop in ruby (and Ruby on Rails specifically).

-Bok
 
Posted by Icarus (Member # 3162) on :
 
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?
 


Copyright © 2008 Hatrack River Enterprises Inc. All rights reserved.
Reproduction in whole or in part without permission is prohibited.


Powered by Infopop Corporation
UBB.classic™ 6.7.2