# Python PrimePy Module

We all have learned about prime numbers in our school days, and don't worry if any of us have forgotten about them. Prime numbers are basically those natural numbers that are only divisible by 1 or themselves, and the other definition of a prime number is that natural numbers that are greater than 1 and don't have any complete divisible factors other than the number itself or 1. All the prime numbers are odd except for the '2', which is the only even prime number.

All the prime numbers, except 2 and 3, can be expressed using any one of the two forms given below:

(i) 6a + 1

(ii) 6a - 1

In the above-given expressions, 'a' is a natural number.

We all may have heard that it is very easy to deal with the numbers through programming languages, and this is because most of the programming languages provide us the packages which we can use to classify and work with numbers of all types, such as integers, natural, and other types. Using these programming languages packages inside a program makes it very easy to deal with numbers, classify the numbers, and work with numbers. Like all other programming languages, Python also provides us multiple packages which we can use inside the Python programs to deal and work with numbers. Some of these packages come in-built the source Python, and some of them require to be installed when we have to work with them. PrimePy, the module of Python, is one of such Python packages about whom we have talked above. Using the primepy module in a Python program, we can work and perform many operations on prime numbers like finding prime factors of a number, etc. Therefore, we will learn about the primepy module in this tutorial and understand how we can use it and its functions in a Python program to work on prime as well as other numbers.

## Introduction to PrimePy Module of Python

PrimePy is a Python package or module which provides multiple functions that we can use to compute multiple operations related directly or indirectly to prime numbers. The biggest advantage of this module is that using this module, all the functions on prime numbers can be performed in less time. Since Prime numbers don't have any factors and that's why sometimes it becomes much difficult to work with them. In such instances, the primepy module becomes very important as it provides functions that we can use to carry out all these operations on prime numbers easily.

## Installation of PrimePy Module

As we have already stated in the introduction part, some Python modules don't come in-built inside with the Python source package. The primepy is one of such packages which doesn't come in-built, and therefore if we want to work with this module of Python, we have first to install this module in our device. There are multiple installation methods available which we can use to install the primepy module in our machine, but before we choose any of these methods, we should try to find out which one is the easiest and simplest for us. Therefore, we have installed the primepy module in our system by using the pip installer installation method in this part of the tutorial. Using the pip installer method to install the primepy module is the simplest and easiest method, and we just have to write the following pip command inside the terminal shell to install this module:

Once we write the above-given pip command inside the command prompt terminal of our system, we just have to press 'enter,' and installation of the primepy module will begin. Now, we have to only wait for some time until all dependencies for this module are successfully installed. As we can see, the primepy module is successfully installed in our device, and now we can import this module in the Python programs to use the functions of this module to work with prime numbers.

## Implementation of PrimePy Module of Python

We can use the primepy module in Python programs to perform many functions that include direct or indirect involvement of prime numbers. After importing the primepy module into a program, we can use the functions of this module to perform the required tasks on prime numbers.

We can perform the following several functions using the functions of the primepy module:

1) Checking Prime Number

2) Return 'n' numbers of prime number

3) Return first 'n' prime numbers

4) Prime numbers between two numbers and many others

We will perform these tasks using the functions of the primepy module inside the example programs of this section, and that's how we will understand the implementation of the primepy module.

### Implementation 1: Functions to Check If a given Natural Number is Prime:

We can use the primepy module to determine if the given number is prime or not. PrimePy module provides us with the check() function, which we can use to check the number is prime or not. Following is the syntax for using the check() function in a Python program:

Where num = a given natural number in the program

We can understand the implementation of the check() function of the primepy module by using this function in the following example program:

Example 1: Look at the following Python program where we find out if the given number is prime:

Output:

```Enter first natural number: 88
Enter second natural number: 673
Is the first natural number which is entered by the user in the input is prime?  False
Is the second number entered by user is prime?  True
```

As we can see, the output is printed in the form of true and false, stating whether the numbers entered by the user are prime or not. That's how we can write any Python program using the check() function of the primepy module to check that if the given number in the program is prime or not.

### Implementation 2: Printing all the Prime Factors of a given Natural Number in the Program:

Using the functions of the primepy module, we can easily print all the prime factors in the output of any given natural number in the program. The PrimePy module provides us with the factors() function that we can use in any example program to print all prime factors of the given user input number or number given in the program. Following is the syntax for using factors() function in a Python program:

Where num = a given natural number in the program

We can understand the implementation of factors() function of the primepy module to print all prime factors of a natural input number by using this function in the following example program:

Example 2: Look at the following program where we found out and printed all prime factors of the given user input numbers as a result:

Output:

```Enter first natural number: 243
Enter second natural number: 679
All prime factors of the first number entered by user:  [3, 3, 3, 3, 3]
All prime factors of the second number entered by user:  [7, 97]
```

As we can see, all prime factors of both the numbers entered by the user in the input are printed in the output, and that's how we can know all the prime factors of any given number.

### Implementation 3: Printing Lowest or First Prime Factor of a Number:

This implementation of the primepy module works very similarly to the previous implementation of this module. The only difference in both these implementations is that we will only print the first or lowest prime factor of a number, whereas previously, we were printing all prime factors of the given number. The primepy module provides us with the factor(), which we can use to print the lowest prime factor of any given number. Following is the syntax for using the factor() function in a Python program:

Where num = a given natural number in the program

We can understand the implementation of the factor() function of the primepy module to print the first prime factor of a number by using this function in the following example program:

Example 3: Look at the following program where we have used the factor() function to print the lowest or first prime factor of the given input numbers:

Output:

```Enter first natural number: 73
Enter second natural number: 279
The lowest prime factor of the first number entered by user:  73
The first prime factor of the second number entered by user:  3
```

As we can see, the lowest or first prime factor of both the input numbers is printed in the output, and that's how we can use the factor() function of the primepy module in any Python program to print the lowest prime factor of any given natural number.

### Implementation 4: Prime Numbers up to a given Natural Number:

Suppose we have a number given, and we have to find out how many prime numbers exist which are less than or equal to the given number. It is a very hectic and time-consuming task if calculated manually, and that's where we can take advantage of the primepy module. The primepy module provides us the option where we can print all the prime numbers up to a given number by the user. For this task, the primepy module provides us with the upto() function, which we can use with any natural number to print prime numbers upto the given number. Following is the syntax for using the factor() function in a Python program:

Where num = a given natural number in the program

We can understand the implementation of the upto() function of the primepy module for printing prime numbers up to a given number by the user by using this function in the following example program:

Example 4: Look at the following program where we have used the upto() function to print all the prime numbers up to the given numbers by the user in input():

Output:

```Enter first natural number: 15
Enter second natural number: 18
Following are all prime numbers that are either less than or equal to 15 as given by the user :  [2, 3, 5, 7, 11, 13]
Following are all prime numbers that are either less than or equal to 18 as given by the user :  [2, 3, 5, 7, 11, 13, 17]
```

As we can see, all the prime numbers which are less than or equal to the input numbers given by the user are printed in the output. That's how we can use the upto function to print all prime numbers upto a given natural number.

### Implementation 5: Printing First 'n' Prime Numbers:

This implementation of the primepy module is similar to the previous implementation of this module because, in both implementations, we are printing certain prime numbers according to the given number. Talking particularly about this implementation part, we will print first 'n' (where n is a natural number) prime numbers, and 'n' will be the given number in the program. The primepy module provides us with the first() function, which we can use to print first 'n' prime numbers in the output. Following is the syntax for using the first() function in a Python program:

Where num = a given natural number in the program

We can understand the implementation of the first() function of the primepy module to print the first 'n' prime factors by using this function in the following example program:

Example 5: Look at the following program where we have used the first() function to print the first 'n' prime factors of the given input number by the user:

Output:

```The number of first prime numbers you want to print: 7
These are first 7 prime numbers as the number entered by the user:  [2, 3, 5, 7, 11, 13, 17]
```

As we can see, first 'num' (user input numbers in the program) prime numbers are printed in the output, and that's how we can print any first 'num' prime numbers.

### Implementation 6: Getting all Prime Numbers that are present between any two given Input Numbers:

This implementation part of the primepy module is also very similar to the implementation of this module's upto() function. The only difference is that here, we will find prime numbers between any two given numbers, whereas previously, we were finding prime numbers upto a given number (or prime numbers between 1 and the given number). For performing the task to find out all prime numbers present between any two user input natural numbers, the primepy module provides us with the between() function. Following is the syntax for using this between() function for finding all prime numbers between two given numbers:

Output:

```Enter first natural number: 5
Enter second natural number: 41
Enter third natural number: 79
These are all prime numbers which are present between first and second number given by you:  [5, 7, 11, 13, 17, 19, 23, 29, 31, 37]
These are all prime numbers which are present between the second and third number given by you:  [41, 43, 47, 53, 59, 61, 67, 71, 73]
```

As we can see, all the prime numbers that are present between the two given natural numbers by the user are printed in the output, and that's how we can use the between() function of the primepy module to find out and print all prime numbers between any two given numbers.

### Feedback   