Program to Find Smallest Prime Divisor of a Number

Python Program to Find Smallest Prime Divisor of a Number

Prime Divisor:

The prime divisor of a polynomial is a non-constant integer that is divisible by the prime and is known as the prime divisor.

Some prime divisors are 2 , 3 , 5 ,7 , 11 ,13 ,17 ,19 and 23 etc.

Divisors can be both positive and negative in character. A divisor is an integer and its negation.

Given a number, the task is to print the smallest divisor of the given number in Python.

Examples:

Example1:

Input:

Given number =91

Output:

The smallest prime divisor the number [ 91 ] is [ 7 ]

Example2:

Input:

Given number =240

Output:

The smallest prime divisor the number [ 240 ] is [ 2 ]

Program to Find Smallest Prime Divisor of a Number in Python

Below are the ways to print the smallest divisor of the given number in Python.

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take an empty list and initialize it’s with an empty list using list() and [].
  • Loop from 2 to the given number using the For loop.
  • Check if the iterator value divides the given number using the % operator and the If statement.
  • If It is true then append it to the list.
  • Print the first element of the list using the index 0.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
numb = 91
# Take an empty list and initialize it's with an empty list using list() and [].
nwlist = []
# Loop from 2 to the given number using the For loop.
for m in range(2, numb+1):
        # Check if the iterator value divides the given number
    # using the % operator and the If statement.
    if(numb % m == 0):
        # If It is true then append it to the list.
        nwlist.append(m)
smalldivisor = nwlist[0]
# Print the first element of the list using the index 0.
print(
    'The smallest prime divisor the number [', numb, '] is [', smalldivisor, ']')

Output:

The smallest prime divisor the number [ 91 ] is [ 7 ]

Method #2: Using For Loop (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take an empty list and initialize it’s with an empty list using list() and [].
  • Loop from 2 to the given number using the For loop.
  • Check if the iterator value divides the given number using the % operator and the If statement.
  • If It is true then append it to the list.
  • Print the first element of the list using the index 0.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
numb = int(input('Enter some random number ='))
# Take an empty list and initialize it's with an empty list using list() and [].
nwlist = []
# Loop from 2 to the given number using the For loop.
for m in range(2, numb+1):
        # Check if the iterator value divides the given number
    # using the % operator and the If statement.
    if(numb % m == 0):
        # If It is true then append it to the list.
        nwlist.append(m)
smalldivisor = nwlist[0]
# Print the first element of the list using the index 0.
print(
    'The smallest prime divisor the number [', numb, '] is [', smalldivisor, ']')

Output:

Enter some random number =240
The smallest prime divisor the number [ 240 ] is [ 2 ]

Related Programs: