Ray's playground

 

Project Euler Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

 1 #include <iostream>
 2 using namespace std;
 3 
 4 long long GetFirstFactor(long long num)
 5 {
 6     long long root = sqrt((long double)num);
 7     for(long long i=2; i<root; i++)
 8     {
 9         if(num % i == 0)
10         {
11             return i;
12         }
13     }
14 
15     return num;
16 }
17 
18 int main()
19 {
20     long long num = 600851475143;
21     long factor = 1;
22     while(num > 1)
23     {
24         long f = GetFirstFactor(num);
25         if(f > factor)
26         {
27             factor = f;
28         }
29 
30         num = num / f;
31     }
32 
33     cout << factor << endl;
34     cin.get();
35 }

posted on 2011-03-10 20:35  Ray Z  阅读(171)  评论(0编辑  收藏  举报

导航