10 examples of 'print n prime numbers in python' in Python

Every line of 'print n prime numbers in 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.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
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
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

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
16def primeGen(n):
17 for i in xrange(2, n):
18 prime = True
19 if i % 2 == 0 and i != 2:
20 continue
21 sqrtp = int(i ** 1 / 2)
22 for j in xrange(2, sqrtp):
23 if j % 2 == 0:
24 continue
25 if i % j == 0:
26 prime = False
27 break
28 if prime:
29 yield 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
1def 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
3def is_prime(n):
4 i = 3
5 while i * i <= n:
6 if n % i == 0: return False
7 i += 2
8 return True
1285def primefactors(n, limit=None, verbose=False):
1286 """Return a sorted list of n's prime factors, ignoring multiplicity
1287 and any composite factor that remains if the limit was set too low
1288 for complete factorization. Unlike factorint(), primefactors() does
1289 not return -1 or 0.
1290
1291 Examples
1292 ========
1293
1294 >>> from sympy.ntheory import primefactors, factorint, isprime
1295 >>> primefactors(6)
1296 [2, 3]
1297 >>> primefactors(-5)
1298 [5]
1299
1300 >>> sorted(factorint(123456).items())
1301 [(2, 6), (3, 1), (643, 1)]
1302 >>> primefactors(123456)
1303 [2, 3, 643]
1304
1305 >>> sorted(factorint(10000000001, limit=200).items())
1306 [(101, 1), (99009901, 1)]
1307 >>> isprime(99009901)
1308 False
1309 >>> primefactors(10000000001, limit=300)
1310 [101]
1311
1312 See Also
1313 ========
1314
1315 divisors
1316 """
1317 n = int(n)
1318 factors = sorted(factorint(n, limit=limit, verbose=verbose).keys())
1319 s = [f for f in factors[:-1:] if f not in [-1, 0, 1]]
1320 if factors and isprime(factors[-1]):
1321 s += [factors[-1]]
1322 return s
3def 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
21def 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
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

Related snippets