After finishing this tutorial, you will know:

- What are the prime numbers?
- Several Python methods for obtaining prime numbers.
- Python algorithm optimization for obtaining prime numbers.
- Using Python, find all prime numbers inside a specified range of values.

## What are Prime Numbers?

In python, Prime numbers are numbers of int type that are greater than 1 and do not have any other factors but themselves and 1. A prime number is one like 5, while a non-prime number is four (since 2 x 2 is equal to 4). The numbers 2, 11, 23, and 43 are among the few prime numbers.

## How Prime Numbers can be Generated in Python?

A series of prime numbers can look like 2, 3, 5, 7, 9, 11, 13, and so on. The logic can be implemented in Python to recreate this sequence of prime numbers and determine whether any given number is prime or not. Using Python libraries, loops, conditions, and the lambda function are a few techniques for carrying out this task.

There are various approaches to implementing prime numbers in Python; we explained a few in the examples below.

## Example # 1: Generating Sequence of Prime Numbers Using Lambda Function

Prime numbers can be generated using the lambda function as can be seen in the example below.

**Code**:

for i in range(2,num):

if prime(i) == True:

print(i,end = " ")

else:

pass

num = int(input("Enter the range: "))

prime = lambda num: all( num%x != 0 for x in range(2, int(num**.5)+1) )

Prime_seq(num)

**Output**:

We input key value 30 so the function has printed all prime numbers from 2 up to 30. Using a lambda function which is an orphan function or an anonymous function is used in the program above to determine the range of prime numbers. We designed the program in such a way that when we execute the script, the program will ask the user to input a key value. Then, the program will generate the sequence or series of prime numbers starting from 2 up to the specified key value.

## The flow of the program:

The user enters the input range and the python input method is used to take that input. The input is explicitly transformed into the numeric datatype int. In the process of invoking the function, the cast int is supplied as an argument. The lambda function is invoked by the defined function. As a result, the lambda function is invoked for every number or integer value from 2 to a specified key value and a prime check is performed. To do this prime check, the logic number%2! = 0 is used.

## Example # 2: Generating Prime Numbers Using for Loop

We saw how to get a sequence of prime numbers using the lambda function. Now, we will use the for loop and a condition to generate a sequence of prime numbers.

**Code**:

for i in range(2, num):

for x in range(2, i):

if (i%x==0):

break

else:

print(i)

**Output**:

Our program has obtained a series of prime numbers from 2 to 13 successfully as we entered value 15. This program uses for loops and conditions to get a series of prime numbers. It executes in such a way that, after the user enters a specific integer, all the prime numbers between the range starting from 2 to the specified input number, will be generated and printed.

## The flow of the program:

The user enters the input range and the input() function takes that input. The specified input is then explicitly turned into an INT type. In this example, two integers are determined via nested looping; the first loop makes sure to obtain all the numbers or integers lying within the keyed range. The prime number verification is determined by the second for-loop. The integer handled is displayed/printed to the console if the process’s outcome for each integer value denominated is not 0. Every number in the specified range of two to input value is subjected to a loop that repeats this process.

## Example # 3: Generating Prime Numbers Using the While Loops

Now, we will use while loops for obtaining prime numbers under a specified range.

**Code**:

num = 1

while(num <= range):

count = 0

x = 2

while(x <= num//2):

if(num % x == 0):

count = count + 1

break

x = x + 1

if (count == 0 and num!= 1):

print(" %d" %num, end = ' ')

num = num + 1

**Output**:

We specified the input value of 45 and the program extracted all the prime numbers up to the number ‘45’. In the code above, the while loops and conditions are used to determine the range of prime numbers. Once the user enters an integer, all the prime numbers falling between the range of 2 and the specified input are generated and displayed.

## The flow of the program:

The user enters the input range and the python input method is used to take that input. The input was then cast into datatype int. Here, the prime check is performed using a while loop. The loop control is based on a conditional check and the cycling process will continue until the entered value exceeds the loop control variable. The variable for loop control is initialized with 1 and increases by 1 after each loop. Again, if the outcome from the process for each integer value denominated is not zero, the integer handled will be printed. For each integer value from 1 to the specified key value, this process will be repeated and executed.

## Example # 4: Generating Prime Numbers Using the Sympy Library in Python

Using the sympy.primerange() function in the sympy library, we can obtain a list containing prime numbers within a specified range using this function.

**Code**:

l_limit = int(input( "lower limit : " ))

h_limit = int(input( "higher limit : " ))

prime_numbers = list(sympy.primerange(l_limit,h_limit+1))

print("Prime Numbers : " , prime_numbers)

**Output**:

This program used a predefined library to find the range of prime numbers. In this example, the sympy module is used to perform the prime check and the function has generated a list with prime numbers that are lying within the lower and higher limits, specified by us.

## The flow of the program:

The user enters the lower and higher range. The user’s input is then processed by the Python input function. The input that was obtained is cast into an INT type. The primerange() method of the sympy module takes the lower and higher range values as input. The function’s output is printed after being cast into a list variable.

## Verifying Prime Numbers

We have seen different approaches to generating prime numbers. Now, you should also be able to verify if a number is prime or not. For this, you can check the program given below. The simplest and most basic approach is to iterate through the range of positive integers from Two to the input number and check if the range and modulo of the number are equal to zero. If that happens, the number isn’t a prime number because it has a divisor other than ‘one’ and the number itself.

**Code**:

if n > 1:

for i in range(2,n):

if n % i == 0:

return False

return True

return False

print(checkprime(13))

print(checkprime(12))

Output:

For number 13, the function has returned true and false for number 12. We defined the function ‘checkprime’, which only accepts a single integer as an argument because prime numbers must be larger than 1, if the input number is not greater than one, the function returns False. Then it iterates over the range from two to the specified number. The function will return False if the modulo (%) of the entered number ‘num’ and the iteration ‘i’ is equal to zero. If not, the method returns True.

## Conclusion

In this tutorial, we first saw the introduction to prime numbers and then discussed how we can generate random numbers in python. We used different approaches for generating a series/range of prime numbers. First, we used the lambda function, secondly, we used for loops and conditions, then we used the while loop, and finally, we used the primerange() method of the sympy module to generate random numbers within a specified range. We also implemented one example to verify if a number is prime or not.