## Saturday, November 12, 2016

### Largest palindrome which is product of two n-digit numbers

http://www.geeksforgeeks.org/largest-palindrome-product-two-n-digit-numbers/
Given a value n, find out the largest palindrome number which is product of two n digit numbers.

Below are steps to find the required number.
1) Find a lower limit on n digit numbers. For example, for n = 2, lower_limit is 10.
2) Find an upper limit on n digit numbers. For example, for n = 2, upper_limit is 99.
3) Consider all pairs of numbers where ever number lies in range [lower_limit, upper_limit]
`int` `larrgestPalindrome(``int` `n)`
`{`
`    ``int` `upper_limit = 0;`

`    ``/* Loop to calculate upper bound(largest number`
`       ``of n-digit)*/`
`    ``for` `(``int` `i=1; i<=n; i++)`
`    ``{`
`        ``upper_limit *= 10;`
`        ``upper_limit += 9;`
`    ``}`

`    ``// largest number of n-1 digit. One plus this number `
`    ``// is lower limit which is product of two numbers.`
`    ``int` `lower_limit = 1 + upper_limit/10;`

`    ``int` `max_product = 0; ``// Initialize result`
`    ``for` `(``int` `i=upper_limit; i>=lower_limit; i--)`
`    ``{`
`        ``for` `(``int` `j=i; j>=lower_limit; j--)`
`        ``{`
`            ``// calculating product of two n-digit numbers`
`            ``int` `product = i * j;`
`            ``if` `(product < max_product)`
`                ``break``;`
`            ``int` `number = product;`
`            ``int` `reverse = 0;`

`            ``/* calculating reverse of product to check`
`               ``whether it is palindrome or not*/`
`            ``while` `(number != 0)`
`            ``{`
`                ``reverse = reverse * 10 + number % 10;`
`                ``number /= 10;`
`            ``}`

`            ``/* update new product if exist and if`
`               ``greater than previous one*/`
`            ``if` `(product == reverse && product > max_product)`
`                ``max_product = product;`
`        ``}`
`    ``}`
`    ``return` `max_product;`
`}`

http://www.geeksforgeeks.org/largest-palindrome-product-two-n-digit-numbers/