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

I have no prior knowledge of programming what so ever and today I have decided to give these lectures and problems a try... I am currently stuck on problem 1 of assignment 1. It is likely I am doing this completely wrong here is my code thus far... limit = 1000 start = 3 divisor = 2 for start in range (3,limit): if start%2 != 0: if start%3 != 0: if start%7 != 0: if start%5 != 0: print 'prime',start start= start + 2

OpenStudy (anonymous):

i seem to be getting all primes starting at 11

OpenStudy (anonymous):

what programming language are you doing?

OpenStudy (anonymous):

python

OpenStudy (anonymous):

oh...I am a new to python you can ask StackOverflow.They are really good there

OpenStudy (anonymous):

thanks I'm obviously writing a very inefficient code as I'm still on day one at the moment... seems others on that site are much more experianced

OpenStudy (anonymous):

why don't you start with object oriented programming?

OpenStudy (rsmith6559):

The first few primes are 2, 3, 5, 7 and 11. You're checking whether the primes can be divided by 2, 3, 7 and 5 without any remainder. What's the remainder of 5 / 5 and 7 / 7?

OpenStudy (anonymous):

yea i realize that 2 3 5 and 7 are all prime however those are the only numbers that are divisible is there a way to implement them into the list as a type of exception?

OpenStudy (anonymous):

spend some time with the Tutorial in the Python documentation. a number is NOT prime if it is evenly divisible (no remainder) by any number besides one and itself. To test a number you need to divide it by all numbers between one and itself and see if it is evenly divisible by any of them loops are used to repeatedly perform the same sequence of steps. sometimes you 'loop' til a certain condition is met. sometimes you 'loop' a fixed number of times. a loop would be ideal for this problem: you can repeatedly divide the candidate by a number (the divisor) and check to see if there is a remainder - the 'trick' is to change the divisor for each loop. for example if the candidate is seven the divisor could be two for the first loop, three for the second loop, ..., six for the fifth loop. start off writing a piece of code that will test any number (n) for primeness if you want to find say 100 primes then you have to run that test over and over till you find one-hundred of them - that sounds like another loop. use descriptive names for your variables. please use a code pasting site: - http://dpaste.com - http://pastebin.com - https://gist.github.com/ - http://pastie.org - http://codepad.org - http://ideone.com - http://www.repl.it/ paste your code there and post the link here. select Python syntax highlighting when u paste. - http://dpaste.com/1354734/

OpenStudy (e.mccormick):

@eLg You need to get to know what an object is before you jump into OOP, and when taking a Python based MOOC, OOP is generally not item #1. If it were Java based, OOP would be #2 or #3 when they explain how much of Java is OOP. Python uses everything as an object, but they hide this better, so less need to cram it down the student's throat at the start.

OpenStudy (e.mccormick):

@jkpalmer94 one big problem I see is that your implementation won't scale very well. For finding say the 3rd prime, it has no way to do so. I understand the idea of dividing by the early prime numbers. If you were looking for very large primes, this is one way to eliminate a ton of things. If you goal was speed, try only dividing by odd values up to half the size of the number. That is much faster than testing every number... though you won't see it for the first thousand primes. The machines are fast enough for even a basic method to work for 1000 primes. One nice way to debug is to start with say the first 10 primes. Get all of those good then it bump to 100. Those check against a primes list (just the last ones) go to 1000. If your end numbers there are fine, then change it so it only prints the last number.

OpenStudy (anonymous):

This guy started today on the MIT course, I.. started a week ago on Code Academy, and he is light years ahead of me already!

OpenStudy (e.mccormick):

Yah, the MIT course sort of lands you in the thick of it. Sink or swim!

OpenStudy (anonymous):

Thanks for all the comments guys I really appreciate the help and I look forward to continuing to work my way through this problem. I'm determined to solve this without copying someones code

OpenStudy (anonymous):

Im trying to use a code similar to the one i started with however I'd like to implement a tuple that holds all the primes as I narrow them down

OpenStudy (anonymous):

http://dpaste.com/1355071/ above is my code which tried to implement a tuple into however it is returning no numbers when I run it

OpenStudy (anonymous):

http://dpaste.com/1355099/ seems to be getting closer still not quite there yet any suggestions?

OpenStudy (anonymous):

is there a way to have one statement that would divide by all odd numbers and check to see if they have a remainder of 0 up to a given odd number without having to write them all out by hand in if statements like I am currently doing

OpenStudy (anonymous):

new approach attempting to divide a tuple of all odd integers by another tuple of all odd integers and find all the integers that do not have a remainder of 0 is that at all possible??!?!?!?

OpenStudy (anonymous):

http://dpaste.com/1355128/

OpenStudy (anonymous):

diving a tuple by a tuple approach turned out to be on the edge of retarded lol...so far my closest code was this one http://dpaste.com/1355099/

OpenStudy (anonymous):

you are going to end up with a loop within a loop - nested loops. you have to implement a loop that will check if a number is evenly divisible by Any of the numbers between 1 and itself you have to implement a loop that will run the previous loop till 1000 primes have been found ``` " is there a way to have one statement that would divide by all odd numbers .... ``` you could write a for loop that would see if a number is evenly divisible by any the numbers between it and one once you have that working you could nest it in a loop that runs till you have found what you want http://dpaste.com/1355152/

OpenStudy (anonymous):

how exactly do I write a loop that tests every number between it and one

OpenStudy (anonymous):

thats exactly what I am looking for

OpenStudy (anonymous):

if you can see that a problem will be solved in two or more separate parts, it helps to write discrete bits of code to solve each of those parts - then figure out how to glue them together. writing down in words the steps you think you need to take to solve the problem will help organize your thoughts and you can use that as a template for your code.

OpenStudy (anonymous):

nested loops are like the odometer on your car (if it has an odometer) - the inner loop is the rightmost wheel, the outer loop is the leftmost wheel.

OpenStudy (anonymous):

these statements might help http://docs.python.org/2.7/tutorial/controlflow.html#break-and-continue-statements-and-else-clauses-on-loops if you spend some time with the Python documentation you will have a better idea of the tools it offers to solve problems. The Tutorial in the documentation is a good place to start.

OpenStudy (anonymous):

okay so now I have a working prime number finder however do yo mind translating these lines of code into words.... for n in range (2,7919): for x in range (2,n): if n % x == 0:

OpenStudy (anonymous):

search all the numbers between 2 and 7919 to see if they are prime check if a number is if a number is prime by testing it with numbers between itself and one if you wanted to find 1000 things and you didn't know what the thousandth thing was you might want to while the number of things found is less than 1000 search for things and increment number things found each time you find one

OpenStudy (anonymous):

or even while the number of things found is less than 1000 search for things and keep track of them

OpenStudy (anonymous):

You sir are perhaps the best question answer I have ever had the pleasure to encounter. Thank you very much

OpenStudy (anonymous):

thnx

OpenStudy (rsmith6559):

All non-prime numbers factor into primes. Even numbers are always non-prime, so checking those is a waste of time to check. You want the whatever-th prime. So: While your collection of primes isn't long enough You need to check the next odd number only against your collection of primes If the prime in your list squared is bigger than your test number, your test number is prime Add it to your collection No need for more tests The "If" is actually redundant.

OpenStudy (anonymous):

@rsmith6559 what if you aren't saving all the primes you find?

OpenStudy (rsmith6559):

Then the program will run slow, like the code you recommended above.

OpenStudy (anonymous):

this one turns out to be pretty decent http://dpaste.com/1355432/

OpenStudy (rsmith6559):

Yeh, I know my code is slightly slower than that code, but IMO, mine is much more suitable for algorithms and coding skill of someone doing the first exercise of an Intro course.

OpenStudy (anonymous):

ahhhh .. that is the point isn't it - learning flow control and conditional statements not how fast can you find a prime

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!
Latest Questions
YoungBlood: STOP THE YELLOW NAME TAG SLANDER!
1 hour ago 11 Replies 2 Medals
Bubblezz: Art for @euphoriiic
4 hours ago 23 Replies 3 Medals
ilovemybf: i need more drawing ideas
5 hours ago 15 Replies 1 Medal
toga: what is a Mayuri
8 hours ago 3 Replies 1 Medal
Midnight97: Here is a beat I made let me know what y'all think
9 hours ago 24 Replies 2 Medals
toga: who thinks that there should be more titles
10 hours ago 5 Replies 0 Medals
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!