找最大质因子问题

题目描述,例如13195的质因子有5,7,13,29,其中29是最大质因子,现在给你一个数600,851,475,143,,让你求它的最大质因子。

这个数记为number,在2-sqrt(number)内先找出质因子然后让number除这些比较小的质因子不断的变小最后剩下的number%i==0的number就是要求的maxnumber(这个number本身是质数的情况要单独讨论下)

以下是AC代码

 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<cmath>
 5 using namespace std;
 6 typedef __int64 LL;
 7 int main(){
 8     LL number,limit,maxnumber;
 9     cin>>number;
10     maxnumber=0;
11     limit=(LL)sqrt(number);
12     for(int i=2;i<=limit;i++)
13         if(number%i==0){
14             maxnumber=i;
15             while(number%i==0){
16                   number/=i;
17             }    
18         }
19     if(maxnumber=0)
20      maxnumber=number;
21     cout<<maxnumber;
22     return 0;
23 }
原文地址:https://www.cnblogs.com/acplayfacm/p/3863874.html