A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Having difficulty with PSet1 Prob2. Here is what I have thus far... from math import * prime_poss = 2 sum = 0 n = int(raw_input("Enter a number: ")) while prime_poss < n: if prime_poss%2 != 0: for divisor in range (2, (prime_poss/2)): if prime_poss%divisor != 0: logprime = log(prime_poss) print logprime sum = logprime + sum prime_poss += 1 else: prime_poss += 1 print sum Ideas?

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

    made numerous edits. my question is this ... when setting the range for the divisor to check a primes, what is the end limit... 'n'? n/2? our candidate prime?

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

    Technically, the square root of the number

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

    # is there a functional way to do this? in part 1a, where i wrote the 1000th prime finder, i used a FOR loop.. for divisor in range (2, (candidate/2)): # and this was how i was able to test a range of divisors to identify a prime is it the same concept (given that i am not using a square-root function?

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

    I think you are trying too hard, let me help you understand. You have done problem 1, correct? Please show you your code for number 1 and, for the sake of simplicity and efficiency, replace your "n/2" by "math.sqrt(n)+1" Then I'll build from that code to get the next problem done =D

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

    Basically, you need a MINOR modification from code 1 to get code 2 working

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

    ## here is my code for part 1a: prime_count = 1 candidate = 2 not_one_if_true = 0 while prime_count < 1000: if candidate%2 == 0: # to eliminate even nnumbers candidate += 1 else: for divisor in range(2,(candidate/2)): # find divisor range if candidate%divisor == 0: # find primes not_one_if_true = 1 # if not prime if not_one_if_true == 0: if prime, increment prime_count += 1 candidate +=1 else: # if not a prime, increment, reset Boolean truth candidate += 1 not_one_if_true = 0 print candidate - 1

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

    That code seems to have a problem. Maybe use pastebin, because there seem to be problems.

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

    # i forgot to comment out a note... i just ran this newly pasted code and got 7919 # don't know how to use pastebin... prime_count = 1 candidate = 2 not_one_if_true = 0 while prime_count < 1000: if candidate%2 == 0: # to eliminate even nnumbers candidate += 1 else: for divisor in range(2,(candidate/2)): # find divisor range if candidate%divisor == 0: # find primes not_one_if_true = 1 # if not prime if not_one_if_true == 0: #if prime, increment prime_count += 1 candidate +=1 else: # if not a prime, increment, reset Boolean truth candidate += 1 not_one_if_true = 0 print candidate - 1

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

    Okay, here is the easy part. I made your code work for problem 2, but I will not show it. Create a variable named Sum. Every time you find a prime number, add the math.log of that number to Sum. Answer is 7803

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

    implementing ....

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

    If you come close to 7803, tell me. I will tell you why you may be a "little" off

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

    7811.333

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

    7811.59

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

    Okay. Remember this. 1) You must NOT count the math.log of the last number ( 7919 ). This is said in the assignment. It has to be all numbers BELOW the nth prime. Is your answer now exact?

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

    7802.6

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

    so i set my limit too high (1000), instead of 999... thanks for the help!

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

    Your answer is wrong. Do you count the math.log(2) somewhere in your code?

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

    no, i couldn't figure that part out... my code can be broken down into 1. determine if odd; if even, increment by one 2. if odd, determine if there are factors within range (2 to n/2) [is it prime] 3. if prime, increment by one, and add for part 1b, also add sum = math.log(candidate) + sum but since i am eliminating the even in my initial code, i don't have the math.log(2) incorporated

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

    Then add math.log(2) somewhere in the code. The exact answer is near 7803.3

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

    will do, thanks!

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