Finding all divisors of a number optimization

后端 未结 4 1873
一个人的身影
一个人的身影 2020-12-15 01:59

I have written the following function which finds all divisors of a given natural number and returns them as a list:

def FindAllDivisors(x):
    divList = []         


        
4条回答
  •  醉梦人生
    2020-12-15 02:38

    I'd suggest storing the result of math.sqrt(x) in a separate variable, then checking y against it. Otherwise it will be re-calculated at each step of while, and math.sqrt is definitely not a light-weight operation.

提交回复
热议问题