A community for students.
Here's the question you clicked on:
 0 viewing
anonymous
 5 years ago
Can some one help me with pset 1 problem 1
anonymous
 5 years ago
Can some one help me with pset 1 problem 1

This Question is Closed

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0biggest problem I am having is how to keep using modulus (%) with prime numbers. So for example 21%2 != 0, 21%3 !=0 but 21%7 == 0...As the numbers get bigger I can't rely on doing that code over and over... I need a more effcient method

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0could we see your code?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0could we see your code?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0use dpaste.com  paste your code in the code window, choose Python for the syntax and click the paste it button  post the link to that here

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Hi guys, thanks for your response, here is the code http://dpaste.com/534549/ it isn't totally complete but you can see my thought process... x % 2... x % 3... soon x % 5 etc etc I know I need to do x % (each prime number i find) in order to do this correctly, but I am not sure how to create a while loop that will allow me to do that. All the examples I have seen so far are using for loops and ranges...which we haven't learnt yet, is that the only way?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0the while statement starts a loop. you correctly realized that you need another loop  one that is 'inside' of the while statement. A for 'loop' would work but you could probably do it with a while statement also. what do you think you need to do with the 'inner' loop?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i need the inner loop to increase what I am dividing by so x % z, I need Z to represent all of the prime numbers I am storing as I do the outer loop but I think it gets tricky because z should be < than x. So I need to think of a while condition that will allow me to do the inner loop, while ( z < x ) maybe?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yep  so in the outer loop you are starting at 3 and adding one on each 'loop', And the inner loop basically needs to do the same thing with the divisor.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0and then if at ANY point x % z == 0 the inner loop should break and go back to the outer loop

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0that would work but somehow you need to keep track of whether or not the 'candidate' ended up being prime. break is a keyword that will break out of a loop. why don't you revise your code with what you've got so far then look at it. by the way  what we are doing when we are writing down in english what we want the program to do is kinda like writing pseudocode which is always a good place to start tackling a problem

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Think about what you're actually trying to do. You want a counter to keep track of the primes you've found which goes to 1000. You are working with a number to be tested (which you've labeled x) and a divisor (which you've labeled y). A prime number can only be divided by 1 and itself, so if you get a remainder with your first divisor you can move on and try and divide your test number (x) by the next natural number which follows your first divisor. If you keep increasing your divisor by 1 and your test number is prime you will eventually get a remainder of 0. If this occurs when the divisor is equal to the number being tested, then you've found a prime number. Don't forget to increase your counter. If you get a remainder of 0 when the divisor is NOT equal to the test number, the number being tested was not prime. Either way, you need to test another number, so increase x by 1 and repeat the same process. Once you counter has reached 1000 you're done. Hope that helps. I find it helpful to try and explain what I want my code to do in English.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0haha its funny you both mentioned that, I will give it a shot thank you :)

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0have you revised your code and added the inner loop? if so paste and post it

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0yea, getting owned though... for some reason its not working http://dpaste.com/534556/

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0i added y = 2 under the 1st while loop, but its still not really looping

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0hmmm.. ok earlier i thought you said your inner loop would make divisors up to the number you are testing and then break out of the loop if you found it wasn't prime.  it doesn't look like you did that. you actually changed a lot  i thought you were going to do something like while count < 1001 while divisor < x test if x can be divided by divisor increment divisor

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0ya i started trying to do what matt was talking about, but even if i do a super basic nested while loop it doesnt work...so my knowledge of nested while loops is very wrong while count < 1001 : print "This is the first count" z = z  1 while z > 0 : "This is the 2nd loop" count += 1 doesn't even print 'This is the 2nd loop'

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0one way to help figure out what is really happening when your code runs is to put print statements in. I think you might have your tests wrong  if the number you are testing is actually prime then lines 79 will run forever  z will always be greater than 0 for every value of y

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Try and write the code based on the following: While the count is below 1000, if there is a remainder when x/y then increase y by 1. While the count is below 1000, if there is no remainder and x=y, increase your count and x by one, while making sure to reset y to its original value. While the count is below 1000, if there is no remainder and x != y, increase x by 1, reset the divisor to its original value and do NOT increase the count. Finally, print the value of x which raises the counter to 1000.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0add this after line 7 print "x:", x, "y:", "z:", z you can stop it from running by typing ctrlz  then you can scroll back and see what happened.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0whoops  ctrlc that is NOT ctrlz

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0a while statement will continue to loop as long as its condition is True

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0it keeps spamming that over and over lol

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0big lesson learned my friends when doing if statements if remainder == 0 & div = x is not the same as remainder == 0 and div = x :( & != and !!!!

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0if anyone runs into problems with this pset please!! watch video 3 lol, he goes over for loops, ranges, and collecting values to add to tuples
Ask your own question
Sign UpFind more explanations on OpenStudy
Your question is ready. Sign up for free to start getting answers.
spraguer
(Moderator)
5
→ View Detailed Profile
is replying to Can someone tell me what button the professor is hitting...
23
 Teamwork 19 Teammate
 Problem Solving 19 Hero
 Engagement 19 Mad Hatter
 You have blocked this person.
 ✔ You're a fan Checking fan status...
Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.