C Program for Find largest prime factor of a number?

In this section, we will see how we can get the largest prime factor of a number in an efficient way. There is a number say n = 1092, we have to get the largest prime factor of this. The prime factors of 1092 are 2, 2, 3, 7, 13. So the largest is 13. To solve this problem, we have to follow this rule −

  • When the number is divisible by 2, then store 2 as largest, and divide the number by 2 repeatedly.

  • Now the number must be odd. Now starting from 3 to square root of the number, if the number is divisible by current value, then store factor as largest, and change the number by divide it with the current number then continue.

  • And finally if the number is greater than 2, then it is not 1, so get the max prime factor.

Let us see the algorithm to get a better idea.

Syntax

int getMaxPrimeFactor(int n);

Algorithm

getMaxPrimeFactors(n)

begin
   while n is divisible by 2, do
      max := 2
   n := n / 2
   done
   for i := 3 to ?n, increase i by 2, do
      while n is divisible by i, do
         max := i
         n := n / i
      done
   done
   if n > 2, then
      max := n
   end if
end

Example

#include <stdio.h>
#include <math.h>

int getMaxPrimeFactor(int n) {
    int i, max = -1;
    
    /* Handle factor 2 */
    while(n % 2 == 0) {
        max = 2;
        n = n/2; /* reduce n by dividing this by 2 */
    }
    
    /* Handle odd factors from 3 onwards */
    for(i = 3; i <= sqrt(n); i = i + 2) { /* i will increase by 2, to get only odd numbers */
        while(n % i == 0) {
            max = i;
            n = n/i;
        }
    }
    
    /* If n is still greater than 2, then it's a prime */
    if(n > 2) {
        max = n;
    }
    
    return max;
}

int main() {
    int n = 24024;
    printf("Number: %d<br>", n);
    printf("Max prime factor: %d<br>", getMaxPrimeFactor(n));
    
    /* Test with another number */
    n = 1092;
    printf("Number: %d<br>", n);
    printf("Max prime factor: %d<br>", getMaxPrimeFactor(n));
    
    return 0;
}

Output

Number: 24024
Max prime factor: 13
Number: 1092
Max prime factor: 13

How It Works

The algorithm works by dividing the number by its smallest prime factors first. It handles 2 separately, then checks odd numbers starting from 3. When we find a factor, we divide the number by that factor repeatedly until it's no longer divisible. The last factor found will be the largest prime factor.

Time Complexity

The time complexity is O(?n) because we only need to check divisors up to the square root of the number. The space complexity is O(1) as we use only a constant amount of extra space.

Conclusion

Finding the largest prime factor efficiently requires checking divisibility up to the square root of the number. This approach eliminates smaller factors first and ensures the remaining number (if greater than 2) is the largest prime factor.

Updated on: 2026-03-15T11:03:20+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements