How to find all the possible proper divisor of an integer in Python3
In this tutorial, we will learn how to find all the possible divisors of an integer in Python. This problem uses a very basic mathematical concept and basic python.
By the time you will be done reading this post, you will be through with both of the concepts and will easily learn to code the concept as well.
Find DIVISORS of an integer in Python
This problem is based on simple mathematical division. Let us begin :
If a number divides the given number completely leaving the remainder to be 0(zero) then it is said to be the positive proper divisor of that integer(excluding that number) and if we include the number too then we will get all the divisors of the number.
Example: 1,2,4 are positive proper divisors of 8 and if we include 8 then we will get all the divisors of 8.
Divisors of an integer in Python
We will first take user input (say N) of the number we want to find divisors. Then we will run a loop from 1 to (N+1). We do so because if we run the loop from 0(zero) then we will get ‘Division By Zero Error’. And if we run it only N times then we will not get the number(N) as its own divisor. Within the loop, we will check the dividing condition. If the remainder after dividing N with X( X is from 1 to N) is 0 then we will print X. When the loop will be over, we will get the answer.
N=(int)(input("Enter Number: ")) for x in range(1,N+1): if(N%x==0): print(x)
INPUT: Enter Number: 5 OUTPUT: 1 5
You may also read: