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

Popular posts from this blog

problem 3: given two integers N and M. The problem is to find the number closest to N and divisible by M. If there are more than one such number, then output the one having maximum absolute value.

problem no 7:Given two numbers A and B, find Kth digit from right of AB.

Problem no 16: count the number of squares below N.