본문 바로가기
Programming/Project Euler

3. Project Euler #3 : Find the biggest prime factor.

by NoxLuna 2015. 1. 20.

This problem is making a program to find the largest prime factor for a given number.

In fact, no choice but to use the body of elastomer Ness Te find the largest prime factor.

It is one that is required is to continue to divide a given number. Is beyond the scope of this program as well.

The general form for a small number of bit to speed, all except 2 is an odd prime number.

Is an odd prime factor is to reduce the number of attempts and the like has a 6k + 1, 6k + 5 is.

Well, not like this time by a more efficient method for finding algorithm.

Simply squeeze the program look as follows:

int main()
{
        int p = 3;
        int64 n = 600851475143;

        while( n%2 == 0 ) n /= 2;
        if( n == 1 ) { printf("Ans = 2\n"); return 0; }
        while( 1 )
        {
                while( n%p == 0 ) n /= p;
                if( n == 1 ) break;
                p += 2;
        }
        printf("Ans = %d\n", p);
        return 0;
}

댓글