MIT 6.00 Intro Computer Science (OCW) 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 Foreverangel: Three teams each made several attempts at completing an obstacle course in less than 10 minutes.
1 minute ago 6 Replies 0 Medals Sprinkles285: Determine the answer to 3 + (u22125) and explain the steps using a number line. (
8 seconds ago 6 Replies 0 Medals FrogGirlEmmy: can a unvaccinated Ferret play with a vaccinated ferret?
36 minutes ago 15 Replies 4 Medals Foreverangel: Complex changes have shaped Eurasia since the Soviet Union's disintegration in 1991.
1 hour ago 3 Replies 0 Medals baileyadams: I suck at singing but anyway whats yalls opion ik its terrible https://voca.ro/14
1 hour ago 11 Replies 2 Medals omglolove12: What is something that charms you everytime you see it/them. for me its: youuuu!!
1 hour ago 1 Reply 0 Medals Joe348: Sinc everyone else was doing it.... how do i sound??? https://voca.ro/19FFNtqa4zR
1 hour ago 9 Replies 0 Medals aramondi: Calculate the number of grams of Fe2O3 needed to react with 19.0 g C.
2 minutes ago 2 Replies 0 Medals iosangel: Read the line from the first stanza of u201cO Captain! My Captain!u201d The ship
1 hour ago 3 Replies 0 Medals 5824234167: I hope you like this my voice cracked a little. Enjoy. Lovely Billie Eilish ft. K
2 hours ago 2 Replies 0 Medals