欧拉计划3 (最大质因数)

2017-04-06  本文已影响0人  请你吃薯片
题目:13195的所有质因数为5、7、13和29。600851475143最大的质因数是多少?

Java:

long n = 6008514751433L;
long n2 = n;
int factor = 2;
int lastFactor = 1;
while (n > 1){
  if (n % factor == 0){
    lastFactor = factor;
    n = n / factor;
    while (n % factor == 0)
       n = n / factor;
    }
  factor+=1;
  if (factor > Math.sqrt(n2))
     break;
  }
LogUtil.e("lastFactor==",lastFactor+"");

python3:

import math
n = 600851475143
n2 = n;
factor = 2
lastFactor = 1
while n > 1:
    if n % factor == 0:
        lastFactor = factor
        n = n / factor
        while n % factor == 0:
            n = n / factor
    factor+=1
    if (factor > math.sqrt(n2)):
        break
print("lastFactor==",lastFactor)
图片来自优美图

初期教育应是一种娱乐,这样才更容易发现一个人天生的爱好。
—— 柏拉图

上一篇下一篇

猜你喜欢

热点阅读