• + 1 comment

    I can't figure out how to fix this code. It only works for 2 tests. For the ones it fails it's usually just one more move than expected

    def downToZero(n):
        moves = 0
        while n != 0:
            a = 1
            b = 1
            if n == 1:
                n -= 1
                moves += 1
                break
            # calculate factors until a < b
            for num in range(n-1, 1, -1):
                if n % num == 0:
                    a = num
                    b = n // num
                    if a < b:
                        break
    
            if (a == 1) or (a == n):
                n -= 1
            else:
                n = max(a, b)
            moves += 1
    
        return moves