Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 20 Online
OpenStudy (anonymous):

I've just started following this class and am new to programming. I've been struggling through exercise ps1 and found a solution that seems to work, but I'm not really sure for part b. Could anyone give me some feedback on ps1a: http://codepad.org/q17hpI7H and ps1b: http://codepad.org/v4LOHeyP Thanks.

OpenStudy (maitre_kaio):

Your code seems fine to me. Gives the good results and the logic is good. Quite clear too. Of course, you could improve little things from time to time. But the main things are here.

OpenStudy (anonymous):

mioto, can you please explain /in details if possible/ what do you do in line 2 / primes=[2]/ in line 14 /primes = primes + [num]/ and in line 16 /print 'The 1000th prime is: ' , primes[-1] /?

OpenStudy (anonymous):

mioto, it gets easier, dont worry. Im working on the same problem set, and it takes a few days to get used to thinking in the computers language to address what you're trying to do. What helped me alot was writing out a list of sub-problems that I needed to solve, and writing short bits of code for each one. Then I put them all together, and it's pretty close to working by now. I think I found about 8 of them for ps1a, they paper i wrote them on is in my locker at school right now but as i recall some of them were: 1) need to build a test that determines whether the number is prime or not; int division? 2) the divisor used in the test has to increase by one and then repeat 3)the test needs to stop if the divisor passes the square root of the number without and success. 4)when this happens, I need to record a sucessful result 5) Again, when this happens I need to increase the number by one, 6)and resume testing 7) when I have 1000 results, I need to stop all aforementioned testing 8) when I have 1000 results, I need to print the latest number that was tested to be prime Like I said, after a few days you'll start to get into the hang of it! As you saw, those "problems"are really basic, but those are essentially the questions to ask yourself as you build your code. I'd advise looking at it for an hour a day each day, continue to approach it from new angles like this one, and dont ever think that you wasted the hour cause you didnt have a "breakthrough" that day. This approach helped me for snowboarding, and also for playing ukulele C: good luck mioto!!

OpenStudy (anonymous):

Thanks for the feedback. I'll keep working on this course and hopefully things will get easier. Programming is a whole new way of thinking and I'm definately not used to it. It is a good idea to try and break things into smaller parts, but sometimes even the smaller parts seem rather big. I'm starting to look at the next assignment and I'll definately have to break it all down to get my head around it. Wanderboy: In line 2, i set up a basic list with the first primes (the number 2 because it won't come back as a prime using my program) and then in line 14 if a prime number is found, it is added to the list. Line 16 just prints out the last number in this list when the count = 1000. If you have any suggestions, I'd love to hear. Thanks to everyone for your help.

OpenStudy (anonymous):

Mioto, How did you come up with 'if divisor == int((num**0.5)+1): #when the divisor equals the sqrt of the number, stop' I mean why would you want to stop at that and not at any other condition? I am really impressed since it didn't strike me that I can stop at a condition like that. I found your code very helpful. Since I had been working with a for loop to solve it, I was not getting the right answer. I am working on part b now. Thanks again!

OpenStudy (anonymous):

I don't think you need that list for this problem set. Here is how I would do using a count function: http://codepad.org/RhiGtGcn

OpenStudy (anonymous):

wanderboy, I understand your code here: http://codepad.org/RhiGtGcn very well but, Could you please explain this condition: if divisor == num: in line 11

OpenStudy (anonymous):

Ahmed.Salah, as I understand it if the condition "if num % divisor != 0:" is fulfilled you then increase divisor with 1. Therefore at some point after you increase the divisor /which before the increase was smaller than num/ will become equal to num and therefore you don't need to run the check any more.

OpenStudy (anonymous):

wanderboy, I imagine that you don't need a list, but it helped me figure out what was happening when I was testing my program. With the list I could print out the results of a small number of examples to see if I was actually finding prime numbers or not. I'm sure as I get more experience I'll look back on this code and have a good laugh. I also know that this isn't the most efficient way of solving the problem, but I was happy to have solved it at all.

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!