Euler problem number #4

前端 未结 14 2031
萌比男神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:28

    Some efficiency issues:

    1. start at the top (since we can use this in skipping a lot of calculations)
    2. don't double-calculate
    def is_palindrome(n):
        s = str(n)
        return s == s[::-1]
    
    def biggest():
        big_x, big_y, max_seen = 0,0, 0
        for x in xrange(999,99,-1):
            for y in xrange(x, 99,-1):  # so we don't double count   
                if x*y < max_seen: continue  # since we're decreasing, 
                                    # nothing else in the row can be bigger
                if is_palindrome(x*y):
                    big_x, big_y, max_seen = x,y, x*y
    
        return big_x,big_y,max_seen
    
    biggest()
    # (993, 913, 906609)
    

提交回复
热议问题