For problem set 1, question 1, a friend of mine sent me this: n = int(raw_input("Which Prime do you want? ")) counter = 1 prime = 3 total = n while (counter < total): divisor = prime/2 while (divisor > 1): if prime % divisor == 0: prime = prime + 2 divisor = prime / 2 else: divisor = divisor - 1 How does this actually work, I don't understand what is actually happening.
Which part don't you understand. If you have Python installed on your computer then there is a help file which is really good. Start with the Tutorial that is in the help file - I have read parts of it many times. There is also an index tab (windows version anyway) that works great, you can use it to find info on specific statements, keywords and operators, etc.... the online docs are here: http://python.org/doc/ Have you watched the lectures?
i have a doubt.... at the start of loop prime is 3, divisor =1 so it wont enter the next loop, further even if the counter increments its chking only for the prime number 3...
Woops whole program isn't included.... here it is: n = int(raw_input("Which Prime do you want? ")) counter = 1 prime = 3 total = n while (counter < total): divisor = prime/2 while (divisor > 1): if prime % divisor == 0: prime = prime + 2 divisor = prime / 2 else: divisor = divisor - 1 counter = counter + 1 prime = prime + 2 print prime - 2
5 have trouble wrapping my head around from "while (divisor > 1) ---> the end. (Assuming the total is 1000) 1. 3%1 == 0. 2. So prime is now 3+2=5. 3. The divisor is now 5/2=2. (integer division) 4. The count becomes 2 5. Then the prime becomes 7. 6. Then 7 is tested. 7. 7%2 != 0 8. So divisor -1 to equal 1 9. But does the count still go to 3, or does 7 get retested first?? and am I thinking this through correctly?
Thanks for all the help!
Join our real-time social learning platform and learn together with your friends!