MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
#Problem Set 1 - Problem 1
#Name: Username53
#Collaborators: None
#Time: 2 days
print "This program will find and display the 1000th prime"
n = 1000 #Sets limit on program
counter = 1 #Starts prime number counter at one (2 already counted)
prime = 3 #First test number is 3
limit = n #Total is limit
while (counter < limit): #Run only while the # of primes is less than limit
divisor = prime/2 #Divisor is the prime num./2 (integer division gives original divisor value of 1)
while (divisor > 1):
if prime % divisor == 0: #Tests if a number is prime
prime += 2 #If number is not prime, next odd number is selected
divisor = prime / 2 #Divisor is now set to next odd number/2
else: divisor -= 1 #Number is prime
counter += 1 #Increasers counter by 1
prime += 2 #Selects next odd number
print prime - 2 #Once 1000th prime is reached, it is printed
#Problem Set 1 - Problem 2
#Name: Username53
#Collaborators: None
#Time: 1 hour
print "This program will findthe sum of the logarithms of all the primes from 2 to some number n, and print out the sum of the logs of the primes, the number n, and the ratio of these two quantities."
import math #gets math tools
prime = 3
counter = 1 #counter
limit = int (raw_input("Please Enter a Number")) #Input total number of primes desired
sum_of_primes = math.log(2) #Start with log of 2
while prime < limit: #Run only while prime<limit
divisor = 3 #Start with divisor of 3
while (divisor < math.sqrt(prime) and prime%divisor <> 0): #This is done when divisor < sqrt (prime) and for primes
divisor += 2 #Sets divisor to next
if divisor > math.sqrt(prime):
sum_of_primes = sum_of_primes + math.log(prime) #Add log of prime to sum of the log of primes
prime += 2 #Sets prime to next odd integer
print sum_of_primes #Prints sum of the log of primes
print prime #The prime number chosen
ratio = sum_of_primes / prime #gets ration of sum to number chosen
print ratio #prints ratio
Username53
11 months ago