Every line of 'isprime python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.
3 def is_prime(n): 4 i = 3 5 while i * i <= n: 6 if n % i == 0: return False 7 i += 2 8 return True
1 def is_prime(n): 2 if n < 2: 3 return False 4 5 i = 2 6 7 while i * i <= n: 8 if n % i == 0: 9 return False 10 11 i += 1 12 13 return True
7 def isPrime(num): 8 # Returns True if num is a prime number, otherwise False. 9 10 # Note: Generally, isPrime() is slower than primeSieve(). 11 12 # all numbers less than 2 are not prime 13 if num < 2: 14 return False 15 16 # see if num is divisible by any number up to the square root of num 17 for i in range(2, int(math.sqrt(num)) + 1): 18 if num % i == 0: 19 return False 20 return True
21 def is_prime(n): 22 for i in xrange(2, int(sqrt(n)) + 1): 23 if n % i == 0: 24 return False 25 26 return True
3 def is_prime(n): 4 if n <= 1: 5 return False 6 elif n == 2: 7 return True 8 elif n % 2 == 0: 9 return False 10 for i in xrange(3, int(sqrt(n))+1, 2): 11 if n % i == 0: 12 return False 13 return True
6 def is_prime(n): 7 ''' 8 checks if a number is prime 9 ''' 10 if n < 2: 11 return False 12 if n == 2: 13 return True 14 for x in range(2, int(n**0.5)+1, 2): 15 if n % x == 0: 16 return False 17 return True
23 def isPrime(n): 24 return not any(x for x in range(2, int(sqrt(n)) + 1) if n % x == 0)
20 def prime(num): 21 # num is actually a string because input() returns strings. We'll convert it to int 22 num = int(num) 23 24 if num < 0: 25 print("Negative integers can not be prime") 26 quit() 27 if num is 1: 28 print("1 is neither prime nor composite") 29 # See how I lazily terminated program otherwise it'd forward "None"(default behaviour of python when function 30 # returns nothing) rather than True or False. Which could mess up the program. 31 # If we hit this if statement above statement is printed then program exits. 32 quit() # Now you don't need to get sys.exit() to exit python has quit to handle the same thing 33 if num in [2, 3]: 34 # if given argument is 2 or 3, it is prime. We used list without defining a variable which is perfectly valid 35 return True 36 if num % 2 == 0: # excluding all even numbers except two. 37 return False 38 else: 39 # Here we are starting counter variable from 3 in range. Second argument excludes numbers above one third 40 # of the given argument. Third argument in range sets steps to take to 2. This makes loop to iterate odds 41 for x in range(3, int(num/3), 2): 42 # Checking if argument is divisible by counter. % is modulus operator which returns remainder of division 43 if num % x == 0: 44 return False 45 # It's okay to have more than one return statement when program hits return statement it exits the function. 46 return True
19 def isprime(n, precision=7): 20 # http://en.wikipedia.org/wiki/Miller-Rabin_primality_test#Algorithm_and_running_time 21 if n < 1: 22 raise ValueError("Out of bounds, first argument must be > 0") 23 elif n <= 3: 24 return n >= 2 25 elif n % 2 == 0: 26 return False 27 elif n < _smallprimeset: 28 return n in smallprimeset 29 30 31 d = n - 1 32 s = 0 33 while d % 2 == 0: 34 d //= 2 35 s += 1 36 37 for repeat in range(precision): 38 a = random.randrange(2, n - 2) 39 x = pow(a, d, n) 40 41 if x == 1 or x == n - 1: continue 42 43 for r in range(s - 1): 44 x = pow(x, 2, n) 45 if x == 1: return False 46 if x == n - 1: break 47 else: return False 48 49 return True
7 @staticmethod 8 def is_prime_num(k): 9 if k < 2: 10 return False 11 for i in range(2, k): 12 if k % i == 0: 13 return False 14 return True