Problem no 14: Largest prime Factor.
QUESTION:
Given a number N, the task is to find the largest prime factor of that number.
Example 1:
Input:
N = 5
Output:
5
Explanation:
5 has 1 prime factor
i.e 5 only.
Example 2:
Input:
N = 24
Output:
3
Explanation:
24 has 2 prime factors
3 and 2 in which 3 is
greater
ANSWER:
long long int largestPrimeFactor(int n){
int num=n;
while(true)
{
if(n%num==0)
{
int yes= isPrime(num); ..calling isPrime func.
if(yes==1)
{
return num;
}
}
num--;
}
}
int isPrime(int n){
if(n==1)
{
return 0;
}
else
{
for(int i=2;i<=n/2;i++)
{
if(n%i==0)
{
return 0;
}
}
return 1;
}
}
Comments
Post a Comment