Monday, March 13, 2017

One of the common programming kata to learn coding is write a program to find the largest prime factor of a number. Like any other programming problem, you need to build the logic to solve this problem. Before solving the problem, let's revise the concept of number theory. Prime factors of a positive integer are the prime numbers that divide the number exactly i.e. without any remainder, and prime factorization is the process of finding all prime numbers when multiply together make original number. A positive integer can have multiple prime factors, our challenge is to find the largest prime factor of a number. For example, 6 has two prime factors 2 and 3, your program should return 3, because that's the largest prime factors. In one of earlier problem, we have learned how to find prime factor of a number in Java and we will use the similar logic here, but instead of returning a list of prime factors we will only return the largest prime factor.

Java Program to Find Largest Prime Factor of an Integer

Problem : Write a program to find the largest prime factor of a positive integer in Java. If we pass 15 to your program, it should return 5, and if we pass 6 to your program it should return 3.

Solution : As we learned a number is called prime factor if it is prime number and it can divide the number exactly. Another property of prime factor is that if we keep dividing the number by prime factor then it will either fully divide the number or produce another prime factor e.g. if we need to find the prime factors of 16 then starting from 2 if keep dividing, eventually dividend become 1, so 2 is the only prime factor of 16. On other hand if need to find prime factor of 15, then we first try to divide it by 2, but since its not divisible by 2, we move to next number which is 3. Since 3 can divide 15, it produces another prime number 5, now 5 is not divisible by anything other than 5, so 3 and 5 become prime factor of 15.

Algorithm : In our program, we have used the same logic. We start with 2, the smallest prime number and try to divide the number, if number is divisible then we keep dividing it by same number until its not divisible any more. Now we move to next number, the largest number which is able to fully divide the input is our largest prime factor. This would be more clear when you see the actual program.

and here is the test result, you can see the test passed successfully.

One thing to note here is that we are not handling invalid input here e.g. 0, 1 or negative number, which we should if this question is asked on Interview. You can throw IllegalArgumentException for those inputs which are not valid as per problem specification. Similarly, you also need to include unit test to check those invalid inputs. I leave that task for you as practice.

That's all about how to find largest prime factor of a number in Java. This is a really good exercise to learn coding when you are starting with Java or Python or any other programming language. This kind of problem will help to build your programming logic and improve your coding skill. Believe me, its not easy to convert a real life algorithm into program without practice. You must solve some basic coding problem based upon String, array and recursion to get hold of coding. I have shared many such exercise in this blog, if you are interested you can also take a look at following list of problem :