SHOW:
|
|
- or go back to the newest paste.
| 1 | - | #compute n prime numbers |
| 1 | + | #sum the log of primes |
| 2 | - | #James Tuvell |
| 2 | + | |
| 3 | import math | |
| 4 | ||
| 5 | def is_prime(number): | |
| 6 | """Check positive integer for primeness | |
| 7 | It will return false for any none positive integer | |
| 8 | or the number 0 | |
| 9 | """ | |
| 10 | count = 2 | |
| 11 | ||
| 12 | if number <= 1: | |
| 13 | return False | |
| 14 | if number == 2: | |
| 15 | return True | |
| 16 | ||
| 17 | while count <= math.sqrt(number): | |
| 18 | if number % count == 0: | |
| 19 | return False | |
| 20 | count = count + 1 | |
| 21 | - | count = count + 1 |
| 21 | + | |
| 22 | - | |
| 22 | + | |
| 23 | #==== End of Defs ========== | |
| 24 | ||
| 25 | n = 40 | |
| 26 | logsum = 0 | |
| 27 | - | testnum=2 |
| 27 | + | |
| 28 | for i in range(2,n): | |
| 29 | - | for i in range(1000): |
| 29 | + | |
| 30 | - | primeflag=False |
| 30 | + | if is_prime(i): |
| 31 | - | while primeflag != True: |
| 31 | + | logsum = logsum + math.log(i) |
| 32 | - | primeflag = is_prime(testnum) |
| 32 | + | print i,logsum,logsum/n |
| 33 | - | testnum=testnum+1 |
| 33 | + | |
| 34 | - | print testnum-1, |
| 34 | + | print "The sum of the primes from 2 to ",n," is: ", logsum |
| 35 | print "in the ratio ",logsum ,":",n ,"is equal to:", logsum/n |