码迷,mamicode.com
首页 > 其他好文 > 详细

找最大质因子问题

时间:2014-07-23 22:10:47      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   

题目描述,例如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 }

找最大质因子问题,布布扣,bubuko.com

找最大质因子问题

标签:style   blog   color   os   io   for   

原文地址:http://www.cnblogs.com/acplayfacm/p/3863874.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!