Q) A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 * 99. Find the largest palindrome made from the product of two 3-digit numbers.
Lets take the case: largest palindrome made from the product of two single-digit numbers.
what will be the smallest product of two single-digit numbers?
of-course the product of two smallest-single-digit numbers ie. 0 and 0 ie. 0-
and the largest product?
product of two largest-single-digit numbers ie. 9 and 9 ie. 81
Now we are interested in only palindrome numbers, that too between the above found numbers
( 0 and 81 )List of palindrome numbers between 0 and 81: 1, 11, 22, 33, 44, 55, 66, 77
Note: 88 and 99 won't get into the list.
since we are looking for largest palindrome number which will be the product of two single-digit numbers,
let's go through the list from highest to lowest and with each number in the list, we will check if it is
a product of two one digit number.
We will stop traversing through the list of numbers when we find such a number.
Now let's do a little optimization to our logic. Why do we need to generate a list of all palindrome numbers
below 81?
So first we will find the immediate palindrome number below 81, will check if it suffice our need,
if not go for next.
Better.. , isn't it?
The same logic can be scaled up to find largest palindrome made from the product of two n-digit numbers.
We make use of a function in our code to divide the task.
"palindromeNumJustBelow ( input )":
returns the palindrome number just below the argument.
Rather than a brute-force search I use a few heuristics here:
A palindrome number is a smaller number combined with the same number reversed.
Eg:
1221 = 12 combined with reverse of 12 ie 21
765567 = 765 combined with reverse(765)
In case of odd number of digits, it will be similar to above case but a digit in between.
Eg:
12821 = '12' + a digit(here '8') + reverse(12)
12321 = '12' + '3' + '21'
So a palindrome less than a number can be found by splitting the number into two sub-parts:
In case of even number of digits:
two numbers of half the number of digits ie 1221 into 12 and 21, 765567 into 765 and 567
In case of odd number of digits:
the small number and middle digit forms first number and reversed number forms the second
number ie 12821 into 128 and 21, 12321 into 123 and 21.
Now the first number is decremented by 1. Eg: 12 of 1221 becomes 11, 765 of 765567 becomes 764
128 of 12821 becomes 127, etc
Now we just need to take the reverse of particular digits of this new number and append to the new
number to get the required palindrome number.
ie. in case of even number of digits, just reverse the new number.
in case of odd number of digits, reverse all digits except the last.
Lets try our logic with a few numbers to ensure its correctness:
1) Find the palindrome number just below 139890?
This number has even number of digits ( 6 digits)
It is split into two sub-parts: 139 and 890
Now the first number is decremented by 1 and becomes 138.
Now the reverse of new number is appended with the new number to form the final answer.
ie '138' + '831' = 138831 (Note: + doesn't mean mathematical addition)
You may have a doubt now, why the first number is reduced and not the second number.
If we reduce the second number and prepend with the reverse of new number, we wont get
the expected result. In this case (831 - 1) = 830, '038' + '830' = 038830. Is this what
we wanted?
A BIG NO... !
Now with a number of odd number of digits:
2) 1637001
yes, you are right, 7 digits
right.., split into 1637 and 001
Note: 001 wont be considered as 1
1637 is decremented by 1 and becomes 1636
Now, since odd number of digits, only 1, first 6 and 3 is reversed and appended.
So final answer becomes: 1636361
Lets go for one more
3) 13300029212
11 digits.
133000 and 29212
133000 - 1 = 132999
'132999' + '99231' = 13299999231
"palindromeNumJustBelow ( long input )" implementation:
First, I insert each digit of the argument ie input into a vector. so the vector can be seen as an array
of digits. This is for my easiness to handle the digits of the number. Since I use the push_back() function
of the vector, The order of the digits will be reversed. Then I set a variable numOfDigits to number of digits
in the number. Then a new number ie 'temp_input_half' is generated with last
half of digits (Note: last half digits means first half digits of the number, don't get confused).
'temp_input_half' is decremented by 1.
The second half digits in the vector are replaced with the digits of new value of 'temp_input_half'
Now our final array of digits is ready; we just need to form the final number out of it.
I have used two lambda functions in the code. It's just a way to define a function somewhere just for one time
use. I use those with for_each loop.
At the top of the program, num_of_digits_in_num is defined as 3, ie. it should be a multiple of two 3-digit
numbers.
In the main function:
I find
smallest 3-digit number
( to avoid hard-coding, we use 'num_of_digits_in_num',
so when i say 3 assume that I used 'num_of_digits_in_num' in the code)
largest 3-digit number
Then the execution gets into a while loop which exits if the "palindrome_num" (palindrome number) is less than
the product of two smallest-3-digit-number. Product of two 3-digit-number should be between product of two
smallest 3-digit number and product of two largest 3-digit number. It's simple mathematics.
Note: first the "palindrome_num" is set to product of two largest 3-digit number before the while-loop
Inside while-loop, we find a number just below the current "palindrome_num" and we use another for-loop which
iterates from largest 3-digit number to smallest 3-digit number and in each iteration it is checked if it's
possible to divide the palindrome number with the iteration variable (in our code, "temp_div1") and still get
a 3-digit number
Largest palindrome made from the product of two 3-digit numbers is 906609 = 993 * 913
Code for reference:
#include <iostream>
#include <cmath>
#include <algorithm>
#include <iterator>
#include <vector>
#include <boost/lambda/lambda.hpp>
#include <boost/functional.hpp>
#include <boost/function.hpp>
#define num_of_digits_in_num 3
using namespace std;
using namespace boost::lambda;
long palindromeNumJustBelow ( long input ) {
vector inputAsArray; //array of single-digit-numbers
long temp = input;
while ( temp ) {
inputAsArray.push_back ( temp%static_cast(10) );
temp = floor( temp/static_cast(10) );
}
int numOfDigits = inputAsArray.size();
temp = floor ( ( inputAsArray.size()/2 ) ) ;
long temp_input_half = 0;
boost::function f2 = ( var(temp_input_half)*=10, var(temp_input_half)+=boost::lambda::_1);
for_each ( inputAsArray.rbegin(), inputAsArray.rend()-temp, f2 );
temp_input_half--;
long temp1 = temp;
while ( temp_input_half ) {
inputAsArray [ temp1++ ] = ( temp_input_half%static_cast(10) );
temp_input_half = floor( temp_input_half/static_cast(10) );
}
// mirror all the digits from index temp till end
while ( temp < numOfDigits ) {
inputAsArray [ numOfDigits - temp -1 ] = inputAsArray [ temp ] ;
temp ++ ;
}
temp = 0;
boost::function f1 = ( var(temp)*=10, var(temp)+=boost::lambda::_1);
for_each ( inputAsArray.rbegin(), inputAsArray.rend(), f1 );
return temp;
}
int main ( int argc, char * argv[] ) {
long num_min, num_max;
if ( num_of_digits_in_num==1 ) {
num_min = 0;
}
else {
num_min = pow( static_cast(10), static_cast(num_of_digits_in_num-1) );
}
num_max = (pow( static_cast(10), static_cast(num_of_digits_in_num) ) - 1 );
long prod_min, prod_max;
prod_min = num_min*num_min;
prod_max = num_max*num_max;
long palindrome_num = prod_max;
long temp_div2;
bool ans_found = false;
while ( palindrome_num > prod_min && !ans_found) {
palindrome_num = palindromeNumJustBelow(palindrome_num);
for (int temp_div1 = num_max; temp_div1 >= num_min && !ans_found; temp_div1-- ) {
temp_div2 = palindrome_num/temp_div1;
if ( palindrome_num % temp_div1 == 0 && temp_div2 <= num_max && temp_div2 >= num_min ) {
cout << "Largest palindrome made from the product of two "
<< num_of_digits_in_num << "-digit numbers is " << palindrome_num
<< " = " << temp_div1 << " * " << temp_div2 << endl;
ans_found = true;
}
}
}
}
Like this:
Like Loading...
Related