简体   繁体   English

python 代码未运行。我正在计算一个数字的素数(项目欧拉问题 - 3)

[英]python code is not running.I am calculating prime factors of a number (project euler problem - 3)

I am trying to calculate prime factors of a number in python python 3.8.2 32bit (when i pass 35 in code its output should be 7,and so on).我正在尝试计算 python python 3.8.2 32bit中数字的素数(当我在代码中传递 35 时,它的 output 应该是 7,等等)
But for some reason the program does not return any answer (when i click run the cmd does not output anything).But when i run this javascript, the exact same code works there.但是由于某种原因,程序没有返回任何答案(当我单击运行 cmd 时没有 output 任何东西)。但是当我运行这个 javascript 时,完全相同的代码在那里工作。
(Previously i had an array(list) to which i would append prime factors and at last i would pop the last element which worked for smaller numbers, but for really large numbers i would get a Memory error,so i converted it to use only one variable which will be updated for every while loop). (以前我有一个数组(列表),我会使用 append 素数因子,最后我会弹出最后一个适用于较小数字的元素,但对于非常大的数字,我会得到 Memory 错误,所以我将其转换为仅使用每个while循环都会更新一个变量)。
What is going on here??这里发生了什么??

My code is:我的代码是:

import math
# Computes only prime factors of n
def compute(n):
    arr = 0
    # Checks if n is divisible by 2, and if it is divisible,returns 2 because there will be no any other 
    # prime factor.
    if n % 2 == 0:
        return 2
    # Now that 2 is eliminated we only check for odd numbers upto (square root of n)+1 
    for i in range(1, round(math.sqrt(n)) + 1, 2):
        while n % i == 0:
            arr = n/i
            n /= i

    return str(arr)

print(compute(81))

I am a newbie in python so plz tell me if i have made any silly mistakes.我是 python 的新手,所以请告诉我是否犯了任何愚蠢的错误。 Ty.泰。

For numbers that are not divisible by 2, your code runs into an infinite loop at while n%i == 0对于不能被 2 整除的数字,您的代码会在while n%i == 0处陷入无限循环

For numbers that are divisible by 2, your function returns 2. The execution of the return statement exits the function.对于能被 2 整除的数字,您的 function 返回 2。 return 语句的执行退出 function。

Even if you change the while in while n%i == 0 to if n% == 0 the function will not work.即使您将while n%i == 0中的 while 更改为if n% == 0 ,function 也将不起作用。

You will have to restructure your code.您将不得不重组您的代码。

An easy fix would be to check for all factors of a number till n/2 + 1 and return the factors (in a list) that are prime (which can be checked using a separate isprime function.一个简单的解决方法是检查一个数字的所有因子直到 n/2 + 1 并返回素数因子(在列表中)(可以使用单独的 isprime function 检查。

def isprime(n):
    for x in range(2,n//2+1):
        if n%x==0:
            return False
    return True

def compute(n):
    arr = [] 
    for i in range(2, n//2+1):
       if n % i == 0:
            if isprime(i):
                arr.append(i)

    return arr

If you want all prime factors (which i guess) you shouldn't return values before you have all prime factors in a list for example.例如,如果您想要所有主要因素(我猜),则不应在列表中包含所有主要因素之前返回值。

And with this programm you only check once for a 2. But 4 has 2*2.使用这个程序,您只需检查一次 2。但 4 有 2*2。 Put it in a loop.把它放在一个循环中。

So this code saves all prime factors in a list:因此,此代码将所有主要因素保存在一个列表中:

from math import sqrt, ceil

def get_prime_factors(n):
    prime_factors = []

    while n > 1:
        for i in range (2, ceil(sqrt(n))+1):
            if n % i == 0:
                possible_prime_number = i
                while True:
                    for j in range(2, possible_prime_number//2-1):
                        if possible_prime_number % j == 0:
                            possible_prime_number //= j
                            break
                    prime_factors.append(possible_prime_number)
                    n //= possible_prime_number
                    break

    return prime_factors

if you only want the highest one replace return prime_factors with return max(prime_factors)如果您只想要最高的一个,请将return prime_factors替换为return max(prime_factors)

OK.好的。 I think counting upto 600 billion is ridiculous(considering that i am on a 32 bit system).我认为数到 6000 亿是荒谬的(考虑到我在 32 位系统上)。 So here is the final code that i am setteling on which gives answer in acceptable time upto 99 million, without using any array and only 1 for loop.所以这是我正在解决的最终代码,它在可接受的时间内给出答案,最高可达 9900 万,不使用任何数组,只有 1 个 for 循环。

from math import ceil
# Computes only prime factors of n
def compute(n):
    arr = 0
    # Checks if n is divisible by 2, and if it is divisible,sets arr =2 
    if n % 2 == 0:
        arr = 2
    # Now that 2 is eliminated we only check for odd numbers upto (square root of n)+1 
    for i in range(1, ceil(n/2) + 1, 2):
        if n % i == 0:
            arr = i
            n //= i

    return str(arr)

print(compute(999999999))

PS:- If you see any possible improvement in my code plz tell me. PS:-如果您发现我的代码有任何可能的改进,请告诉我。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM