A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Can some one help me with pset 1 problem 1

  • This Question is Closed
  1. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    biggest 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

  2. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    could we see your code?

  3. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    could we see your code?

  4. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    use 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

  5. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Hi 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?

  6. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    the 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?

  7. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i 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?

  8. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yep - 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.

  9. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    and then if at ANY point x % z == 0 the inner loop should break and go back to the outer loop

  10. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    that 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 pseudo-code which is always a good place to start tackling a problem

  11. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Think 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.

  12. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    :)

  13. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    haha its funny you both mentioned that, I will give it a shot thank you :)

  14. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    have you revised your code and added the inner loop? if so paste and post it

  15. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    yea, getting owned though... for some reason its not working http://dpaste.com/534556/

  16. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    i added y = 2 under the 1st while loop, but its still not really looping

  17. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    hmmm.. 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

  18. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    ya 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'

  19. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    one 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 7-9 will run forever - z will always be greater than 0 for every value of y

  20. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    Try 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.

  21. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    add this after line 7 print "x:", x, "y:", "z:", z you can stop it from running by typing ctrl-z - then you can scroll back and see what happened.

  22. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    whoops - ctrl-c that is NOT ctrl-z

  23. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    a while statement will continue to loop as long as its condition is True

  24. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    x: 3 y: z: 1

  25. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    it keeps spamming that over and over lol

  26. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    big lesson learned my friends when doing if statements if remainder == 0 & div = x is not the same as remainder == 0 and div = x :( & != and !!!!

  27. anonymous
    • 5 years ago
    Best Response
    You've already chosen the best response.
    Medals 0

    if 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

  28. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Sign Up
Find more explanations on OpenStudy
Privacy Policy

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
  • 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.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.