The divisor is the number by which the divisible is divided by an integer. A divisor can have one or more divisors, you can find them all with a simple algorithm that is easily implemented in Python 3.
Finding the divisors of a number
From a practical point of view, it would be useful if the Python program could not only find the divisors of a number, look for their sum, determine the minimum and maximum, as well as the prime divisors. Each subtask is somehow related to the previous one, so the code of the subsequent program is a slightly upgraded code of the previous one. In addition, all functionality can be combined in one program, if necessary.
The user enters an integer whose divisors the program will look for, then the code looks like this
numb = int(input("Enter an integer: ")) print("Result:", end = " ") for i in range(numb - 1, 1, -1): if (numb % i == 0): print(i, end = " ")
For example, a user has entered the number 625. The program starts the loop with the value 624, the loop checks if 625 is divisible by 624, and then it goes to the next iteration and works with the number 623 and so on to two. Thus, the output of the program will be as follows:
Enter an integer: 625 Result: 125 25 5
Simple divisors of numbers
A simple divisor is a divisor that is only divisible by one and itself. To find simple divisors using Python, you need to modernize your program a bit by adding an additional for loop and a counter variable. The program is based on the following algorithm:
- Zero the counter.
- In the loop look for divisors.
- If found, look for its divisors in the nested loop. This is to determine if it is prime.
- If found, increase the counter.
- If the counter is zero, then the number is prime and the value of the divisor should be displayed in the console.
- Go to the next iteration of the outer loop.
The loop now looks like this:
numb = int(input("Enter an integer: ")) print("Simple:", end = " ") for i in range(numb - 1, 1, -1): is_simple = 0 # Counter if (numb % i == 0): for j in range(i - 1, 1, -1): if (i % j == 0): is_simple = is_simple + 1 # Increase if we find a divisor if (is_simple == 0): # if no divisors were found, print print(i, end = " ")
It is clear that if the counter value is greater than zero, the number is definitely not prime. We can optimize the code a bit and immediately terminate the nested loop when the counter reaches zero. To do this, you can use the
break operator in the conditional operator in the nested loop. The result of the program:
Enter an integer: 63 Simple: 7 3
The divisors are in descending order. And if you want to output only the largest prime divisor using Python, you can use the
break statement to exit the loop after the first number is output.
Sum of divisors
In order to find the sum of all divisors of a number using Python, just add a variable to the initial program, to which each found divisor will be added in the loop. Program code:
numb = int(input("Enter an integer: ")) sum_of_dividers = 0 for i in range(numb - 1, 1, -1): if (numb % i == 0): sum_of_dividers += i print("Sum:", sum_of_dividers)
Enter an integer: 63 Sum: 40
Number of dividers
This version of the program also differs only slightly from the original one. To count the divisors, we need to enter a counter variable to which we will add one every time the “
numb % i == 0” condition is met.
numb = int(input("Enter an integer: ")) count_of_dividers = 0 for i in range(numb - 1, 1, -1): if (numb % i == 0): count_of_dividers += 1 print("The number is equal to:", count_of_dividers)
Enter an integer: 63 Count is equal to: 4
Maximum and minimum divisor
To find the minimum and maximum divisor, you need to add two variables to your Python code:
max_divider. In the loop, the divisor will be compared to the value of these variables and, if necessary, written to them. Program code:
numb = int(input("Enter an integer: ")) min_divider = numb max_divider = 1 for i in range(numb - 1, 1, -1): if (numb % i == 0): if (min_divider > i): min_divider = i if (max_divider < i): max_divider = i print("Minimal is equal to:", min_divider) print("Maximum equals:", max_divider)
Enter an integer: 63 Minimum equals: 3 The maximum equals: 21