在Python中找到最大的素数因子太慢了(Finding largest prime factor too slow in Python)

编程入门 行业动态 更新时间:2024-10-26 08:30:57
在Python中找到最大的素数因子太慢了(Finding largest prime factor too slow in Python)

题:

13195的主要因素是5,7,13和29. 600851475143的最大主要因素是什么?

我尝试使用下面的代码解决它,它需要很长时间才能运行。 有没有最好的方法来解决这个问题?

def find_prime(num, ranger): count = 0 prime = True for i in range(2, num): if num % i == 0: count = count + 1 if count > 1: prime = False return prime return prime prime_list = [] target = 600851475143 for i in range(2,target ): if target % i == 0: print(i) if find_prime(i,target) and i % 2 == 0: prime_list.append(i) print(prime_list) print(max(prime_list))

请建议。

Question:

The prime factors of 13195 are 5, 7, 13 and 29. What is the largest prime factor of the number 600851475143 ?

And I tried to solve it by using the below snippet, its taking long time to run. Is there any best way to solve this problem?

def find_prime(num, ranger): count = 0 prime = True for i in range(2, num): if num % i == 0: count = count + 1 if count > 1: prime = False return prime return prime prime_list = [] target = 600851475143 for i in range(2,target ): if target % i == 0: print(i) if find_prime(i,target) and i % 2 == 0: prime_list.append(i) print(prime_list) print(max(prime_list))

Please suggest.

最满意答案

n=600851475143 d=2 while d < n/d: if n%d==0: n/=d else: d+=1 print n n=600851475143 d=2 while d < n/d: if n%d==0: n/=d else: d+=1 print n

更多推荐

本文发布于:2023-07-07 09:07:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1061417.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:素数   因子   慢了   中找到   Python

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!