Find the largest divisor using Python:
In this tutorial, we will learn how to find the largest divisor of a number in Python. The largest divisor of a number is the largest number that can divide it. It should not be the number itself. For example, for number 10, it can be divided by 1,2,5, and 10. So, the largest divisor is 5.
The program will ask the user to enter a number. It will use one loop starting from 2 to check if any number can divide the user input number or not. If any number is found, it will be assigned to a variable. At the end of the loop, this variable will hold the largest divisor of the number.
Let’s take a look at the program to understand how it works:
Method 1: Python program to find the largest divisor using a for
loop:
The following Python
program shows how to find the largest divisor of a number using a for
loop. We are using Python-3 in this example:
#1
num = int(input("Enter a number: "))
largest_divisor = 0
#2
for i in range(2, num):
#3
if num % i == 0:
#4
largest_divisor = i
#5
print("Largest divisor of {} is {}".format(num,largest_divisor))
Explanation:
The commented numbers in the above program denote the step numbers below:
-
Ask the user to enter a number. Read the number as an integer using the
int()
function and assign it to thenum
variable. Also, create one more variablelargest_divisor
to assign the largest divisor for the user input number. -
Run one
for
loop from 2 to the user input number. -
For each number in the loop, check if it can divide the user input number or not.
-
If the number can divide the user input number, assign it to
largest_divisor
variable. -
After the
for
loop ends, thelargest_divisor
variable will hold the largest divisor for the user input number. Print it out.
Sample output:
If you run the above program, it will print outputs as below:
Enter a number : 50
Largest divisor of 50 is 25
Enter a number : 112
Largest divisor of 112 is 56
Enter a number : 10
Largest divisor of 10 is 5
Enter a number : 50
Largest divisor of 50 is 25
Download it on Github
Method 2: By iterating the loop up to half of the number:
We can optimize the above program by iterating up to half of the given number. It will reduce the iterating steps and it runs faster than the previous example.
num = int(input("Enter a number: "))
largest_divisor = 0
for i in range(2, int(num/2+1)):
if num % i == 0:
largest_divisor = i
print("Largest divisor of {} is {}".format(num,largest_divisor))
If you run the above program, it will give a similar output.
Download it on Github
Method 3: Using next()
:
The next()
method returns the next item from an iterator. We can use it to get the first element of a list of all divisors of a number:
num = int(input("Enter a number: "))
largest_divisor = next(i for i in range(int(num / 2 + 1), 0, -1) if num % i == 0)
print("Largest divisor of {} is {}".format(num, largest_divisor))
It makes the code concise.
Download it on Github
Conclusion:
We have learned how to find out the largest divisor of a number in Python. We are using one for
loop and the next()
method to find out the largest divisor. But you can also use one while
loop to write the same program. Please raise a PR on Github if you have any other way in mind.
Similar tutorials :
- Python program to find the largest even and odd numbers in a list
- Python 3 program to convert a decimal number to ternary (base 3)
- Python tutorial to calculate the sum of two string numbers
- Find out the multiplication of two numbers in Python
- Write a python program to reverse a number
- Python program to find the smallest divisor of a number