4

I am trying to find the nth prime number, without looking at other solutions I am building this function

def Nth_Prime(Target):

    Primes = [2,3,5,7,11,13,17,19]
    Num = 20
    N=8
    Count=0

    while(N<Target):
        for i in Primes:
            if Num%i==0:
                Num+=1
                i=2
            else Num%i!=0:
                Count+=1
                if Count==len(Primes):
                    i=2
                    Primes.append(Num)
                    N+=1
                    print(Primes)
                    Num+=1
                    print(Count)

Nth_Prime(10002)

Now, while it may not be the most efficient, what I am trying to understand is why I can't reset my variable I to the beginning of the array for each loop? the function correctly finds 23 as the next prime number then it goes wrong

help appreciated.

EDIT: I got it! Thanks to all, time to clean it up a little and make it more aesthetic

def Nth_Prime(Target):

    Primes = [2,3,5,7,11,13,17,19]
    Num = 20
    N=8
    Count=0
    x=0

    while(N<Target):
        i = Primes[x]
        if Num%i==0:
            Num+=1
            x = 0
        elif Num%i!=0:
            Count+=1
            x+=1
            if Count==len(Primes):
                Primes.append(Num)
                N+=1
                Num+=1
                Count = 0
                x=0
    print(Primes[10000])

Nth_Prime(10002)
78282219
  • 593
  • 5
  • 21
  • for loop overrides the value just before starting the loop. – Vineeth Sai Sep 21 '18 at 10:27
  • you are iterating trough a list, so in the next iteration your `i` will be the next value in the list, no matter what you did with it before, use a while loop instead and indexing instead – Netwave Sep 21 '18 at 10:27
  • Because that's just the way `for` loops work in Python. You can do whatever you like with `i` inside the loop, but it will get set to the next value from `range` (or whatever you're iterating over) when you get back to the top of the loop. – PM 2Ring Sep 21 '18 at 10:27
  • I will return with an edit with my attempt at a while loop – 78282219 Sep 21 '18 at 10:28
  • Hm, so how would I use a while loop whilst still using my array of primes? – 78282219 Sep 21 '18 at 10:29
  • BTW, `else Num%i!=0:` is invalid syntax. You can't give `else` a condition. If you need to do that, use `elif`. However, there's no need for a condition there. If the `Num%i==0` condition is false then `Num%i!=0` _must_ be true, and vice versa. – PM 2Ring Sep 21 '18 at 10:34
  • You can keep using your `for i in Primes:` loop. But instead of doing `i = 2`, use `break` to exit the inner loop, after you finish incrementing `Num` and printing stuff. – PM 2Ring Sep 21 '18 at 10:41

1 Answers1

3

No matter what you do with i variable in the loop for is going to reinitialize it with the next value from primes list. That's just how the for loop works.

so you can imagine the first line of for loop to be

for loop:
    i = Primes[x] # initialize
    ... # do stuff
    x+=1
    i = 'something' # HAS NO EFFECT for next iteration
Vineeth Sai
  • 3,389
  • 7
  • 23
  • 34
  • 1
    Better post it as a comment, just like you already did ;) – Sheldore Sep 21 '18 at 10:30
  • 2
    This seems like a proper answer to "why I can't reset my variable I to the beginning of the array for each loop"? I was considering writing an answer very similar to this one. – Rory Daulton Sep 21 '18 at 10:32