[Project Euler] 3. Largest Prime factor
问题描述:
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
解题思路:
我们可以先从小的数字开始找起,如2
令因数factor为2,然后若当前数字num可以整除2,我们将最大质因数赋给2,同时将num除以2至不能再整除2,factor++
当factor > num 的时候,说明我们已经找完了全部的可能性了
代码:
long long num = 600851475143; long long factor = 2; long long largestF = 1; while(factor <= num){ if(num % factor == 0) largestF = factor; while(num % factor == 0){ num /= factor; } factor++; }