Euler problem number #4

前端 未结 14 2015
萌比男神i
萌比男神i 2021-01-03 11:00

Using Python, I am trying to solve problem #4 of the Project Euler problems. Can someone please tell me what I am doing incorrectly? The problem is to Find the larg

14条回答
  •  遥遥无期
    2021-01-03 11:33

    Here's some general optimizations to keep in mind. The posted code handles all of this, but these are general rules to learn that might help with future problems:

    1) if you've already checked z = 995, y = 990, you don't need to check z = 990, y = 995. Greg Lind handles this properly

    2) You calculate the product of z*y and then you run x over a huge range and compare that value to y*z. For instance, you just calculated 900*950, and then you run x from 1000 to 1M and see if x = 900*950. DO you see the problem with this?

    3) Also, what happens to the following code? (this is why your code is returning nothing, but you shouldn't be doing this anyway)

    x = str(100)
    y = 100
    print x == y
    

    4) If you figure out (3), you're going to be printing a lot of information there. You need to figure out a way to store the max value, and only return that value at the end.

    5) Here's a nice way to time your Euler problems:

    if __name__ == "__main__":
        import time
        tStart = time.time()
        print "Answer = " + main()
        print "Run time = " + str(time.time() - tStart)
    

提交回复
热议问题