Python prime list wrong?

I tried several different methods to get a total of 10001.

def isPrime(value):
  if ((2**value)-2)%value==0:
    return True

def nthPrime(n):
  count = 0
  value = 1
  while count < n:
    value += 1
    if isPrime(value):
      count += 1
  return value

When 10001 is an argument, it returns 103903. When I expect 104743.

I tried:

primes = []
for i in range(2,105000):
  if ((2**i) - 2) % i == 0:
    primes.append(i)

print primes[10001] ---> 103903
+3
source share
3 answers

I think your main sieve is wrong. Try using the isPrime function, which takes the number of each smaller number. If any of them is 0, then the number is composite (not simple). As far as I know, there is not the only comparison that will tell you whether a number is prime, as the isPrime function suggests.

+2
source

Is this for Project Euler? It seems familiar to me.

isPrime , TEOUltimus, , .

Python

, .

+2

You can create a function to generate prime numbers, this may be slow, but it will work.

Here is my function:

def PrimesSieve(limit):
    np=set()
    p=[2]
    for i in xrange(1,limit+1,2):
            if i == 1: continue
            if {i} & np: continue
            beg=2 if i % 2 == 0 else 0
            for j in xrange(beg,int(limit)+1,i):
                    np.add(j)
            p.append(i)
    return p
+2
source

All Articles