Can Fermat's Factorization Method Be used in any way to get the largest prime factor for a given number?

1.2k Views Asked by At

I have given a shot at trying to find the largest prime number for a given number, and thought of using Fermat's Factorization Method. I might be sitting the pot miss and I think I am going about this the wrong way (or I am making shallow assumptions again).

Let me start with a small example:

Latex of example

Will it be safe to assume that 17 is the largest prime for 255? if not so, are there any other methods I can use to get the largest prime factor for any given number > 2?

1

There are 1 best solutions below

0
On

I Resorted to programming a C# Solution and came to the conclusion that Fermat's Factorization is good at Finding Factors, not primes, for a given number ( but at times, a + b or a - b may be the largest prime by coincidence). The pseudo logic for the solution followed:

Step 1 : Set up 3 variables: A divisor, a recorder and the number which's highest prime that is in question (255 in my case), N
step 2 : while N is not equal to 1
 step 2.1 : Check if N modulus divisor isn't 0 (increment the divisor if this condition is met)
 step 2.2 : If N modulus divisor = 0, divide the integer with the divisor. 
 step 2.3 : If the Divisor > Recorder, set the recorder = divisor

return the recorder

Now, I am not sure whether I may post my programmatic solution (this is math.stackexchange, not stackoverflow), and will remove it if necessary.

    private object Largest_Prime_Factor()
    {
        var divisor = 2;
        var integer = 12321;
        var highest = 0;

        while (integer != 1)
        {
            if (integer % divisor == 0)
            {
                integer /= divisor;

                if (divisor > highest)
                {
                    highest = divisor;
                }

                divisor = 2;
            }
            else
            {
                divisor++;
            }
        }
        return highest;
}