3

I've tried with several different methods to get the 10001 prime number.

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 the argument this returns 103903. When I'm expecting 104743.

I've tried:

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

print primes[10001] ---> 103903
tijko
  • 6,089
  • 11
  • 39
  • 54
  • 1
    What does your `isPrime` function answer for numbers: 1105, 1729, 2465 ? You'd know at plain sight that 1105 and 2465 are *not* prime... (Hint: http://en.wikipedia.org/wiki/Carmichael_number) – Pablo May 19 '12 at 03:27
  • Pseudo-prime numbers eh, I vaguely remember coming across this before but, this has given it some context. Good stuff, thanks! – tijko May 19 '12 at 03:57

3 Answers3

2

I believe your prime sieve is wrong. Try using an isPrime function that takes that number mod each lesser prime. If any of these are 0 then the number is composite (not prime). To the best of my knowledge there is no single comparison that will tell you if a number is prime, as your isPrime function assumes.

kindall
  • 158,047
  • 31
  • 244
  • 289
TEOUltimus
  • 184
  • 8
2

Is this for Project Euler? It does seem familiar to me.

Your isPrime function is wrong, like TEOUltimus said, there is no one way to tell if a number is prime.

Simple Prime Generator in Python

This pretty much answers your question i guess.

Community
  • 1
  • 1
Ayrx
  • 1,792
  • 4
  • 21
  • 31
2

You can make a function to generate primes, this might be slow but it will work.

Here's my function to do so :

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
  • I've never used the sieve method. After seeing your implementation it became much clearer. You populate an array of factors by stepping through a range incrementing by the integer, if the integer is in the factors array its not a prime if it isn't it is. – tijko Oct 02 '15 at 21:17
  • Also, I went and wrote a sieve using the description I gave above after I got the gist. Only, I started the outer loop at 2 and have the inner loop start at 0. I got correct results, I'm thinking this is a slight performance improvement? – tijko Oct 02 '15 at 21:23
  • A small optimization for your code. You can use logical operators on `sets()`. `if i in np: continue` can be changed to `if {i} & np: continue` – tijko Oct 02 '15 at 21:25
  • Thank you for optimization :) – aban-developer Oct 03 '15 at 05:09
  • Is it faster to use `if {i} & np` or `if i in np`? – aban-developer Oct 03 '15 at 05:13
  • I timed those out and I was getting a slight improvement with `if {i} & np` for finding all primes > 1 million. For finding all primes < 1 million `if i in np` was slightly faster. – tijko Oct 03 '15 at 06:19