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

I am completely stuck on assignment 1.1 and have tried many things, all of them involve using while loops and if statements. I was wondering if anyone could help me out here, and I will post code if needed

OpenStudy (anonymous):

Use pastebin.com to post your code and then put the link here.

OpenStudy (anonymous):

that's the thing, my code has already gone through several iterations, but I guess I'll show you guys the current one http://pastebin.com/4HiQFbAg

OpenStudy (anonymous):

although I was sorta grasping at straws with this one

OpenStudy (anonymous):

Well here are a few things I see right off the bat: "if x>(x/2)*2" What is (x/2)*2? It's x. Your code here is asking if x > x, which obviously doesn't make sense. "while (primecounter<1000):" Your using a while loop, which is fine. However, you never change primecounter, therefore the loop will be infinite. What you need to do is increment it (ie: primecounter += primecounter or primecounter = primecounter +1) Can you remind me what assignment 1.1 asks for? I'm having trouble figuring out what you're trying to do.

OpenStudy (anonymous):

Write a program that computes and prints the 1000th prime number. also that x>(x/2)*2 is to figure out if the number is odd, since even numbers will be equal while the odds will have a remainder. do you think that there is a way to do this problem with only while loops and for statements or should I learn some other things? I've already watched lectures 1 and 2, and this assignment is "due" the third lecture.

OpenStudy (anonymous):

Ah, that makes sense! I've been taking a couple math courses lately and forgot about integers vs. floats. Another method of finding out if it is odd is "if x%2>0:" Just noting. You can definitely figure this out just using while/for loops and if statements. However instead of trying to figure out how to make a loop for checking the 1000th prime number, let's just make a program that figures out if a number is prime. Then we can work from there. Write a program where you check to see if a number is prime. Check the numbers 2,3,9, and 11. Here is the pseudo code for let's say 11: A prime number is divisible only by itself and 1. Therefore we need to check 2-10 to see if any of those numbers divide into 11. Hints: A For loop would work well here. Bonus: 10 is actually too high of a number to check. What number can you actually stop at?

OpenStudy (anonymous):

If you get stumped on how to make a prime checker, you can look here. http://pastebin.com/aPbMvSQB I definitely suggest working it out on your own at first.

OpenStudy (anonymous):

I tried doing it on my own with no luck, then I checked your answer and got a bit confused. where did it get the value of x from in line 6?

OpenStudy (anonymous):

Ah, I just noticed a coding error (forgot the range), but let me explain for loops. Here is a classic for loop: for x in range(1,10): print x So what happens is that x is going through a list of numbers in the range 1-9. range(1,10) creates a list that looks like this [1,2,3,4,5,6,7,8,9]. Every time the loop goes through, it moves to the next position in the list/range. So first loop it will be 1, the second loop it will be 2, so on and son on. Take a look at this code here http://pastebin.com/kWUfzii8 Try it out and let me know if you have any trouble. If you're having trouble with for loops, be sure to watch the videos and review it.

OpenStudy (anonymous):

Have you been doing the reading because i got stuck on 1.1 as well but i didn't see the reading tab and doing the reading makes a big difference.

OpenStudy (anonymous):

ah yes I see what you mean, but I'm having a hard time figuring out how to make it so that I can find everything up to the thousandth prime, or any loops/if statements for that matter. Maybe I should think about it some more...also what exactly did you read in the reading tab?

OpenStudy (anonymous):

I'm talking about specifics, because lessons 1-3 are lumped together, but I'll just read em all I guess

OpenStudy (anonymous):

this is how i did it but i know some one that did python programming so i asked for his help and this is how he explained it to me http://pastebin.com/mi7CTnSQ

OpenStudy (anonymous):

ya that is what i did

OpenStudy (anonymous):

alright I'll read up a bit

OpenStudy (anonymous):

Hi guys. As a note, Garydos, your prime checker fails for n = 1. Fishbait, yours fails for n = 2. Let me know if you want some pointers.

OpenStudy (anonymous):

sometimes it helps to write down in words what you want to do: test to see if a number is prime if you find a number preserve that fact for use later keep checking numbers till you have found 1000 of them

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!