A community for students.

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

anonymous

  • 5 years ago

ps1.2 - logarithm of prime numbers - where i am stuck. plz help...

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

    from math import * n = 2 Goal = int(raw_input('Please input a prime number to count ')) TotLn = 0 ##sum of log(prime)s so far def isprime(n): ## defines a prime number if n == 2: return 1 ## 2 is prime if n % 2 == 0: return 0 ## all other evens are not prime max = n**0.5+1 i = 3 while i <= max: ## determines if remaining odd numbers are if n % i == 0: ## composite return 0 i += 2 return 1 while n < Goal: ## produces a list of prime numbers n, Goal if isprime(n) == 1: print n NewLn = log(n) ##defines NewLn as log of current n print NewLn elif isprime(n) == 0: pass n += 1 TotLn += NewLn ##adds NewLn to the sum of previous logs print TotLn if n == Goal: NewLn = log(n) TotLn += NewLn print 'n = ', n print 'log(n) = ', log(n) print 'sum of logs of primes up to n =', TotLn print 'the ratio between the two =', n/TotLn

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

    the problem i am having is that it adds too many times to TotLn Please input a prime number to count 5 2 0.69314718056 0.69314718056 3 1.09861228867 1.79175946923 2.8903717579 n = 5 log(n) = 1.60943791243 sum of logs of primes up to n = 4.49980967033 the ratio between the two = 1.11115810808 >>> log(5) 1.6094379124341003

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

    so you see there, it prints the prime number, then it's log, then TotLn, which works for 2, but when it gets to three it seems as if it's adding twice.

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

    here is an update with some context in the prints Please input a prime number to count 7 this is the current iteration of n, 2 this is the log of the current iteration of n, 0.69314718056 this is the total of all logs so far, 0.69314718056 this is the current iteration of n, 3 this is the log of the current iteration of n, 1.09861228867 this is the total of all logs so far, 1.79175946923 this is the total of all logs so far, 2.8903717579 this is the current iteration of n, 5 this is the log of the current iteration of n, 1.60943791243 this is the total of all logs so far, 4.49980967033 this is the total of all logs so far, 6.10924758276 n = 7 log(n) = 1.94591014906 sum of logs of primes up to n = 8.05515773182 the ratio between the two = 0.869008433236

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

    so it seems as if it is adding NewLn to TotLn twice. and i have no idea why.

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

    oops nevermind i figured it out. looks like placing it in the form of a question on here made me think of it in a new context. hahahah

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

    I soooo identify with that last observation about posting a problem and then having the answer come to me. It's happend to me a ton of times. And if I may make a suggestion about style... It's more "Pythonic" to return True or False from isPrime(). Then you can just say "if isPrime():". The way you have it looks like a C programmer would write it, since C has no such thing as True or False.

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

    I feel like such the newb. :-) I'll post my code - I used a list to do everything, so my log sums was pretty easy. import math somelist = [] for x in range(1,10000): somelist.append(x) somelist.remove(1) for q in range(2,int(math.sqrt(10000)+1)): for y in (somelist): if y%q == 0: if y >= 4 and y !=q: somelist.remove(y) while len(somelist) > 1000: somelist.remove(somelist[-1]) print "1000th Prime is:", somelist[-1] z = 0 for a in somelist: #z = z + math.log(a,2) #Sum of logs, base 2. z = z + math.log(a) print "Sum of all those logs:",z

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

    I am going to rewrite this code to accomplish it without the lists...wish me luck!

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

    ps. Any constructive criticism of my code is welcomed!

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

    One thing I noticed right off is the way you build somelist. if you just replace everything from "somelist = []" to "somelist.remove(1) with the single line "somelist = range(2, 10000), you'll get the same result in a much cleaner way.

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

    Good point Radly...being completely green in the programming world, these are things I like to know. Sometimes it's hard to be critical of your own code, and I tend to miss the simple things.

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