简体   繁体   中英

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 ). 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.

  • 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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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