简体   繁体   English

如何使我的程序在python中更有效?

[英]How to make my program more efficient in python?

So my task is this: A number, n, is called lean if the sum of all its factors is n + 1. A number, n, is called fat if the sum of all its factors is greater than 3*n. 所以我的任务是:如果所有因素的总和为n + 1,则将n称为瘦。如果所有因素的总和大于3 * n,则将n称为胖。 A number, n, is called Jack Sprat if it is both lean and the next number, n + 1 is fat. 如果数字n都比较瘦,而下一个数字n +1就是脂肪,则将其称为Jack Sprat。

I have to ask the user to input a number and I have to say whether it is lean, fat or Jack Sprat. 我必须要求用户输入一个数字,并且必须说它是瘦,胖还是杰克·斯普拉特。 I also have to print all the numbers that are jack sprat from 1 to 10000 in 1 second. 我还必须在1秒内打印从1到10000的所有杰克·斯普拉特数字。

This program takes about 10 seconds to do that 该程序大约需要10秒钟

Here is the Code: 这是代码:

def isLean(n, t):
if t == n + 1:
    return True
else:
    return False

def isFat(n, t):
    if t > 3*num:
        return True
    else:
        return False

def isJackSprat(n, t, t2):
    if t == n+1 and t2 > 3*(n+1):
        return True
    else:
        return False

num = int(input("Please enter a number: "))

total = 0
total2 = 0
total3 = 0
total4 = 0
prime = ""

for factor in range(1,num+1):
    if num % factor == 0:
        total += factor

for factor in range(1,num+2):
    if (num+1) % factor == 0:
        total2 += factor

if isLean(num,total) == True:
    print ("Lean: Yes")
elif isLean(num,total) == False:
    print ("Lean: No")

if isFat(num,total) == True:
    print ("Fat: Yes")
elif isFat(num,total) == False:
    print ("Fat: No")

if isJackSprat(num, total, total2) == True:
    print ("Jack Sprat: Yes")
elif isJackSprat(num, total, total2) == False:
    print ("Jack Sprat: No")


print ("These are the Jack Sprat Numbers from 1 - 1000")

for count in range (1,10000):
    if count % 2 != 0:
        for factor in range (1,count+ 1):
            if factor %  2 != 0:
                if count % factor == 0:
                    total3 += factor
        for factor in range (1,count+2):
            if (count+1) % factor == 0:
                total4 += factor 
        if total3 == (count + 1) and total4 > 3*(count + 1):
            prime = prime + str(count) + ", "
        total3 = 0
        total4 = 0

print (prime[0:len(prime)-2])

I would really appreciate it if I can get some help 如果能得到一些帮助,我将不胜感激

You can get a big speed up by having your for loops only go up to the square root of the number. 通过使for循环仅上升到数字的平方根,可以大大提高速度。 Each factor less than the square root will have a pair that is larger than the square root, so you can find all the factors by only iterating up to the square root. 每个小于平方根的因子都会有一对大于平方根的对,因此您可以通过仅迭代到平方根来找到所有因子。

Try it like the Sieve of Eratosthenes, but instead of a Boolean array and crossing out, have an int array which for each index builds up the sum of its factors. 尝试像Eratosthenes的筛子一样,但是要使用布尔数组而不是布尔数组,而要交叉运算,而要使用int数组,该数组为每个索引建立其因子之和。 I just did that and it takes about 0.05 seconds to find all Jack Sprat numbers up to 10000 (the same that your code finds). 我只是这样做,大约需要0.05秒才能找到所有最多10000个Jack Sprat编号(与您的代码所找到的相同)。

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

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