Program to Find Next Prime Number

Python Program to Find Next Prime Number

In the previous article, we have discussed Python Program to Compute the Perimeter of Trapezoid
Itertools count() function:

The count(start, step) method generates an iterator that is used to generate equally spaced numbers, where the step argument defines the space between them. The start argument specifies the iterator’s starting value, which is set by default to start=0 and step=1.

In the absence of a breaking condition, the count() method will continue counting indefinitely (on a system with infinite memory)

Given a number N, the task is to print the next prime number in Python.

Examples:

Example1:

Input:

Given Number =48

Output:

The Next prime number of { 48 } is: 53

Example2:

Input:

Given Number =75

Output:

The Next prime number of { 75 } is: 79

Program to Find next Prime Number in Python

Below are the ways to print the next prime number in Python:

Method #1: Using For Loop (Static Input)

Approach:

  • Import the count from the itertools module using the import and from keyword.
  • Give the number n as static input and store it in a variable.
  • Loop till the given number using the For loop and the count() function.
  • Inside the For loop, Iterate from 2 to the iterator value of the parent loop using another For loop and range() functions(Nested For loop).
  • Check if the inner loop iterator value divides the parent loop iterator value using the if conditional statement.
  • If it is true then break the inner for loop using the break keyword.
  • else print the iterator value of the parent For loop.
  • Break the parent for loop using the break keyword.
  • The Exit of the Program.

Below is the implementation:

# Import the count from the itertools module using the import and from keyword.
from itertools import count
# Give the number n as static input and store it in a variable.
gvnnumb = 48

# Loop from 1 to given number using the For loop and the count() function.
for m in count(gvnnumb+1):
        # Inside the For loop, Iterate from 2 to the iterator value
    # of the parent loop using another For loop and range() functions(Nested For loop).
    for n in range(2, m):
        # Check if the inner loop iterator value divides
        # the parent loop iterator value using the if conditional statement.
        if(m % n == 0):
            # If it is true then break the inner for loop using the break keyword.
            break
    else:
        # else print the iterator value of the parent For loop.
        print('The Next prime number of {', gvnnumb, '} is:', m)
        # Break the parent for loop using the break keyword.
        break

Output:

The Next prime number of { 48 } is: 53

Method #2: Using For loop (User Input)

Approach:

  • Import the count from the itertools module using the import and from keyword.
  • Give the number n as user input using the int(input()) function and store it in a variable.
  • Loop from 1 to given number using the For loop and the count() function.
  • Inside the For loop, Iterate from 2 to the iterator value of the parent loop using another For loop and range() functions(Nested For loop).
  • Check if the inner loop iterator value divides the parent loop iterator value using the if conditional statement.
  • If it is true then break the inner for loop using the break keyword.
  • else print the iterator value of the parent For loop.
  • Break the parent for loop using the break keyword.
  • The Exit of the Program.

Below is the implementation:

# Import the count from the itertools module using the import and from keyword.
from itertools import count
# Give the number n as user input using the int(input()) function
# and store it in a variable.
gvnnumb = int(input('Enter some random number = '))

# Loop from 1 to given number using the For loop and the count() function.
for m in count(gvnnumb+1):
        # Inside the For loop, Iterate from 2 to the iterator value
    # of the parent loop using another For loop and range() functions(Nested For loop).
    for n in range(2, m):
        # Check if the inner loop iterator value divides
        # the parent loop iterator value using the if conditional statement.
        if(m % n == 0):
            # If it is true then break the inner for loop using the break keyword.
            break
    else:
        # else print the iterator value of the parent For loop.
        print('The Next prime number of {', gvnnumb, '} is:', m)
        # Break the parent for loop using the break keyword.
        break

Output:

Enter some random number = 75
The Next prime number of { 75 } is: 79

Explore more instances related to python concepts from Python Programming Examples Guide and get promoted from beginner to professional programmer level in Python Programming Language.