Here's my code for PS 1: what do you think? from math import * #problem 1 primes = () for n in range(2, 7920): for i in range(2, n): if n % i == 0: break else: primes = primes + (n, ) #Problem 2 add = 0 l = 0 for x in range(0,1000): z = primes [x] ## print z add += log(z) print add / primes [x] ## print add, primes [x], add/ primes [x]
Did you try running it? There's no print statement on one. I feel like it's not "finding" the 1000th square root, it's printing out the range which ends on the correct number. I do not have a math background, I'm a philosophy major, so logs are beyond me. I won't try to evaluate your number 2. Haha
pls use a code pasting site - http://dpaste.com/725281/ at line 6 you limited your 'search' range to just above the 1000th prime - which means you already knew what it was - can you change it to be more generic and find the nth prime (in this case n=1000) - i think that was the intent of the problem. lines 17 and 18 - when you want to iterate over items in a sequence use the 'for item in sequence:' idiom: http://pastebin.com/BMbut2P7 If you need access to the item index and the item use enumerate - http://codepad.org/8x0mERKg
I did this for 1.1: http://dpaste.com/727098/ It seems like an inelegant mess, but it does run.
Erm, I didn't see any instructions or directions for 1.1, besides "write a program that computes and prints the 1000th prime number." All I saw was a list of 'hints,' which I interpreted as being optional to follow. But you're quite right, I did violate some of them (mostly because I didn't really look at them.) I won't bother posting 1.2 as it turns out to be very easy once 1.1 is done.
line nine: changing a 'for loop' variable inside of the loop can produce unexpected results - it is better to let the for loop increment the variable unless there is a really good reason to do otherwise and you thoroughly understand the consequences. you are using iter to keep track of how many primes have been found and you are using a for loop to generate possible primes (x). the way you structured it, you have to guess at the range of numbers to generate in order to find 1000 primes - it is best not to write programs that have guess's in them; two bad things can happen, you guess is wrong and you don't get what you want and if you want to use that code for a different prime, you have to guess again. you could probably eliminate the guessing and make it more generic by using a while loop along with iter and another variable that represents the nth prime you are looking for. iter happens to be a Python builtin function - you shouldn't use variable names that are the same as Python functions. http://docs.python.org/library/functions.html#iter
bwCA, that's all good reasoning. I rewrote the problem in accordance with it (and just stuck with while loops this time, as they seem to make things less hairy): http://dpaste.com/727426/ I get now why you're not asked to test 2 for primality, it's annoying with while loops. And, yeah, I probably should have figured something was up when iter turned purple... thanks for the list.
Join our real-time social learning platform and learn together with your friends!