A community for students.
Here's the question you clicked on:
 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?
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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0made 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
 5 years ago
Best ResponseYou've already chosen the best response.0Technically, the square root of the number

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.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 squareroot function?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0I 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
 5 years ago
Best ResponseYou've already chosen the best response.0Basically, you need a MINOR modification from code 1 to get code 2 working

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0That code seems to have a problem. Maybe use pastebin, because there seem to be problems.

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.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

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Okay, 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
 5 years ago
Best ResponseYou've already chosen the best response.0If you come close to 7803, tell me. I will tell you why you may be a "little" off

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Okay. 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
 5 years ago
Best ResponseYou've already chosen the best response.0so i set my limit too high (1000), instead of 999... thanks for the help!

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0Your answer is wrong. Do you count the math.log(2) somewhere in your code?

anonymous
 5 years ago
Best ResponseYou've already chosen the best response.0no, 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
 5 years ago
Best ResponseYou've already chosen the best response.0Then add math.log(2) somewhere in the code. The exact answer is near 7803.3
Ask your own question
Sign UpFind more explanations on OpenStudy
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
 Engagement 19 Mad Hatter
 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.