Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

largest prime factor

okay i need some help making a function that finds the largest prime factor of a number

so far i have a function that finds the primes of a number and a function that finds the factors of a number

bool isPrime(int a)
{


for (int i = 2; i <=(a-1); i++)

if (a % i == 0)

return false;


else
return true;

}

below is what i used to find the factors...not yet a function but i can easily implement it to a function

for (char i = 1; i <= number; i++)
{
if (number % i ==0 )
{
cout<< i<<endl;

}

}

so can someone help me cram these two together making one fucntion that finds the biggest prime factor of a number?

thanks
[958 byte] By [lonewolf_860] at [2007-11-11 8:36:02]
# 1 Re: largest prime factor
the brute force way:

for(factor = n; factor > 0; factor--)
{
if(n % factor == 0) //haha if n is prime, and the first time is n, we catch it
if(isprime(factor))
//this is the one, stop, print, and get on with your life.
}

there are ways to do a lot less loop iterations. but this is the idea.
jonnin at 2007-11-11 21:01:14 >
# 2 Re: largest prime factor
Hi :

I will rewrite the entire code :

for(int i=n; i>0; i--)
{
if( n % i == 0 )
{
if( isprime(i) )
{
cout << "result : "<< i << endl;
break;
}
}
}
Comete32 at 2007-11-11 21:02:19 >
# 3 Re: largest prime factor
That didnt make a lot of sense. Did you get it to work or not?
jonnin at 2007-11-11 21:03:20 >