10 examples of 'python program to find prime numbers in a list' in Python

Every line of 'python program to find prime numbers in a list' 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
23def findprimes(start, end):
24 for i in range(start, end):
25 if i not in Checked:
26 Checked.append(i)
27 if is_prime(i):
28 Primes.append(i)
12def primes():
13 yield 2
14 yield 3
15
16 for i in itertools.count(start=5, step=2):
17 if is_prime(i):
18 yield i
20def 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
7def 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
7def prime():
8 D = {9: 3, 25: 5}
9 yield 2
10 yield 3
11 yield 5
12 MASK = 1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 0, 1, 1, 0, 0,
13 MODULOS = frozenset((1, 7, 11, 13, 17, 19, 23, 29))
14
15 for q in it.compress(
16 it.islice(it.count(7), 0, None, 2),
17 it.cycle(MASK)):
18 p = D.pop(q, None)
19 if p is None:
20 D[q * q] = q
21 yield q
22 else:
23 x = q + 2 * p
24 while x in D or (x % 30) not in MODULOS:
25 x += 2 * p
26 D[x] = p
746def prime_number_factorisation(n):
747 if n < 2:
748 return [n]
749 i = 2
750 factors = []
751 while i * i <= n:
752 if n % i:
753 i += 1
754 else:
755 n //= i
756 factors.append(i)
757 if n > 1:
758 factors.append(n)
759 return factors
12def isprime(n):
13 n = int(n)
14 return n > 1 and all(n%i for i in islice(count(2), int(sqrt(n)-1)))
50def prime_factors(n):
51 """Lists prime factors of a given natural integer, from greatest to smallest
52 :param n: Natural integer
53 :rtype : list of all prime factors of the given natural n
54 """
55 i = 2
56 while i <= sqrt(n):
57 if n % i == 0:
58 l = prime_factors(n/i)
59 l.append(i)
60 return l
61 i += 1
62 return [n] # n is prime
6def 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
36def primes_from(prime_sieve):
37 for n, is_prime in enumerate(prime_sieve):
38 if is_prime:
39 yield n

Related snippets