Factors of a Number in Java

In this article, we explore various methods in Java to find all factors of a given number. Factors are numbers that divide the original number without leaving a remainder. We'll use loops to iterate from 1 to the number, identifying and listing or printing each factor. This guide provides a comprehensive approach to understanding factor determination in Java programming.
Factors of a Number Using Loop
Our first approach to finding the factors of a number is using the for loop. We run a for loop from 1 to the number n, which we need to find the factors of. Then, we check for those numbers which perfectly divide n, and hence obtain the factors.
Code:
Output:
The same can be done using while or do-while loop. The logic remains the same for both, only syntax changes.
Factors of a Number Using while loop
Factors of a Number Using do-while loop
Output for the above codes remains the same.
The above-discussed approaches are naive to find all the factors of a number. We are simply iterating from to , checking if that number divides , and printing it. The time complexity of the approach is , and auxiliary space required is .
So, can we improve the above approach? The answer is yes. We will now discuss a better approach for finding factors of a number.
Another Approach
If we carefully observe all the factors of a number, then we can say they are present in pairs.
Example: For n = 100, the various pairs of divisors are: (1,100), (2,50), (4,25), (5,20), (10,10). Similarly, for n = 40, the various pairs of divisors are: (1,40), (2,20), (4,10), (5,8).
This remains true for all other numbers, and we can use this fact to devise a better solution. We only need to iterate to the number's square root, which will give us all the pairs. It is important to note that for equal pairs (like (10,10)), the number is considered only once.
Below is the implementation for the same.
Output
Time Complexity: O(sqrt(n))
Auxiliary Space : O(1)
The solution gives all the factors of the number, but it can still be improved since all the factors are not sorted. We can modify the code in our favor so that it prints all factors in sorted order.
For that, we will first print half of all the pairs since they will be sorted. This will give us the numbers which divide the number completely, and then we can print the quotient for each number to get other half.
Code:
Output:
Here also, time complexity remains the same O(sqrt(n)).
Factors of Negative Number
To find factors of a negative number, we can calculate all of its positive factors, then duplicate them and write a negative sign in front of the duplicates. We can also do this by traversing from to to print all factors. We have to exclude since it will give an error.
Code:
Output:
Conclusion
Finally, let's summarise everything we have learned in the article:
- Numbers that divide the given number completely, leaving remainder, are called factors of the given number.
- In programming, we use loops such as for, while, and do while to find the factors of a number by simply iterating over all the numbers from to to check factors.
- The time complexity to calculate all factors by traversing to is .
- The factors of the number do occur in pairs. Thus time complexity to calculate all the factors can be improvised to .