from math import *
def primes_upto_n(n):
primes = [2]
choice = 1
while len(primes) < n:
choice += 2
rt = sqrt(choice)
isPrime = True
# since we need all primes from 2 to n, this optimization works
# we can prime-factor all non-prime numbers.
for i in range(0, len(primes)):
poss = primes[i]
if (choice % poss) == 0:
isPrime = False
break
if (poss >= rt):
break
if isPrime:
primes.append(choice)
return primes
primes = primes_upto_n(1000)
nthPrime = primes[-1]
sums = reduce(lambda sum,prime: sum+log(prime),primes)
print "Sum: ", sums
print "Nth Prime: ", nthPrime
print "sums / Nth Prime: ", sums/nthPrime
Yeah, this includes some weird-crazy optimizations >_<
no subject
from math import * def primes_upto_n(n): primes = [2] choice = 1 while len(primes) < n: choice += 2 rt = sqrt(choice) isPrime = True # since we need all primes from 2 to n, this optimization works # we can prime-factor all non-prime numbers. for i in range(0, len(primes)): poss = primes[i] if (choice % poss) == 0: isPrime = False break if (poss >= rt): break if isPrime: primes.append(choice) return primes primes = primes_upto_n(1000) nthPrime = primes[-1] sums = reduce(lambda sum,prime: sum+log(prime),primes) print "Sum: ", sums print "Nth Prime: ", nthPrime print "sums / Nth Prime: ", sums/nthPrimeYeah, this includes some weird-crazy optimizations >_<