Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 数代倒数_Python_Loops_Recursion_Mathematical Optimization - Fatal编程技术网

Python 数代倒数

Python 数代倒数,python,loops,recursion,mathematical-optimization,Python,Loops,Recursion,Mathematical Optimization,我正在尝试对一组给我的数字(f,m)进行反向工程,我需要通过以下算法,找出从1,1开始需要多少代: x = 1 y = 1 new_generation = y+x x OR y = new_generation 也就是说,我不知道X或Y是否改变了,另一个变量保持不变。。。对于结束值4和7,可能的输出列表如下所示: f = 4 m = 7 [1, 1] [2, 1, 1, 2] [3, 1, 2, 3, 3, 2, 1, 3] [4, 1, 3, 4, 5, 3, 2, 5, 5, 2, 3,

我正在尝试对一组给我的数字(f,m)进行反向工程,我需要通过以下算法,找出从1,1开始需要多少代:

x = 1
y = 1
new_generation = y+x
x OR y = new_generation
也就是说,我不知道X或Y是否改变了,另一个变量保持不变。。。对于结束值4和7,可能的输出列表如下所示:

f = 4
m = 7
[1, 1]
[2, 1, 1, 2]
[3, 1, 2, 3, 3, 2, 1, 3]
[4, 1, 3, 4, 5, 3, 2, 5, 5, 2, 3, 5, 4, 3, 1, 4]
[5, 1, 4, 5, **7, 4**, 3, 7, 7, 5, 2, 7, 7, 2, 5, 7, 7, 3, **4, 7**, 5, 4, 1, 5]
其中每两组数字(2,1)和(1,2)是一个可能的输出。注意**表示答案(在这种情况下,顺序并不重要,只要m和f在列表中都有它们的值)

很明显,这里有指数级的增长,所以我不能(或者效率较低)列出一个清单,然后找到答案;相反,我使用以下代码来反转此过程

def answer(m,f):
    #the variables will be sent to me as a string, so here I convert them...
    m = (int(m))
    f = (int(f))
    global counter
    #While I have not reduced my given numbers to my starting numbers....
    while m != 1 or f != 1:
        counter +=1
        #If M is greater, I know the last generation added F to M, so remove it
        if m > f:
            m = m-f
        #If F is greater, I know the last generation added M to M, so remove it
        elif f > m:
            f = f-m
        else:
            #They can never be the same (one must always be bigger, so if they are the same and NOT 1, it can't be done in any generation)
            return "impossible"
    return str(counter)

print(answer("23333","30000000000"))
这将返回正确的答案(例如,4,7返回“4”,这是正确的),但当我传递较大的数字时,需要花费很长时间(我必须能够处理10^50,我知道这是一个疯狂的数字!)。
我的想法是,我应该能够应用一些数学方程来减少这个数字,并使它们成倍增加,但我很难找到一种方法来做到这一点,同时保持答案的完整性(例如,如果我用较大的数除以较小的数,在较小的数(7300)上,我得到一个非常接近(但错误)答案,然而,对于更接近的数字,例如(23333300000),答案是否定的,即使接近,这是有意义的,因为在生成路径上的差异)。注:我也曾在递归函数中尝试过这一点(以查找代),并使用了非反转方法(构建列表并检查答案;由于明显的原因,该方法的速度明显较慢)


以下是一些测试用例及其答案:

非常感谢您的帮助!另外,我正在运行Python 2.7.6

def back():
    global f,m,i
    if f<m:
        s=m//f
        i+=s
        m-=s*f
    elif m<f:
        s=f//m
        i+=s
        f-=s*m
    else:
        return False
    return True
while True:
    f=int(raw_input('f = '))
    m=int(raw_input('m = '))
    i=0
    while True:
        if f==m==1:
            print 'Output:',str(i)
            break
        else:
            if not back():
                print 'Output: impossible'
                break
    print
[编辑]
下面的代码正在按需要工作

from fractions import gcd

def answer(m,f):
    #Convert strings to ints...
    m = (int(m))
    f = (int(f))

    #If they share a common denominator (GCD) return impossible
    if gcd(m,f) != 1:
        return "impossible"
    counter = 0
    #While there is still a remainder...
    while m != 0 and f != 0:
        if m > f:
            counter += m // f
            #M now equals the remainder.
            m %= f
        elif f > m:
            counter += f // m
            f %= m
    return str(counter - 1)

尝试一种递归形式:

(Python 2.7.6)

def back():
全局f,m,i

如果f这不是Python问题,也不是真正的编程问题。这是一个旨在让你思考的问题。因此,如果你只是从别人那里得到答案,你将不会从练习中获得任何知识或后见之明


只需在
循环时在
中添加一个
打印(m,f)
,观察小输入的数字是如何变化的。例如,尝试使用类似于
(3100)
:您没有看到任何可以加快速度的方法,而不是重复地从较大的数字中删除3吗?

您采用的是自上而下的方法。如果你使用整数除法而不是重复减法,你可以大大提高运算速度

def answer(m, f):
    m = int(m)
    f = int(f)
    counter = 0
    while m != 0 and f != 0:
        if f > m:
            m, f = f, m
        print(m, f, counter, sep="\t")
        if f != 1 and m % f == 0:
            return "impossible"
        counter += m // f
        m %= f
    return str(counter - 1)
使用上述方法,
答案(2333330000000)
产生

30000000000 23333   0
23333   15244   1285732
15244   8089    1285733
8089    7155    1285734
7155    934 1285735
934 617 1285742
617 317 1285743
317 300 1285744
300 17  1285745
17  11  1285762
11  6   1285763
6   5   1285764
5   1   1285765
1285769
7   4   0
4   3   1
3   1   2
4
答案(4,7)
产生

30000000000 23333   0
23333   15244   1285732
15244   8089    1285733
8089    7155    1285734
7155    934 1285735
934 617 1285742
617 317 1285743
317 300 1285744
300 17  1285745
17  11  1285762
11  6   1285763
6   5   1285764
5   1   1285765
1285769
7   4   0
4   3   1
3   1   2
4

“我正在运行Python 2.7.6”-可能与问题无关,但为什么?现在已经三年了,不是3.x了。你能澄清一下规则吗?你是说(1,1)可以转到(1+1,1)还是(1,1+1)?然后(2,1)可以转到(2+1,1)或(2,2+1)?所以每一代都是从前一对派生出来的一对数字?这与我至今无法摆脱的遗留代码有关;通常我喜欢Python3或至少2.7+(项目要求用2.7.6编写,并且没有外部依赖项)是的,John D;这是正确的,每个周期/代的变化数量可能不同。例如,如果(1,1)-->(2,1 | 1,2)(它将随机选择现在是(2,1还是1,2)可能我遗漏了一些东西-仍然不确定如何到达(2,3)…您能展示从(1,1)到(2,3)的步骤吗?我尝试过类似的方法,但是你的代码需要22秒才能完成,我的代码需要8秒,列表和递归会导致它变慢(使用的输入数字是:3000000000000和23333)。我看到了很多方法来加速它,但不是不危及进化周期。此外,我不想寻找“答案在这里”我正在寻找正确方向的指导,以解决我的代码中存在的问题。嗯,是的,但如果不透露答案,给你比我更多的指导几乎是不可能的……啊,如果你不知道整数除法和模运算符,答案可能就不那么明显了……你可以通过寻找“欧几里德除法”(例如在)。谢谢!我在Python中没有意识到这一点;我确信有一些关于整数除法的优秀文档,但您是否介意为我解释它的基础,或者您是否知道关于它如何工作的任何好文章(我想了解它的功能,而不仅仅是用法)。再次感谢您!没什么大不了的。
x//y
是Python中整数除法的运算符,相当于将除法结果的小数部分设置为0(与浮点除法相反)。Python在这里有点例外:在许多情况下(静态类型)如果输入是整数,则
/
运算符已经执行整数除法。在这种情况下,Python会输出一个浮点。
%
(以及相关的
%=
)是一个模运算符,它基本上计算整数除法的余数。好吧,所以我遇到了一个新问题,我的上一个函数总是返回正确的答案,速度很慢;这个函数几乎可以处理所有问题。我的测试用例通过了,但它无法捕捉到一个不可能的问题(我不知道输入).有什么想法吗?我猜是f=n*m或m=n*f的情况。这也是不可能的,但现在处理不正确。我不确定我是否知道你的意思?我注意到,如果我给它一个不可能的选项(例如,4,2),它会给我一个int(在4,2的情况下,它会给我2)如果不可能的话,简单地回答错误(我目前正在进行一项变通方案)