Ask your own question, for FREE!
MIT 6.00 Intro Computer Science (OCW) 7 Online
OpenStudy (anonymous):

I'm having trouble with ps1 problem 2. The easiest/best way to do this seems to be: 1. Take the list of primes I made in the first problem 2. log all of those primes 3. put the logs of all those primes in a list 4. take the logs list and calculate the sum of the numbers 5. print the sum 6. sum/n to calculate the ratio 7. print the ratio Now putting that into code is a different story. I'm thinking a for statement would be best. How to I use the for statement on my previous list of primes? How do I do logs on that list of primes and stick all of those logs into a list?

OpenStudy (anonymous):

I'm also new to this class, and just went through this chapter yesterday. You're on the right track, you just need to learn the syntax. Take a look at my attachment and see if it helps. Let me know if you want me to get more specific.

OpenStudy (anonymous):

Sorry, when you import math, you still need to address log as math.log(myVariable)

OpenStudy (anonymous):

Strictly speaking, you don't need to keep a list of primes to solve this problem. Here is some pseudo-code: 1. Write a loop such that you stay in the loop until you have found the 1000th prime 2 Let "test_num" be the number you are testing for primality 3. Let "prime_count" represent the nth prime number you have found (e.g. when test_num = 29, prime_count should become 10 since that's the 10th prime) 4. Initialize a variable call "log_sum" which will store the sum of the log of the prime numbers 5. Determine if test_num is prime 6. If test_num is prime (e.g. 3), then take it's log and add it to log_sum (e.g. log_sum+=log(test_num) 7. When prime_count reaches 1000 then you break out of the loop

OpenStudy (anonymous):

But suppose you did want to keep a list of primes for the hell of it. It's actually not a bad way of checking your code to see you are correctly generating the prime numbers: 1. Initialize an empty list, calling it let's say primes=[ ] 2. As you come across a prime, append it to the list e.g. primes.append(test_num) You should get a list of primes like so [2,3,5,7,11,....]

OpenStudy (anonymous):

I actually used the list of prime numbers to find new prime numbers more efficiently. Instead of trying to divide all numbers into n where numbers < n, I only used prime numbers. It doesn't seem to make a big difference on the first 1000 primes, but above that it runs a little smoother

OpenStudy (anonymous):

@makeavellious. For discovering larger primes having an inventory of base primes to test is a much better algorithm. For that one can use a list. But for retrieving the 1000th prime it won't make much of a difference.

OpenStudy (anonymous):

faster algorithm than dividing by num< n is to use num < sqrt(n)

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!