Python Forum

Full Version: Prime number Script Problem
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I was creating a script to find prime numbers, I finally figured out how to do it without errors, and nothing printed. Though clearly in the function it says to print the number when it finds it. Can anyone help with this problem?

wrong = 0
primelist = [2]
check = 0
number = 3
def PrimeFinder():
   global check
   global number 
   global wrong
   for i in primelist:
       if number % i == 0 or number % 2 == 0:
           check += 1
           wrong += 1
           if check == len(primelist):
               number +=1
               check = 0
               wrong = 0
               PrimeFinder()
       else:
           check += 1
           if check == len(primelist) and wrong == 0:
               primelist.append(number)
               number = number + 1
               check = 0
               wrong = 0
               print(number)
               PrimeFinder()
           elif len(primelist):
               number += 1
               check = 0
               wrong = 0
               PrimeFinder()
       
you never call the PrimeFinder function

add at bottom of script
if __name__ == '__main__:
   PrimeFinder()
I just ran it -- lot's of issues!
you can see how others do it: https://stackoverflow.com/questions/1570...me-numbers
Ok now I would like to know how to hide the messages saying where the function is stored so it just shows the numbers
You don't hide them, it's an issue you can only recurse so many times before you run out of heap (stack) space
This applies to most languages unless they have expandable heap.
Even then, eventually you will run out of memory.