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 Flawer1234: A video game is on sale at Amazon for 15% off the original price of \$24.50. Both Pat and Henri want to purchase the game, but they only have \$21 each.
1 minute ago 0 Replies 0 Medals Flawer1234: 03.07 Set Sail Discussion-Based Assessmentu2014411 File Part 1: Reviewing the Lessons Use the 3-2-1 charts from your 411 Files, the interactive on the intro
1 minute ago 0 Replies 0 Medals rainbowsalt: im bored as f
1 minute ago 2 Replies 0 Medals rainbowsalt: give and the treatment of the following disease, in a tabular form: i) asthma ii)
15 minutes ago 14 Replies 0 Medals jennyyyyaaaaa00: Prehistoric age, Industrial age, Electronic age, or Information/New age, which of the following era.
1 hour ago 0 Replies 0 Medals shadieisdead: If a governor feels it is necessary, he/she can call a(n) __________ of the state legislature to discuss emergencies or pressing issues.
1 hour ago 0 Replies 0 Medals jennyyyyaaaaa00: Prehistoric age, Industrial age, Electronic age, or Information/New age, which of the following era.
2 hours ago 5 Replies 0 Medals Vocaloid: [my art] quick sketch of another planned coord
41 minutes ago 6 Replies 5 Medals Stasiamarie: Primary accent of bilateral
2 hours ago 4 Replies 0 Medals rainbowsalt: Help pls its my hw
1 hour ago 22 Replies 1 Medal