A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

PS1, 1a: Think I'm close but stuck and need some help! It looks as if my program is spitting out some non-prime numbers as prime. The first instance I find is when my program suggests that the 61st prime number is 289 (it's not prime). Where am I going wrong? http://dpaste.com/524739/

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

    I would suggest decomposing the task into two separate parts. First, write some code that, given some static assignment of candidate = 114321 or some other number, simply tests to see whether or not this candidate variable is a prime number. Then, once that's completed, take that code, and place it in the body of a while loop that increments candidate until plimit primes are found.

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

    Sounds good. I'll give that a shot now. Thanks!

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

    Ok, so I think I was able to get a test for primeness. Just don't think it is the most efficient way (given the tools given to date). Any advice much appreciated! Will now work on integrating the while loop that increments candidate until plimit primes are founds. http://dpaste.com/524753/

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

    Man, I just spent nearly an hour trying to figure out what wasn't working in my program and it was just a missing indentation! Seem to have it working. polpok (or anyone else), does this look right to you? Something tells me it may be a bit clunky. http://dpaste.com/524758/

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

    Oh, and polpok, thanks again for your help. I really appreciate it!

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

    Sorry bout that. I was off working on something else. I'm going to forgo analysis of your final code for a moment and look at what you wrote for the simple test of primeness. I've made a few minor edits to clean things up and make it a bit easier to read, but the logic is still the same. http://dpaste.com/524760/ Now, if we take your code for looping until we find plimit primes and change the prompt for candidate into a manual assignment we have http://dpaste.com/524764/ Which seems like a perfectly elegant solution.

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

    The key here is to envision what is to be done for a single number, then utilize that process over a sequence of many numbers. Once you have formalized the process for performing the task once, you shouldn't need to change anything about it to perform it many times (in a loop) other than changing how your input variables are initialized.

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

    That makes sense. And I imagine breaking it down before building it up helps avoid a lot of potential errors. Looking over your clean-up of my code was helpful. Particularly converting the input into an integer so I don't have to do it within the loop. Great tip. One question on loops. I see you took candidate=candidate+1 and put it outside of the innermost while loop. For my solution, I incremented in two spots (see below). Is your way just saving a line of code or is it more important than that? I'm having trouble understanding why it increments without being explicitly told to at the end of both if statements. while (divisor<=(candidate/2)) and (not_prime==False): if candidate%divisor == 0: #if no remainder, not prime not_prime=True #not prime candidate = candidate + 1 #not prime, test next candidate divisor = divisor+1 #test next divisor if not_prime==False pcounter=pcounter+1 #add 1 to prime counter candidate = candidate + 1

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

    Remember that the responsibility for assigning candidates is not part of the process of testing for primeness. My code is organized the way it is to provide a clear distinction between the two tasks. The innermost task is simply to take the given candidate and see if it is prime. The outer loop is responsible for iterating through all the possible candidates and assigning each one so that we can test them. This clear distinction makes it much easier to debug in that I can ensure that the prime test code is working correctly before I add the outer loop. But that debugging is only useful if I do not change the prime test process when I add it to my loop for candidates. Otherwise I have to go back and re-debug/test everything. The process of your algorithm is logically equivalent since the number was either prime or not. And in both those cases you are incrementing your candidate. The difference is one of expressing what you mean. For your version you're saying, "If it's prime, move on to the next candidate. However, if it's not prime move on to the next candidate". For my version, I'm saying "Always move on to the next candidate". Since my choice of candidates is not dependent on primeness, I can reuse my code easily to find all primes between any two integers where the distance between the primes should be greater than a third integer. And again, nothing about the code I used to test for primeness needs to change. To emphasize this, I've added comments to show where exactly this unchanging portion of my algorithm is. And you'll note that this code is identical to what I used in the previous 2 instances to test a single number, and to find the 1000th prime. http://dpaste.com/524771/

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

    have you gotten anywhere on Pset 1b? i'm stuck on adding them all together

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

    i actually haven't started 1b yet. just read through it. probably gonna get cranking on it this afternoon. i watched lecture 3 which provides a technique i think you can use but my guess is there's a way to do it without that which i am going to try to figure out if i can.

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

    yea i wasn't trying to jump ahead just yet. I wanted to do the project with what i've been taught so far. I've got the understanding for it just can't seem to put it together just yet.

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

    my plan is to tackle the problem the way polpok suggested i work through 1a. mainly chopping it up into discrete steps. i already have code that determines primeness. and i already have a way to limit the number of primes i want to work with. so i need to create code that will take the log of any number x which shouldn't be too difficult. finally, i need to figure out a way to sum the resulting logs together (sounds like this is where you ran into trouble and i imagine i will too). gonna start in about an hour. wish me luck!

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

    haha good luck mate. don't forget to put from math import* at the top of your code otherwise it won't recognize log.

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

    so what i got it to do was take the ln of each prime number and add them together. but i didn't type ln in the code i wrote log(number).. is that what they wanted?

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

    By default the log function uses the base e. So your code is correct. As an aside, please (always) avoid using from module import * Instead import what you want explicitly, or access the module's namespace indirectly. from math import log,e #explicitly import log print log(e) import math #import the module only print math.log(math.e) #access the attributes of the module indirectly

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

    thanks for the clarification. pretty excited to have figured something out on my own

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

    link to my solution to 1b is below. polpak, is there a reason you print log(e) and math.log(math.e)? i also used "from math import *", probably because that's what is listed in the problem set. what is the reason for the specificity? to avoid importing a ton of stuff unnecessarily? i will go back and make the edit in my program. thanks polpak! http://dpaste.com/524997/

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

    when you do it like that does it include log(2) in with the answer? I had to manually do that.

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

    it does. polpak tweaked my original attempt at 1a so that it counts 2 as prime. i believe it is because i initiate my variable not_prime as False and my divisor variable as 2. my second while loop reads, "divisor <= candidate/2" so in the case of candidate=2 and divisor=2, that doesn't hold. so it jumps to the next if statement and counts it as prime. is that right polpak?

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

    Hey guys, I'm doing the same problem and wanted to remark that--unless I'm mistaken--you don't actually need to check divisors up to candidate/2. Instead, it should suffice to check up to Sqrt(candidate). This would probably save a good bit of time in case you're checking very large numbers, right? Thanks a lot for sharing your ideas! :)

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

    i guess that's right! would certainly save computation time. thanks for pointing out keinerniemand! i'm going to update my script.

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