简体   繁体   English

当我输入大量数据时,为什么我的简单代码要花这么多时间来执行?

[英]Why is my simple code taking so much time to execute when i give in large numbers as input?

I have written a simple code to print the largest prime factor of a given number from Project Euler.我编写了一个简单的代码来打印来自 Project Euler 的给定数字的最大素因子。 It works just fine for numbers like 24, but there is no response from the python shell for large numbers!它适用于像 24 这样的数字,但对于大数字,python shell 没有响应!

a=600851475143
b=[]
for i in range(2,600851475143):
    if a%i==0:
         if i==2:
            b.append(i)
            continue
         for j in range(2,i+1):
             if j==i:
                b.append(i)
             if i%j==0:
                break
 print(max(b))
 print(b)

You can use an algorithm like this to get large factors of prime numbers:您可以使用这样的算法来获得素数的大因数:

import math

def LLL(N):
   p = 1<<N.bit_length()-1
   if N == 2:
     return 2
   if N == 3:
     return 3
   s = 4
   M = pow(p, 2) - 1
   for x in range (1, 100000):
     s = (((s * N ) - 2 )) % M
     xx = [math.gcd(s, N)] + [math.gcd(s*p+x,N) for x in range(7)] + [math.gcd(s*p-x,N) for x in range(1,7)]
     try:
        prime = min(list(filter(lambda x: x not in set([1]),xx)))
     except:
        prime = 1
     if prime == 1:
        continue
     else:
        break
   return N/prime

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么我的程序需要这么长时间才能执行/我完成问题的方式有问题吗? - Why is my program taking so long to execute/is there something wrong with the way I completed the problem? 为什么我的代码打印数量如此之大? - Why is my code printing such large numbers? 编写挑战代码需要太多时间 - Programming Challenge Code taking too much time 为什么我的代码为同一个单词给出不同的数字? - Why is my code give different numbers for the same word? 在 pyspark 中读取太多小文件花费了太多时间 - reading too many small files in pyspark taking so much of time 为什么等效的Python代码要慢得多 - Why is equivalent Python code so much slower 输入输入后,以下代码不执行(打印推文),是否有原因? - Is there a reason why the following code does not execute (print the tweets) after taking input? 我如何继续循环我的程序,以便用户可以输入尽可能多的内容? - How do i keep looping the same my program so the user can input as much as they want? pytorch 中的简单起始块需要更长的时间来训练 GPU? - simple inception block in pytorch taking much much longer time to train on GPU? 为什么即使我使用了埃拉托色尼筛法并且我也使用了集合而不是列表,我的质数代码对大数不起作用? - Why does my code for prime numbers not working for large numbers even though I used sieve of eratosthenes and I also used sets instead of list?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM