I'm learning a lot by looking at everyone's solutions after working out my own much less sophisticated ones.
Here are mine. I stuck to while and if statements, since those are about the the only things I understand so far.
Problem 1:
count=2 #keeps track of which prime number we are at, assume 2 is the first prime candidate=3 #the number we are checking to see if it is prime test_divisor=3 #the number we are dividing by while count<1001: #program will continue until we reach the 1000th prime while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate remainder=candidate%test_divisor if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate candidate+=2 #only odd numbers need to be checked test_divisor=3 #must reset this for next candidate else:test_divisor+=2 #if there is a remainder we will try the next divisor, no need to use even divisors count+=1 test_divisor=3 candidate+=2 print "The 1000th prime number is", candidate-2
Problem 2:
from math import * n=input("What number do you want to stop at?") candidate=3 #the number we are checking to see if it is prime test_divisor=3 #the number we are dividing by OldLog=log(2) while candidate<=n: #program will continue until we reach the input number while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate remainder=candidate%test_divisor if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate candidate+=2 test_divisor=3 #must reset this for next candidate else:test_divisor+=2 #if there is a remainder we will try the next divisor NewLog=OldLog + log(candidate) OldLog=NewLog test_divisor=3 candidate+=2 print "Your number is", n print "The sum is", OldLog print "The ratio of the sum to your number is", OldLog/n
no subject
Here are mine. I stuck to while and if statements, since those are about the the only things I understand so far.
Problem 1:
count=2 #keeps track of which prime number we are at, assume 2 is the first prime
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
while count<1001: #program will continue until we reach the 1000th prime
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2 #only odd numbers need to be checked
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor, no need to use even divisors
count+=1
test_divisor=3
candidate+=2
print "The 1000th prime number is", candidate-2
Problem 2:
from math import *
n=input("What number do you want to stop at?")
candidate=3 #the number we are checking to see if it is prime
test_divisor=3 #the number we are dividing by
OldLog=log(2)
while candidate<=n: #program will continue until we reach the input number
while candidate>=(test_divisor*test_divisor): #we can stop dividing when the divisor gets bigger than the square root of our candidate
remainder=candidate%test_divisor
if remainder==0: #if there is no remainder, the number is not prime so we will go to the next candidate
candidate+=2
test_divisor=3 #must reset this for next candidate
else:test_divisor+=2 #if there is a remainder we will try the next divisor
NewLog=OldLog + log(candidate)
OldLog=NewLog
test_divisor=3
candidate+=2
print "Your number is", n
print "The sum is", OldLog
print "The ratio of the sum to your number is", OldLog/n