您的位置:首页 > 其它

Project Euler Problem 3

2011-03-10 20:35 393 查看
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)
{
return i;
}
}

return num;
}

int main()
{
long long num = 600851475143;
long factor = 1;
while(num > 1)
{
long f = GetFirstFactor(num);
if(f > factor)
{
factor = f;
}

num = num / f;
}

cout << factor << endl;
cin.get();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: