MIT OpenCourseWare 6.00 Introduction to Computer Science and Programming: Lesson 2, HW 1
from math import log
from math import *
print("Begin solving Problem 1.")
def isItPrime(numberToCheck):
for n in range(2,numberToCheck):
if numberToCheck%n == 0 :
return False
return True
primes = []
primeCounter=0;
numbers = 2
while True:
if numbers == 2 or isItPrime(numbers):
primes.append(numbers)
primeCounter += 1
if primeCounter == 1000:
#solution to problem 1
print("The thousandth prime is", numbers)
break
numbers += 1
print("\nBegin solving Problem 2.")
targetN=int(input("What is the target N? "))
totalLog = 0
for numbers in range(2,targetN,1):
if numbers == 2 or isItPrime(numbers):
totalLog += log(numbers)
#solution to problem 2
print("Sum of Log of Primes up til N is ",totalLog)
print("N is ", targetN)
print("ratio of Sum of Log of Primes to N is ", totalLog/targetN)
feureau
1 year ago