One of the common homework/tasks in programming courses is about Prime Factorization. You are asked to write a program to find prime factors of given integer number. The prime factors of a number are all of the prime numbers that will exactly divide the given number. For example, prime factors of 35 are 7 and 5, both are prime in itself and exactly divides 35. Last time I did this exercise when I was in college, and it was something like, writing a program that asks the user for an integer input and then displays that number's prime factorization on the command line. There are variants of this program as well e.g. look at this exercise, Write a program that prompts the user to enter a positive integer and displays all its smallest factors in decreasing order. It's more or less same as the earlier mentioned version of prime factorization problem, but with a catch of displaying them in decreasing order.
Displaying is not a problem at all, you can display it in command prompt or a GUI easily, the main thing is to write logic to find prime factors, and this is what you will learn in this programming tutorial.

Remember, we can not use an API method, which directly solves the problem e.g. you are not allowed to use reverse method of StringBuffer, in order to reverse a String in Java. You need to write the core logic of prime factorization by using primitive programming constructs e.g. control statements, loops, arithmetic operator etc. Though you can use essential function e.g. length() to calculate the length of String or toArray() to convert String to array etc.

Java Program to Find Prime Factors

Without delaying any further here is our complete Java program to find prime factors. Logic of calculating prime factors are written inside method primeFactors(long number), it's a simple brute-force logic to find prime factors.

We start from 2, because that's the first prime number and every number is also divisible by 1, then we iterate until we find a prime factor by incrementing and stepping one at a time. When we find a prime factor, we store it inside a Set and also reduce the number till which we are looping.

In order to run this program, you can simply copy paste it in a file PrimeFactors.java and then compile and run by using javac and java command. If you find any difficulty running this program, you can also refer this article for step by step guide on how to run a Java program from command prompt.

If you are curious about what is that angle bracket <>, its diamond operator introduced in Java 7 for better type inference. Now you don't need to write type parameters in both side of expression, as you have to do in Java 1.6, this makes them more readable. Now coming back to exercise, if you look at the output, it only returns the unique prime factors because we are using Set interface, which doesn't allow duplicates.

If your Interviewer ask you to write program to divide a number into its prime factors, and print all of them, then you need to use List interface instead of Set. For example, unique prime factors of '72' are [2,3] but the number in terms of its prime factor is [2, 2, 2, 3, 3]. If you need that kind of output, you can rewrite our primeFactors(long number) method to return a List<Integer>, as shown below :

and, here is the output of running same program with this version of primeFactors(long number) method. This time you can see all the prime factors instead of just the unique ones. This also explains difference between Set and List interface, a very important lesson for beginners.

Now, its time to practice writing some JUnit tests. Actually there are two ways to test your code, one is by writing main method, calling method and comparing actual output to expected output by yourself. Other, much more advanced and preferred approach is to use unit test framework like JUnit to do that.

If you follow test driven development, than you can even write test before writing code and let test drive your design and coding. Let's see how our program fares with some JUnit testing.

In our test class, PrimeFactorsTest we have five test cases to test corner cases, single prime factor cases, and multiple prime factor cases. We have also created a utility method list(int... ints) which take advantage of Java 5 varargs to return List of given numbers. You can call this method with any number of arguments including zero, in which case it will return an empty List. If you like, you can extend our test class to add few more tests e.g. performance test, or some special case tests to test our prime factorization algorithm.

That's all about how to find prime factors of an Integer number in Java. If you need more practice, you can also check out following 20 programming exercises, ranging from various topics e.g. LinkdList, String, Array, Logic, and Concurrency.

1. How to Swap Two Numbers without using Temp Variable in Java? (Trick)
2. How to check if LinkedList contains loop in Java? (Solution)
3. Write a Program to Check if a number is Power of Two or not? (Answer)
4. How to find middle element of LinkedList in one pass? (See here for Solution)
5. How to check if a number is Prime or not? (Solution)
6. Write a Program to find Fibonacci Series of a Given Number? (Solution)
7. How to check if a number is Armstrong number or not? (Solution)
8. Write a Program to prevent Deadlock in Java? (Click here for solution)
9. Write a Program to solve Producer Consumer Problem in Java. (Solution)
10. How to reverse String in Java without using API methods? (Solution)
11. Write a Program to calculate factorial using recursion in Java? (Click here for solution)
12. How to check if a number is Palindrome or not? (Solution)
13. How to check if Array contains duplicate number or not? (Solution)
14. How to remove duplicates from ArrayList in Java? (Solution)
15. Write a Java Program to See if two String are Anagram of each other? (Solution)
16. How to count occurrences of a character in String? (Solution)
17. How to find first non repeated characters from String in Java? (See here for solution)
18. Write a Program to check if a number is binary in Java? (Solution)
19. How to remove duplicates from array without using Collection API? (Solution)
20. Write a Program to calculate Sum of Digits of a number in Java? (Solution)

9 comments
:

It is also possible to check only till its sqrt as in the program to find whether the number is prime or not.We can get the prime factors greater than sqrt by storing the quotient of the prime number less than its sqrt.for e.g.:if the number is 35.we divide it till 5 & then store its quotient 7, thereby greatly reducing its time complexity.Constructive criticism welcome.

@THE CENTAUR, I agree with you on this point, this will certainly help to reduce time taken to calculate prime factors in case the number is itself a prime number, In other cases do you think it will make any difference, as we are already dividing number with their prime factors?