0
点赞
收藏
分享

微信扫一扫

Problem 3 Largest prime factor (分解素因子)


Largest prime factor

Problem 3

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

What is the largest prime factor of the number 600851475143 ?


Answer:

6857

题解:

Let the given number be n and let k = 2, 3, 4, 5, ... . For each k, if it is a factor of n then we divide n by k and completely divide out each k before moving to the next k. It can be seen that when k is a factor it will necessarily be prime, as all smaller factors have been removed, andthe final result of this process will be n = 1.

代码:


#include <bits/stdc++.h>
using namespace std;
long long num = 600851475143ll;
int main()
{
long long i, max = 1;
for(i = 2; i * i <= num; i++)
{
if(num % i == 0)
{
max = i;
while(num % i == 0) num /= i;
}
if(num > 1) max = num;
}
cout<<max<<endl;
return 0;
}



举报

相关推荐

0 条评论