其他分享
首页 > 其他分享> > Problem 3: Largest prime factor

Problem 3: Largest prime factor

作者:互联网

Problem 3

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

解答

这道题目很简单,就是要我们找出 600,851,475,143 的最大素因数。来看看我们的 Haskell 程序吧:

factors 1 = []
factors n = let p = head [ x | x <- [2..], mod n x == 0 ] in p : factors (div n p)
main = print $ last $ factors 600851475143

上述程序中:

改进

每次都从 2 开始逐个测试是没有必要的,完全可以从上次结束的地方开始测试:

factors = flip fact 2
fact 1 _ = []
fact n k = let p = head [ x | x <- [k..], mod n x == 0 ] in p : fact (div n p) p
main = print $ last $ factors 600851475143

上述程序中:

再次改进

由于 2 是唯一的偶素数,因此我们可以先分解出所有的素因数 2,然后就可以只测试奇数了:

factors n = if even n then 2 : factors (div n 2) else fact n 3
fact 1 _ = []
fact n k = let p = head [ x | x <- [k,k+2..], mod n x == 0 ] in p : fact (div n p) p
main = print $ last $ factors 600851475143

上述程序中:

最终的改进

由于整数 n 最多只能有一个素因数大于 sqrt_n,我们可以相应设置测试的上限:

factors n = if even n then 2 : factors (div n 2) else fact n 3
fact 1 _ = []
fact n k = if null xs then [n] else let p = head xs in p : fact (div n p) p
  where xs = [ x | x <- [k,k+2..ceiling $ sqrt $ fromIntegral n], mod n x == 0 ]
main = print $ last $ factors 600851475143

上述程序中:

注意,前面几个程序要测试的数的列表虽然是无限列表,但实际上等效于上限为 n 的列表。因为列表推导式中的条件 mod n x == 0,当 x 增加到 n 时一定会成立(虽然 x 可能没有增加到 n 程序就终止了,但 x 一定不会超过 n)。

详细的论述请参见:003_overview.pdf 。这些程序对小整数来说都足够快了,但是对大整数则太慢了。

 

 

*进阶

将大整数分解为素因数的乘积是一项非常困难的工作,目前最有效的方法是数域筛法,可参阅《整数分解》一书。

将下面这个整数分解为素因数的乘积,就可获得 20 万美元的奖金。Möchte jemand es probieren?

RSA-2048

RSA-2048 has 617 decimal digits (2,048 bits). It is the largest of the RSA numbers and carried the largest cash prize for its factorization, US$200,000. The largest factored RSA number is 768 bits long (232 decimal digits), and the RSA-2048 may not be factorizable for many years to come, unless considerable advances are made in integer factorization or computational power in the near future.

 

 

标签:prime,函数,factors,列表,因数,测试,factor,Problem,fact
来源: https://www.cnblogs.com/pyrie/p/14033802.html