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?
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?
Technically, the square root of the number
# 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?
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
Basically, you need a MINOR modification from code 1 to get code 2 working
## 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
That code seems to have a problem. Maybe use pastebin, because there seem to be problems.
# 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
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
implementing ....
If you come close to 7803, tell me. I will tell you why you may be a "little" off
7811.333
7811.59
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?
7802.6
so i set my limit too high (1000), instead of 999... thanks for the help!
Your answer is wrong. Do you count the math.log(2) somewhere in your code?
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
Then add math.log(2) somewhere in the code. The exact answer is near 7803.3
will do, thanks!
Join our real-time social learning platform and learn together with your friends!