Find the largest palindrome made from the product of two 3-digit numbers

前端 未结 17 995
天涯浪人
天涯浪人 2021-02-02 04:27
package testing.project;

public class PalindromeThreeDigits {

    public static void main(String[] args) {
        int value = 0;
        for(int i = 100;i <=999;i+         


        
17条回答
  •  温柔的废话
    2021-02-02 05:15

    You can use the fact that 11 is a multiple of the palindrome to cut down on the search space. We can get this since we can assume the palindrome will be 6 digits and >= 111111.

    e.g. ( from projecteuler ;) )

    P= xyzzyx = 100000x + 10000y + 1000z + 100z + 10y +x
    P=100001x+10010y+1100z
    P=11(9091x+910y+100z)
    

    Check if i mod 11 != 0, then the j loop can be subtracted by 11 (starting at 990) since at least one of the two must be divisible by 11.

提交回复
热议问题