Skip to content
Related Articles

Related Articles

Improve Article

PrimePy module in Python

  • Last Updated : 05 Aug, 2021

A prime number is a natural number greater than 1 whose only factors are 1 and the number itself. 2 is the only even Prime number. We can represent any prime number with ‘6n+1’ or ‘6n-1’ (except 2 and 3) where n is a natural number.

primePy is that library of Python which is used to compute operations related to prime numbers. It will perform all the functions in less time with the help of the functions of this primePy module. 

Installing Library

This module does not come built-in with Python. You need to install it externally. To install this module type the below command in the terminal. 

 pip install primePy  

Functions of primePy

1. primes.check(n): It will return True if ‘n’ is a prime number otherwise it will return False.

Example:



Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
print(primes.check(105))
print(primes.check(71))

Output: 

False
True

2. primes.factor(n): It will return the lowest prime factor of ‘n’.

Example: 

Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.factor(15)
print(a)
 
a = primes.factor(75689456252)
print(a)

Output: 

3
2

3. primes.factors(n): It will return all the prime factors of ‘n’ with repetition of factors if exist.

Example: 



Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.factors(774177)
print(a)
 
a = primes.factors(15)
print(a)

Output: 

[3, 151, 1709]
[3, 5]

4. primes.first(n) : It will return first ‘n’ prime numbers.

Example: 

Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.first(5)
print(a)
 
a = primes.first(10)
print(a)

Output: 

[2, 3, 5, 7, 11]
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29]

5. primes.upto(n): It will return all the prime numbers less than or equal to ‘n’.

Example: 

Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.upto(17)
print(a)
 
a = primes.upto(100)
print(a)

Output:



[2, 3, 5, 7, 11, 13, 17] 
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97] 
 

6. primes.between(m, n): It will return all the prime numbers between m and n.

Example: 

Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.between(4, 15)
print(a)
 
a = primes.between(25, 75)
print(a)

Output: 

[5, 7, 11, 13]
[29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73]

7. primes.phi(n): It will return the number of integers less than ‘n’ which have no common factor with n.

Example: 

Python3




# Importing primes function
# From primePy Library
from primePy import primes
 
 
a = primes.phi(5)
print(a)
 
a = primes.phi(10)
print(a)

Output: 

4
4

 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course




My Personal Notes arrow_drop_up
Recommended Articles
Page :