简体   繁体   English

我怎样才能找到素因数

[英]How can i find Prime factors

def fi_da_prfac(var):
    fac = []
    prfac = []
    z = range(2, (var/2)+1)
    z.append(var)
    for t in z:
        if var == t:
            prfac.append(t)
            z = range(2, (var/2)+1)
            z.append(var)
            break
        else:
            if  var % int(t) == 0:
                prfac.append(t)
                var = var/t
                del z
                z = range(2, (var/2)+1)
                z.append(var)
                del t

    return prfac

I am a beginner at coding.我是编码初学者。 I am trying to write a code to find the Prime factorisation of a given number.我正在尝试编写代码来查找给定数字的质数分解。 If we analyze the code, what I want to do is that if I find a factor, I want to start the for loop again ie.如果我们分析代码,我想要做的是,如果我找到一个因素,我想再次启动for循环即。 start the for loop from t = 2. I didnt find any way to do it.从 t = 2 开始 for 循环。我没有找到任何方法来做到这一点。 So I deleted "t" at the end.所以我最后删除了“t”。 However the code isnt giving the desired output.然而,代码没有给出所需的输出。 I tried a lot to debug it but couldn't.我尝试了很多来调试它,但不能。 Please help请帮忙

The fundamental piece to the puzzle you are missing is the yield operator, this makes the function act as a generator and keeps its place when running through a loop.您所缺少的难题的基本部分是 yield 运算符,这使函数充当生成器并在运行循环时保持其位置。 A key thing to note however is the output will be a generator object which must be looped through to extract your desired output.然而,需要注意的关键是输出将是一个生成器对象,必须循环遍历以提取所需的输出。

Your example:你的例子:

import math

def fi_da_prfac(var):
    z = range(2, math.ceil(var/2)+1)
    z.append(var)
    for t in z:
        if var % int(t) == 0:
            yield t

prfac = yield_eg(15)
[fac for fac in prfac]

This is a nice problem for learning how to code, however as others of mentioned there are more concise pre-made functions to achieve this if you need.这是学习如何编码的一个很好的问题,但是正如其他人提到的,如果您需要,可以使用更简洁的预制函数来实现这一点。

Usherwood厄舍伍德

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

相关问题 我有一个数字的素因子的Python列表。 我如何(pythonically)找到所有因素? - I have a Python list of the prime factors of a number. How do I (pythonically) find all the factors? 如何使用python查找素数 - how to find Prime factors using python 如何使用python查找数字的素因 - How to find the prime factors of a number with python 在给定素数分解的情况下,如何找到数的因子? - How to find the factors of a number given the prime factorization? Python找到主要因素 - Python find Prime Factors 查找多个质因数 Python - Find multiple prime factors Python 如何检查素因数列表以确定原始数字是否可以表示为 Python 中两个 3 位数字的乘积? - How do I check a list of prime factors to determine if the original number can be expressed the product of two 3-digit numbers, in Python? Python在没有循环的情况下找到素因数的总和 - Python find sum of prime factors without loop Python:找出一个数的两个质因数。 如何让这段代码更有效率? - Python: Find the two prime factors of a number. how to make this code more efficiency? 如何找到一个数字的质因数?花了几个小时试图弄清楚为什么 if 和 else 会同时执行 - how to find the prime factors of a number?spent hours trying to figure out why both the if and else are executed simultaneously
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM