求600851475143最大的质因数是多少?
举例24 = 4 X 6= 2 X 2 X 2 X 3,即2,3为质因数,3最大,可利用24不断除去小的质因数,最后剩下最大质因数,即
( ( 24/2 )/2 )/2 = 3;
结果:6857
#include<iostream>
using namespace std;int main()
{long long temp,res;int i=2;cin>>temp;while(temp>2){if(temp%i==0){res = i;temp = temp/i;}else{i++;}}cout<<res<<endl; return 0;}
更新一下做法吧:
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
#define N 600851475143LLint main() {long long num = N, i = 2;long long ans;while (i * i <= num) {if(num % i == 0) ans = i; //ans记录最大的素因子while(num % i == 0) num /= i;i++;}if(num != 1) ans = num; //一定是素数cout<<ans<<endl;return 0;
}