1

I have just started coding, and I don't seem to be getting it quite right. I would like some feedback on my program.

I am getting 7753 instead of 7919

primes = []
sk     = [] # list for all None values

def primtal(a):
    if ((2 ** a) - 2) % a == 0:
        return a

t = 2
while len(primes) < 1001:
    kand = primtal(t)
    if kand == None:
       sk.append(kand)
    else:
        primt = kand 
        primes.append(primt)
    t = t + 1

print primes[1000]
jeremy
  • 307
  • 4
  • 15
nuevo
  • 11
  • 2
  • 3
    Is the check `((2**a)-2) % a == 0` checking whether a number is prime? I've not seen that one before... – mgilson Feb 13 '15 at 22:40
  • 2
    As @mgilson noted, [pseudoprimes](http://en.wikipedia.org/wiki/Fermat_pseudoprime) like 341 == 11*31 will cause problems for your `primtal`. (Plus remember that `primes[1000]` is the 1001th prime..) – DSM Feb 13 '15 at 22:43
  • @mgilson it seems to work rather... – Zizouz212 Feb 13 '15 at 22:45
  • Please can you give us a link to the algorithm you're using? Like `mgilson` I haven't seen that one before. Also, the values of your variables `sk` and `primt` seem to be pointless as they are never used. `sk` is just an array of `None` values and `primt` is just a copy of `kand`. – Borodin Feb 13 '15 at 22:46
  • 1
    @Zizouz212 it's true that for any prime p for all k < p, k^(p-1) = 1 (mod p), but all non-primes satisfy that property for some values of k, and some (carmichael numbers) satisfy it for all k despite not being prime. – genisage Feb 13 '15 at 22:48
  • What does `sk` do here? Even with the comment it's not clear. Isn't this basically just `if kand is None: pass` ? – Adam Smith Feb 14 '15 at 00:34
  • @mgilson: This is a particularly inefficient version of the [Fermat Pseudoprime test](http://en.wikipedia.org/wiki/Fermat_pseudoprime) to the base 2. – President James K. Polk Feb 14 '15 at 01:34
  • Ye, primt was unecessary and sk i used as a trashbin to put all kands that returned None so the primes array would only house primenumbers. But that was before i knew of ": pass" the algorithm i found in some other question here on stackoverflow, i believe, a couple of weeks ago, dunno which one though – nuevo Feb 14 '15 at 16:57

0 Answers0