Palindrome from the product of two 3-digit numbers

后端 未结 6 1671
北海茫月
北海茫月 2020-12-20 08:12

I want to find the largest palindrome that can be obtained through the multiplication of two 3-digit numbers.

I started off with a and b both being 999, and to decre

6条回答
  •  余生分开走
    2020-12-20 08:56

    I did it and have optimized and reduced the number of steps for search

    time 0.03525909700010743

    palindrome = 906609

    count = 3748

    i = 913

    j = 993

    from timeit import timeit
    
    def palindrome(number):
        return str(number) == str(number)[::-1] # chek number is polindrome
    
    def largest():
        max_num = 0
        count = 0
        ccount = 0
        ii = 0
        jj = 0
        for i in range(999, 99, -1): # from largest to smallest
            for j in range(999,  i - 1, -1): # exclude implementation j * i
                mult = i * j # multiplication
                count += 1
                if mult > max_num and palindrome(mult): # chek conditions
                    max_num = mult #remember largest
                    ii = i
                    jj = j
                    ccount = count
        return "\npalindrome = {0}\ncount = {1}\ni = {2}\nj = {3}".format(max_num, ccount, ii, jj)
    print ("time", timeit('largest()', 'from __main__ import largest', number = 1))
    print(largest())
    

提交回复
热议问题