简体   繁体   English

Python - 更快的循环方式

[英]Python - faster way to do for loops

I was just wondering if anyone knew a faster / more efficient way to test this. 我只是想知道是否有人知道更快/更有效的方法来测试它。

(It's suppose to write out every solution to the equation) (假设写出方程式的每个解决方案)

        for z in range(500):
           for x in range(500):
             for y in range(500):
               if (x * 80) + (z * 65) + (y * 50) == 1950:
                 print("x " + str(x) + " z " + str(z) + " y " + str(y))

Thanks! 谢谢!

We assume that x,y,z must be positive integers. 我们假设x,y,z必须是正整数。 There's an infinity of solutions otherwise. 否则就会有无穷无尽的解决方案。

Calculate y from x and z 从x和z计算y

Here's a method which should be 500x faster than yours, simply because it doesn't iterate over y : 这是一个应该比你快500倍的方法,因为它不会迭代y:

for z in range(500):
    for x in range(500):
        fifty_y = 1950 - (x * 80) - (z * 65)
        if fifty_y >= 0 and (fifty_y % 50) == 0:
            y = fifty_y // 50
            print("x " + str(x) + " z " + str(z) + " y " + str(y))

By iterating over x , y and z , you're basically shooting in the dark and hoping that it lands on 1950 . 通过迭代xyz ,你基本上是在黑暗中拍摄并希望它在1950登陆。

But you know that 50 * y = 1950 - x * 80 - z * 65 , so you can calculate y directly from x and z . 但是你知道50 * y = 1950 - x * 80 - z * 65 ,所以你可以直接从xz计算y

50 * y should be positive, and if y is to be an integer, it should be divisible by 50. 50 * y应该是正数,如果y是整数,它应该可以被50整除。

Restrict x and z 限制x和z

range(500) is far too big for x and z if we want y to be positive. 如果我们希望y为正数,则range(500)对于xz来说太大了。

range(1950 // 65 + 1) should be enough for z . range(1950 // 65 + 1)应该足够z

Knowing z , range((1950 - 65 * z)// 80 + 1) will be enough for x . 知道zrange((1950 - 65 * z)// 80 + 1)对于x就足够了。

As a bonus, we're sure that 50 * y is positive and we can remove one test : 作为奖励,我们确定50 * y为正,我们可以删除一个测试:

for z in range(1950 // 65 + 1):
    for x in range((1950 - 65 * z) // 80 + 1):
        fifty_y = 1950 - (x * 80) - (z * 65)
        if (fifty_y % 50) == 0:
            y = fifty_y // 50
            print("x " + str(x) + " z " + str(z) + " y " + str(y))

With Wolfram Alpha 使用Wolfram Alpha

By typing the equation in wolfram alpha , we get : 通过在wolfram alpha中键入等式,我们得到:

Integer solution: y = 13 n + x, z = -10 n - 2 x + 30, n element Z 整数解:y = 13 n + x,z = -10 n - 2 x + 30,n元素Z.

That's perfect! 那很完美! For any x , we just need to choose n so that both y and z are positive. 对于任何x ,我们只需要选择n ,使yz都为正。 There's no need for if anymore. 有没有必要if了。 This code iterates 42 times to display 42 solutions : 此代码重复42次以显示42个解决方案:

for x in range(1950 // 80 + 1):
    for n in range(- (x // 13), (30 - 2 * x) // 10 + 1):
        z = -10 * n - 2 * x + 30
        y = 13 * n + x
        print("x " + str(x) + " z " + str(z) + " y " + str(y))

It almost fits in one line : 它几乎适合一行:

print [(x, 13 * n + x, 30 - 10 * n - 2 * x) for x in range(25) for n in range(-(x // 13), (30 - 2 * x) // 10 + 1)]

This code is alsost 3 million times faster than your original code :) 此代码比原始代码快300万倍:)

This will make you a generator of list containing your x,y,z values that produce 1950. The advantage of the generator construct is that the values are generated when called for instead of then the object is created. 这将使您成为包含生成1950的x,y,z值的列表生成器。生成器构造的优点是在调用时生成值,而不是创建对象。

the_vals = ([x,y,z] for x in range(500) for y in range(500) for z in range(500) if (x * 80) + (z * 65) + (y * 50) == 1950)

Then to print it out you can just do 然后将它打印出来就可以了

for x,y,z in the_vals:
    print("x " + str(x) + " z " + str(z) + " y " + str(y))

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

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