| by Arround The Web | No comments

Isprime Python

The numbers which are not the product of other integer numbers are called prime numbers. In Python, prime numbers have an integer type that is larger than “1”. The prime number can be divisible by itself rather than other numbers. More specifically, several methods can be utilized to check whether the number is prime.

This post will talk about different ways to view whether the given integer number is prime or not in Python.

How to Check Isprime Number in Python?

These approaches are used to show if a given integer number is a prime number or not in Python:

Method 1: Check Isprime Number in Python Using “sympy.isprime()” Method

The “sympy.isprime()” method is utilized for executing symbolic mathematics. It is a built-in function of the “sympy” library that is used to determine whether a provided number is a prime number or not and returns results in a boolean.

Example

Initially, we import the “sympy” library:

import sympy

Now, use the “sympy.isprime()” method along with the desired number to check is prime or not inside the “print()” function:

print("1st Provide Number is :",sympy.isprime(8))

print("2nd Provide Number is :",sympy.isprime(2))

It can be seen that the specified first number is not prime. On the other hand, the second number is prime:

Method 2: Check Isprime Number in Python Using “while” Loop

You can use the “while” loop for checking whether the specified integer number is a prime number or not in Python. The “while” loop first checks the conditions, if it becomes true then it can execute the rest of the code otherwise it terminates.

Example

First, declare the integer type variable and initialize it:

c_num = 8

Then, declare the two more integer type variable which contains the desired reminder value “0” and the number from which it will start the checking conditions respectively “2”:

a = 0

b = 2

Now, first, while loop will check the provided condition:

while b <= c_num / 2:

if (c_num % b) == 0:

     a = 1

     break

  b+=1

if a:

print("Not Prime Number")

else:

print("Prime Number")

In the above-provided code:

  • First, the loop will check if the provided condition “b = 2” is less than or equal to “c_num = 8” and is divided by 2. If true, then it will be executed further.
  • The remainder will be calculated to view if the “c_num” is fully divided by any other number than itself. If the particular condition is right, then the value of the “a” will be updated.
  • If the given condition is not satisfied, then it will not be updated, and the answer will be displayed.

Output

Method 3: Check Isprime Number in Python With User-defined Function

To check whether the provided number is prime or not in Python, we can define a function by utilizing the “def” keyword.

Example

First, declare a function named the “checkisprime()” function that takes an integer number as a parameter. Then, by using the “if” condition check the provided number is greater than one. If it is, the “for” loop will be executed to check if “a” is fully divided by the “y”. Then, the provided integer number is not a prime number:

def checkisprime(a):

if a > 1:

for y in range(2, int(a/2) + 1):

if (a % b) == 0:

               print("is not a prime number")

break

else:

             print("The Provided Number is a Prime Number")

else:

       print("The Provided Number is Not a Prime Number")

c = 7

checkisprime(c)

On the other hand, if the number is not greater than one, else code block will be executed.

Output

We have explained the different ways to show whether the integer number is prime or not in Python

Conclusion

To check if the provided number is prime, the “sympy.isprime()” method, the “while” loop, and the user-defined function are used. The “sympy.isprime()” is the built-in method of the “sympy” library. This article described about multiple techniques to check whether the number is prime or not in Python.

Share Button

Source: linuxhint.com

Leave a Reply