MIT 6.00 Intro Computer Science (OCW) 63 Online OpenStudy (anonymous):

Don't know what I am missing with problem set 1 b OpenStudy (anonymous):

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 =  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. OpenStudy (anonymous):

for one thing, isPrime == 0 is a comparison, not an assignment. OpenStudy (anonymous):

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. OpenStudy (anonymous):

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. OpenStudy (anonymous):

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" OpenStudy (anonymous):

I generally copy everything before I post, in case it gets stuck OpenStudy (anonymous):

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? OpenStudy (anonymous):

right. and what is that less than? OpenStudy (anonymous):

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. OpenStudy (anonymous):

Can you paste your current version? OpenStudy (anonymous):

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 =  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 OpenStudy (anonymous):

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

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

No problem. I'm glad you got it!

Latest Questions Azayliaa: i need helpp :/
38 minutes ago 12 Replies 1 Medal desss: are any of yall good at mathh!!??
1 hour ago 18 Replies 2 Medals demonwolftwin: who was the 2rd presseddent
4 hours ago 1 Reply 0 Medals Tbone: I made one last edit I'ma stop making them for a while
2 hours ago 22 Replies 8 Medals Arieonna: i got a book of all green paper from christmas but i Don't know what to do with i
5 hours ago 8 Replies 1 Medal yomomalikebracobama: e
6 hours ago 6 Replies 1 Medal yomomalikebracobama: help u tigers
6 hours ago 10 Replies 2 Medals Williammcgeorge: does anyone want to see some of my drawings
3 hours ago 19 Replies 4 Medals Joe348: Math 4 Vectors/Vectors Applications
1 hour ago 3 Replies 2 Medals OLIVER69: Made my first ever song, sorry if my voice is very quiet n I know it ain't very good, but here: https://www.
5 hours ago 11 Replies 1 Medal