简体   繁体   English

找出所有能整除一个数的数

[英]Finding all numbers that evenly divide a number

So I'm trying to make a program that when I input a number it will give me all the factors( 12 -> 1,2,3,4,6,12 ).所以我正在尝试制作一个程序,当我输入一个数字时,它会给我所有的因素( 12 -> 1,2,3,4,6,12 )。 I only started programming very recently so there may be some very obvious things.我最近才开始编程,所以可能有一些非常明显的事情。 But here's my code但这是我的代码

numbers = [1]
newnum = 1
chosen = int(input("Enter what you want the factors of: "))
def factors(numbers,newnum,chosen): 
    lastnum = numbers[-1]
    if (chosen == lastnum):
        for number in numbers:
            if (number % 1 != 0):
                numbers.remove(number)
                print (numbers)
            else:
                factors(numbers,newnum,chosen)
    else:
        newnum = numbers[-1] + 1
        numbers.append(newnum)
        print (numbers)
        factors(numbers,newnum,chosen)

factors(numbers,newnum,chosen)  

Ok, so I don't really need the redundancies addressed but if you see something that would completely stop the program from working please point it out.好的,所以我真的不需要解决冗余问题,但是如果您看到会完全阻止程序运行的内容,请指出。 Sorry I bothered you all with this but I don't know what else to do.抱歉,我为此打扰了大家,但我不知道还能做什么。

There are lots of problems:有很多问题:

  • Every integer number modulo 1 is zero because each integer is divisible by one without remainder.每个以 1 为模的整数都是零,因为每个整数都可以被 1 整除而没有余数。

  • You remove items from the list you're iterating over, that will definetly give wrong results if you don't do it carefully!您从正在迭代的列表中删除项目,如果您不小心操作,那肯定会给出错误的结果!

  • You try to do recursion but you don't return the result of the recursive call.您尝试进行递归,但不返回递归调用的结果。 That's possible because you operate on a mutable list but it's generally not really good style这是可能的,因为你在一个可变列表上操作,但它通常不是很好的风格

  • You don't have any inline comments explaining what that line is supposed to do, so it's hard to give any reasonable guidance on how to improve the code.您没有任何内联注释来解释该行应该做什么,因此很难就如何改进代码提供任何合理的指导。

If you want a code that finds all factors, consider something like this:如果您想要找到所有因素的代码,请考虑这样的事情:

chosen = int(input("Enter what you want the factors of: "))

def factors(chosen, currentnum=None, numbers=None): 
    # Recursion start, always append 1 and start with 2
    if numbers is None:
        numbers = [1]
        currentnum = 2
    # We're at the last value, it's always divisible by itself so
    # append it and return
    if currentnum == chosen:
        numbers.append(currentnum)
        return numbers
    else:
        # Check if the chosen item is divisible by the current number
        if chosen % currentnum == 0:
            numbers.append(currentnum)
        # Always continue with the next number:
        currentnum += 1
        return factors(chosen, currentnum, numbers)


>>> factors(chosen)
Enter what you want the factors of: 12
[1, 2, 3, 4, 6, 12]

That's not the optimal solution but it uses recursion and gives a proper result.这不是最佳解决方案,但它使用递归并给出了正确的结果。 Just don't enter negative values or catch that case in the function at the beginning!只是不要在函数的开头输入负值或捕获这种情况!

    # Two Pointer Approach

     ans = []
     
     
     def divisor(val):
         result = []
     
         for i in range(1, val + 1):
             ans.append(i)
     
         i = 0
         j = len(ans) - 1
     
         while i < j:
     
             if ans[i] * ans[j] == ans[-1]:
                 result.append(ans[i])
                 result.append(ans[j])
                 i += 1
             else:
                 j -= 1
     
         return sorted(result)
     
     
     print(divisor(12))


# Output

>>> [1, 2, 3, 4, 6, 12]

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

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