Looking for something else?

Not the answer you are looking for? Search for more explanations.

## More answers

Looking for something else?

Not the answer you are looking for? Search for more explanations.

- anonymous

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?

At vero eos et accusamus et iusto odio dignissimos ducimus qui blanditiis praesentium voluptatum deleniti atque corrupti quos dolores et quas molestias excepturi sint occaecati cupiditate non provident, similique sunt in culpa qui officia deserunt mollitia animi, id est laborum et dolorum fuga.
Et harum quidem rerum facilis est et expedita distinctio. Nam libero tempore, cum soluta nobis est eligendi optio cumque nihil impedit quo minus id quod maxime placeat facere possimus, omnis voluptas assumenda est, omnis dolor repellendus.
Itaque earum rerum hic tenetur a sapiente delectus, ut aut reiciendis voluptatibus maiores alias consequatur aut perferendis doloribus asperiores repellat.

Get our expert's

answer on brainly

SEE EXPERT ANSWER

Get your **free** account and access **expert** answers to this

and **thousands** of other questions.

Get your **free** account and access **expert** answers to this and **thousands** of other questions

- anonymous

- katieb

See more answers at brainly.com

Get this expert

answer on brainly

SEE EXPERT ANSWER

Get your **free** account and access **expert** answers to this

and **thousands** of other questions

- anonymous

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?

- anonymous

Technically, the square root of the number

- anonymous

# 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?

Looking for something else?

Not the answer you are looking for? Search for more explanations.

- anonymous

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

- anonymous

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

- anonymous

## 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

- anonymous

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

- anonymous

# 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

- anonymous

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

- anonymous

implementing ....

- anonymous

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

- anonymous

7811.333

- anonymous

7811.59

- anonymous

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?

- anonymous

7802.6

- anonymous

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

- anonymous

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

- anonymous

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

- anonymous

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

- anonymous

will do, thanks!

Looking for something else?

Not the answer you are looking for? Search for more explanations.