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
Similar Reads
Platform Module in Python
Python defines an in-built module platform that provides system information. The Platform module is used to retrieve as much possible information about the platform on which the program is being currently executed. Now by platform info, it means information about the device, it's OS, node, OS versio
6 min read
PyDictionary module in Python
AyDictionary It's a Python module used to fetch definitions of words, while googletrans provides translation services.meaningstranslationsInstallation To install AyDictionary run the following pip code on the terminal / command prompt: pip install AyDictionary googletrans==4.0.0-rc1Getting Started N
1 min read
Pyscaffold module in Python
Starting off with a Python project is usually quite complex and complicated as it involves setting up and configuring some files. This is where Pyscaffold comes in. It is a tool to set up a new python project, is very easy to use and sets up your project in less than 10 seconds! To use Pyscaffold, G
4 min read
Python Module Index
Python has a vast ecosystem of modules and packages. These modules enable developers to perform a wide range of tasks without taking the headache of creating a custom module for them to perform a particular task. Whether we have to perform data analysis, set up a web server, or automate tasks, there
4 min read
Prime or not in Python
Every programmer comes across the problem of checking whether a number is prime and indeed it's one of the basic level program in any language. I also tried it in many different languages, but the best language that I found to implement it is Python. It's just one single line and the program does it
3 min read
Python Fire Module
Python Fire is a library to create CLI applications. It can automatically generate command line Interfaces from any object in python. It is not limited to this, it is a good tool for debugging and development purposes. With the help of Fire, you can turn existing code into CLI. In this article, we w
3 min read
Python Modules
Python Module is a file that contains built-in functions, classes,its and variables. There are many Python modules, each with its specific work.In this article, we will cover all about Python modules, such as How to create our own simple module, Import Python modules, From statements in Python, we c
7 min read
Import module in Python
In Python, modules allow us to organize code into reusable files, making it easy to import and use functions, classes, and variables from other scripts. Importing a module in Python is similar to using #include in C/C++, providing access to pre-written code and built-in libraries. Pythonâs import st
3 min read
__future__ Module in Python
__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module. I
4 min read
Docopt module in Python
Docopt is a command line interface description module. It helps you define a interface for a command-line application and generates parser for it. The interface message in docopt is a formalized help message. Installation You can install docopt module in various ways, pip is one of the best ways to
3 min read