def is_divisible(num,prime=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199]):
for j in prime:
if (num % j) == 0:
return True
return False
def find_primes(N):
prime = [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199]
primes = []
n = range(2,N+1)
for num in n:
if is_divisible(num) is False:
primes.append(num)
return(prime+primes)
def brun(N):
list = find_primes(N-1)
list2 = find_primes(N)
combs = []
for x in list:
for y in list2:
if x - y ==2:
combs.append((1/x)+(1/y))
print(brun(10000))
running this program, finally i got a none.I don't know what is wrong, please help
brun
doesn't have a return statement, so by default it returns None
. So print(brun(10000))
prints None
.
Edit: Like said below, you probably want to have a return combs
statement at the end of brun
. However, combs
is a list with N(N-1)/2
elements in it, so I doubt you want to print the whole thing. You may want to print something like
print(brun(10000)[-1])
which will print the last element of combs
.
@Bill correctly answered your question. But let me take a moment to suggest a better algorithm; it's known as the Sieve of Eratosthenes, and was invented by a Greek mathematician over two thousand years ago. The idea is to initially mark all numbers less than n as possible primes, then for each prime, in order, to mark all of its multiples as non-primes, collecting the primes as you go:
def primes(n):
sieve, ps = [True] * n, []
for p in xrange(2, n):
if sieve[p]:
ps.append(p)
for i in xrange(p*p, n, p):
sieve[i] = False
return ps
Then you can compute the sum of the primes less than n by saying sum(primes(n))
. If you're interested in programming with prime numbers, I modestly recommend this essay at my blog.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.