A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

Don't know what I am missing with problem set 1 b

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

    here's what i have: #!/usr/bin/env python # print out prime numbers up to 1000 from math import * max_prime = 1000 # number of prime numbers to output isPrime = 1 # boolean testing for prime numbers prime_candidates = [] prime_numbers = [2] x = 2 while x < max_prime: if (x/2)* 2 == x: x += 1 else: prime_candidates.append(x) x += 1 index = 0 # index of prime_candidates list while index < len(prime_candidates) -1: isPrime = 1 for i in range (2, prime_candidates[index]): if prime_candidates[index] % i ==0: isPrime == 0 index += 1 if isPrime == 1: prime_numbers.append(prime_candidates[index]) index += 1 sum = 0 n = 80 for i in range (0, n): sum = sum + log(prime_numbers[i]) print "sum is" , sum , " n is ", n , "ratio is" , sum/n according to the problem the sum of the logs should be less than n, and as n increases the ratio should be close to 1, which are not the case.

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

    for one thing, isPrime == 0 is a comparison, not an assignment.

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

    Also, n should probably equal max_prime, based on the problem statement: In this case, the conjecture above reduces to the claim that the sum of the logarithms of all the primes less than n is less than n, and that as n grows, the ratio of this sum to n gets close to 1.

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

    Hi Somnamniac, thanks for the reply! Shouldn't n be less than or equal to len(prime_numbers), as this is the actual number of primes up until max_prime? I changed my code in part b to: n = 20 for i in range (0,n): if n > prime_numbers[i]: sum = sum + log(prime_numbers[i]) print "sum is" , sum , " n is ", n , "ratio is" , sum/n When I calculate by hand for n = 20 ( log(2) + log(3) + log(5) + log (7) + log (11) + log (13) + log (17) + log(19) ) I believe I get the correct answer. However when I plugin large values for n, the ratio exceeds 1.0.

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

    I think we have different interpretations of the problem statement. I'll explain mine (using 1000 as n), and you can see if it makes sense: the sum of the logarithms of all the primes less than n translation: "the sum of the logarithms of all the primes less than 1000" is less than n "is less than 1000"

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

    I generally copy everything before I post, in case it gets stuck

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

    This is my interpretation: "the sum of the logarithms of all the primes less than n" (with n =1000) This would be a total of 191 numbers. [2,3,5,7,11,13,..........967,971,977,983,991,997] so then the sum would be the log of each of these individual numbers added together, right?

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

    right. and what is that less than?

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

    It's less than n, 1000. I assume then my is error that I am using the number of primes (191) to calculate the ratio? If I use n = 1000 it looks good. However if I test higher values this way I see the ratio is around 0.176 - 0.18. Like the problem states it does not increase monotonically, but it doesn't seem to get close to 1.

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

    Can you paste your current version?

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

    I found a flaw in my code when I calculated my last post. I attempted to fix it, but now the sum is greater than n and the ratio is greater than 1, when n =1000 :( #!/usr/bin/env python # print out prime numbers up to 1000 from math import * n = 1000 # number until to calculate prime numbers isPrime = 1 # boolean testing for prime number prime_candidates = [] prime_numbers = [2] x = 2 while x < n: if (x/2)* 2 == x: x += 1 else: prime_candidates.append(x) x += 1 index = 0 # index of prime_candidates list while index < len(prime_candidates) -1: isPrime = 1 for i in range (2, prime_candidates[index]): if prime_candidates[index] % i ==0: isPrime == 0 index += 1 if isPrime == 1: prime_numbers.append(prime_candidates[index]) index += 1 sum = 0 #print prime_numbers num_of_primes = len(prime_numbers) #print num_of_primes for i in range (0,num_of_primes): if n > prime_numbers[i]: sum = sum + log(prime_numbers[i]) print "sum is" , sum , " n is ",n , "ratio is" , sum/n

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

    You've still got the isPrime == 0 problem. That should be an assignment, not a comparison

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

    D'oh! Changing that fixes it! Thank you so much for your patience, I really appreciate it :)

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

    No problem. I'm glad you got it!

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