Python-计算一个数字有多少个因素

时间:2018-10-07 23:46:09

标签: python list primes

我一直在尝试制作一个如下所示的“常用号码检查器”:

请输入数字:7

因素是:

1

7

7是质数

再试一次?是

输入数字:6

因素是:

1

2

3

6

6不是质数

它有6个因素

我停留在最后一部分,该部分计算非素数有多少个因素(在下面的代码中列为“ num_factors”)。谢谢! *为清楚起见进行了编辑。

def main():
    num = int(input("Please enter an integer between 2 and 5,000: "))

    def list_factors(x):
        print("The factors of your number are: ")
        for i in range(1, x + 1):
                if x % i == 0:
                    print(i)
    list_factors(num)

    def is_prime(num):
        if num>1:
            for i in range(2,num):
                if (num%i) == 0:
                    print(num, "is NOT a prime number")
                    break
            else:
                print(num, "is a prime number")

        else:
            print(num, "is not a prime number")
    is_prime(num)

    def num_factors(x):
        for i in range(1, x + 1):
                if x % i == 0:
                    list = []
                    print("It has", len(list), "factors")
    num_factors(x)


    print()
    again=input("Try again? (y/n): ")
    if again == "y":
        print()
        main()
    else:
        print("Bye!") 

if __name__ == "__main__":
    main()

6 个答案:

答案 0 :(得分:1)

我建议您先建立一个因素列表,而不要在每个步骤中进行迭代。

def get_factors(n):
    factors = []
    for i in range(1, n+1):
        if n % i == 0:
            factors.append(i)
    return factors

现在您可以根据因素列表定义其他功能:

def is_prime(factors):
    return len(factors) == 2

def num_factors(factors):
    return len(factors)

如果您想提高实施效率,建议您阅读素数分解。

答案 1 :(得分:0)

更改

def num_factors(x):
    for i in range(1, x + 1):
            if x % i == 0:
                list = []
                print("It has", len(list), "factors")
num_factors(x)

def num_factors(x):
    list = []
    for i in range(1, x + 1):
            if x % i == 0:
                list.append(x)      
    print("It has", len(list), "factors")
num_factors(num)

您的电话号码称为num,而不是x。然后,您应该在for循环外创建因子列表,并将每个因子附加到列表中。

如注释中所建议的那样,您应该在功能之外进行打印。另外,您不需要检查大于x//2的因子。所以我建议像这样:

def num_factors(x):
    list = []
    for i in range(1, x//2 + 1):
            if x % i == 0:
                list.append(x)      
    return facts
facts=num_factors(num)
print("It has", len(facts), "factors")

答案 2 :(得分:0)

a=int(input("Enter number: "))
k=0
for i in range(2,a//2+1):
    if(a%i==0):
        k=k+1
if(k<=0):
    print("Number is prime")
else:
    print("Number isn't prime") 

答案 3 :(得分:0)

您非常接近,唯一的调整是您希望添加通过if x % i == 0且也可以声明为if not x % i:的那些值,并且希望将空白列表移出您的环。然后只需返回该列表的长度即可。

def num_factors(non_prime):
    l = []
    for i in range(1, non_prime+1):
        if not non_prime % i:
            l.append(i)
    return len(l)

print('It has {} factors'.format(num_factors(10)))
# It has 4 factors.

未来参考

只是让您了解为什么探索内置函数很重要,您可以在一个表达式中使用filter来实现这一点

def num_factors(non_prime):
    return len(list(filter(lambda x: not non_prime % x, range(1, non_prime +1))))

以及列表理解

def num_factors(non_prime):
    return len([i for i in range(1, non_prime +1) if not non_prime % i])

您可以在https://docs.python.org/3/

上找到相关信息

答案 4 :(得分:0)

# input and validation integer number
try:
    num = int(input(
        "Please enter an integer number between 2 and 5,000 : "))
except ValueError:
    print("Please enter an integer number between 2 and 5,000!!!")

# for check number.
# If number is prime it's true
# If number not prime it's false 
# default is true
status = True

print("%d divided by %d" % (num, 1))
for i in range(2, int(num / 2)+1):
    if num % i == 0:
        status = False
        print("%d divided by %d" % (num, i))
print("%d divided by %d" % (num, num))


if status:
    print("%d is prime" % (num))
else:
    print("%d is not prime" % (num))

答案 5 :(得分:0)

如果您想捕获所有因素,则可以使用以下子。如果您给它一个像1024这样的数字,它将返回一个十个2(所有因子)的列表

var dynamicFunction:Array<Functions>; 
for (i=0; i<10; i++){
@decorator1
@decorator2
dynamicFunction[i] = new Function('a','b','return a+b');
}