def isPrime(n): """Checks if n is prime""" #Check for obvious True or False before entering loop, for efficiency. if n <= 1 or type(n) != int: ##A prime must be a positive whole number. ##1 is not a prime return False if n in [2, 3, 5, 7, 11, 13, 17, 19]: ##Quick check of the first few primes. return True if not n & 1: ##Check to see if n is even. ##2 is the only even prime. return False sifter = int(n**0.5) ##Only the first sqrt(n), any more is redundant. counter = 3 ##Start the counter at 11, the next known prime. #Enter loop to check all other possibilities. while counter <= sifter: if n % counter == 0: ##Test if ctr is a multiple of n. return False counter += 2 ##Only need to check the odd numbers. return True for i in range(500): if isPrime(i): print (i, 'isprime')
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question