
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
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.
Algorithm
getMaxPrimeFactors(n)
begin while n is divisible by 2, do max := 2 n := n / 2 done for i := 3 to √𝑛, 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; while(n % 2 == 0) { max = 2; n = n/2; //reduce n by dividing this by 2 } 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 > 2) { max = n; } return max; } main() { int n; printf("Enter a number: "); scanf("%d", &n); printf("Max prime factor: %d", getMaxPrimeFactor(n)); }
Output
Enter a number: 24024 Max prime factor: 13
- Related Articles
- C Program for Find the largest prime factor of a number?
- Python Program for Find largest prime factor of a number
- Find largest prime factor of a number using C++.
- Java Program to find largest prime factor of a number
- Finding the largest prime factor of a number in JavaScript
- Find sum of a number and its maximum prime factor in C++
- Prime Factor in C++ Program
- Prime factor array of a Number in JavaScript
- Largest number with prime digits in C++
- Recursive program for prime number in C++
- C/C++ Program to find Product of unique prime factors of a number?
- C Program for efficiently print all prime factors of a given number?
- Find the difference between the smallest 3 digit prime number and largest 1digit prime number
- k-th prime factor of a given number in java
- C/C++ Program to find the Product of unique prime factors of a number?
