Python Forum
Prime number Script Problem - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Prime number Script Problem (/thread-4075.html)



Prime number Script Problem - Codezters - Jul-21-2017

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()
       



RE: Prime number Script Problem - Larz60+ - Jul-21-2017

you never call the PrimeFinder function

add at bottom of script
if __name__ == '__main__:
   PrimeFinder()



RE: Prime number Script Problem - Larz60+ - Jul-21-2017

I just ran it -- lot's of issues!
you can see how others do it: https://stackoverflow.com/questions/15706911/generator-in-python-generating-prime-numbers


RE: Prime number Script Problem - Codezters - Jul-21-2017

Ok now I would like to know how to hide the messages saying where the function is stored so it just shows the numbers


RE: Prime number Script Problem - Larz60+ - Jul-21-2017

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.