banner



How To Find Prime Numbers In Python

This article will learn how to check if a number is prime or not in Python. Usually, we all know some common methods using library functions or without using library functions. But how many of us know that there are 6 means to check a prime number. Possibly some of us will be familiar with some methods. Simply this article will teach you all the possible ways. Let united states of america move on to bank check if a number is prime or not.

In the number organization, we have ii types of numbers. They are Prime and composite. Prime numbers are the numbers that are not the product of any other numbers. These numbers are e'er natural numbers. For example, 13 is a prime number. Because we cannot get this number as a production of any other two numbers except to the product of one, on the other hand, if we take 4, information technology will show a upshot as a composite because it is a product of 2X2. I promise now all are articulate about prime numbers.

The following methods are available:

  1. functions
  2. if-else statements
  3. math module
  4. sympy library
  5. primePy library
  6. is_integer office

Method 1: Using isprime() to check if a number is prime or non in python

i.1 Lawmaking

def isprime(num):     for northward in range(2,int(num**0.5)+ane):         if num%n==0:             return False     render True print(isprime(seven)) print(isprime(8))          

This method is implemented using function. It will return True if the number is prime number. Otherwise, it will render False. Start checking with 7 and so with 8.

Output

True Imitation

i.two Lawmaking

def isprime(num):     if num==2 or num==3:         return True     if num%2==0 or num<2:         render Imitation     for northward in range(3,int(num**0.v)+1,2):            if num%northward==0:             return False         return True print(isprime(13)) print(isprime(18))          

This method is implemented using part. It will return Truthful if the number is prime. Otherwise, it volition return Imitation. First checking with 13 and so with eighteen.

Output

Truthful Imitation

i.iii Code

def isprime(num):   if num == 2 or num == 3:       return True   if num < 2 or num%2 == 0:       render False   if num < 9:       return True   if num%3 == 0:       render Fake   a = int(num**0.5)   b = 5   while b <= a:     print ('\t',b)     if num%b == 0:         render False     if num%(b+two) == 0:         return Faux     b=b+6   return Truthful print(isprime(fifteen)) print(isprime(2))          

This method is implemented using function. It will return True if the number is prime number. Otherwise, information technology volition return Fake. Kickoff checking with 15 and then with 2.

Output

False True

one.4 Code

def isprime(num):     if num> 1:           for northward in range(ii,num):               if (num % north) == 0:                   render False         return Truthful     else:         render False print(isprime(64)) print(isprime(5))          

This method is implemented using part. It will render True if the number is prime number. Otherwise, it will render False—first checking with 64 and and so with 5.

Output

Fake Truthful

Method 2: Using if-else statements to check if a number is prime or not

n=int(input("Enter a number:")) if n>1:     for i in range(2,n//2):         if(n%i)==0:             print(n,"is not a prime number")             suspension     else:         impress(n,"is a prime number") else:     print(n,"is neither prime number nor composite")          

This lawmaking is usually using loops. Hither we are getting a number as an input from the user. It performs the code and gives the result to the user. If the user gives one as an input, information technology will display neither prime nor composite.

Output

Enter a number:14 xiv is non a prime number Enter a number:3 3 is a prime number Enter a number:ane 1 is neither prime nor composite

Method three: Using math role to bank check if a number is prime or not

Math is a module that is already available in the python library. This module contains a lot of mathematical functions. To access this module, we have to import the module as:

import math

Hither we are using math.sqrt to bank check if the number is prime or not. sqrt() is a born function in python.

Syntax

math.sqrt(x)

Parameter

x – that can be any value.

Returns

It returns the square root of the x value.

Code

import math def isprime(num):     a=ii     while a<=math.sqrt(num):         if num%a<ane:             return False         a=a+ane     return num>1 print(isprime(14)) print(isprime(7))          

Output

Simulated True

Method 4: Using sympy module to check if a number is prime or not

Sympy is a module in the python library. It simply depends on mpmath. Here nosotros are simply using a sympy module. The pip command line to install the module is:

pip install sympy

Syntax

sympy.isprime(x)

Parameter

x – it is an input value

Returns

Boolean values

4.1 Code

import sympy print(sympy.isprime(90))          

Output

False

4.2 Code

from sympy import * impress(isprime(nineteen))          

Output

True

four.3 Code

import sympy.ntheory as nt print(nt.isprime(8))          

Output

False

Method v: Using primePy library to check if a number is prime number or not

The primePy is a library that is useful to perform the operations regarding prime numbers. Here we are using primePy to bank check whether a number is prime number or not. The pip control to install the primePy module:

pip install primePy

Syntax

primePy.check(due north)

Parameter

north – Information technology is an input number

Returns

Boolean values

Code

from primePy import primes print(primes.check(63))          

Output

False

Method vi: Using is_integer function to bank check if a number is prime or not

is_integer is a built-in function that is useful tos bank check if the given number is an integer or not. It is also useful to check if it is prime or not.

Syntax

float.is_integer()

Parameter

floating number

Returns

Boolean values (True or Faux)

Code

def prime(num):     a=[]     for i in range (one, num+1):         if (num/i).is_integer():             a.suspend(i)     if len(a)==2:         print("Prime")     else:         print("Not Prime") prime number(two)          

Output

Prime

Learn Something New: How to generate a random prime number?

import random def range_primes(a, b):     prime = []     for i in range(a, b):         is_prime = Truthful         for n in range(2, i):             if i % northward == 0:                 is_prime = Imitation         if is_prime:             prime.append(i)     render prime number prime= range_primes(1,100) random_prime = random.pick(prime number) impress("Random Prime Number is:", random_prime)          

Output

Random Prime number is: xi

1. What is a prime?

Prime number numbers are the numbers that are non the product of any other numbers. These numbers are ever natural numbers.

2. How to check if the number is prime or not using loops?

To check if a number is prime or non. We have to create a for loop to iterate the numbers. Suppose the number is greater than one. It will check whether a number is a product of any number. If it is, it displays False as a result.

Determination

Here we have briefly learned near how to check if a number is prime number or not. We have learned many possible ways. With that, nosotros likewise saw how to generate a prime number number. We hope this article is helpful. Endeavour to solve the programs on your own to proceeds more cognition.

Source: https://www.pythonpool.com/check-if-number-is-prime-in-python/

Posted by: beasleyluxual1965.blogspot.com

0 Response to "How To Find Prime Numbers In Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel