Skip to content

Instantly share code, notes, and snippets.

@joeegan
Created April 22, 2011 22:43
Show Gist options
  • Select an option

  • Save joeegan/937869 to your computer and use it in GitHub Desktop.

Select an option

Save joeegan/937869 to your computer and use it in GitHub Desktop.
py1b2.py
from math import *
def calculate_n_primes(n):
primes = []
prime_candidate = 1
global its_a_prime
its_a_prime = True
while len(primes) < (n + 1):
prime_candidate = prime_candidate + 1
divisor = 1
while divisor < prime_candidate-1:
its_a_prime = True
divisor = divisor + 1
if prime_candidate % divisor == 0:
its_a_prime = False
break
if its_a_prime == True:
primes.append(prime_candidate)
return primes
def calculate_nth_prime(n):
primes = calculate_n_primes(n)
return primes[n-1]
def calculate_primes_beneath_n(n):
primes = []
prime_candidate = 1
global its_a_prime
its_a_prime = True
while prime_candidate < n:
prime_candidate = prime_candidate + 1
divisor = 1
while divisor < prime_candidate-1:
its_a_prime = True
divisor = divisor + 1;
if prime_candidate % divisor == 0:
its_a_prime = False
break
if its_a_prime == True:
primes.append(prime_candidate)
return primes
def calculate_sum_of_logarithms_of_primes_beneath_n(n):
primes = calculate_primes_beneath_n(n)
sum_of_logs = 0
for prime in primes:
sum_of_logs = sum_of_logs + log(prime)
return sum_of_logs
print "The 1000th prime is "+ str(calculate_nth_prime(1000))
print "The sum of the logarithms of all the primes beneath 100 is: " + str(calculate_sum_of_logarithms_of_primes_beneath_n(100))
print "The ratio of the sum of the logarithms of all the primes beneath n (100) to n (100) is: " + str(calculate_sum_of_logarithms_of_primes_beneath_n(100)/100)
print "The sum of the logarithms of all the primes beneath 2000 is: " + str(calculate_sum_of_logarithms_of_primes_beneath_n(2000))
print "The ratio of the sum of the logarithms of all the primes beneath n (2000) to n (2000) is: " + str(calculate_sum_of_logarithms_of_primes_beneath_n(2000)/2000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment