Riverbed Interview Question
Software Engineer / DevelopersCountry: United States
Interview Type: Written Test
in particular the last equation implies:
a + d = 10
b + c = 9
hence other palindromes could be: 8812 * 9999
or 9811 * 9999 but 9901 * 9999 is the largest
#include <stdio.h>
#include <stdbool.h>
#include <math.h>
int main (int argc, char *argv[]){
bool found = false;
for (int i = 998; i >= 100; i--)
{
char j[7];
sprintf(j,"%d",i);
j[3]= j[2];
j[4]= j[1];
j[5]= j[0];
int x =atoi(j);
int limit = sqrt((float) x);
for (int z = 999; z >= limit; z--)
{
if (x%z==0){
printf("%d",x);
found = true;
break;
}
}
if (found) break;
}
}
http://stackoverflow.com/questions/3250957/finding-the-largest-palindrome-of-the-product-of-two-three-digit-numbers-problem
{
void LargestPalindrome()
{
unsigned int Res = 0;
unsigned int A = MyPow(10,7) + 1;
unsigned int B = MyPow(10,6) + MyPow(10,1);
unsigned int C = MyPow(10,5) + MyPow(10,2);
unsigned int D = MyPow(10,4) + MyPow(10,3);
for(unsigned int i = 9999; i >= 1000; i--)
{
for(unsigned int a = 9; a >= 1; a--)
{
for(int b = 9; b >= 0; b--)
{
for(int c = 9; c >= 0; c--)
{
for(int d = 9; d >= 0; d--)
{
Res = A*a + B*b + C*c + D*d;
if(Res % i == 0 && Res / i <= 9999)
{
cout << i << " " << Res << " " << Res / i << endl;
return;
}
}
}
}
}
}
}
}
bool ispalin (long num){
long newnum=0;
long tmp;
int digit;
while(num) {
digit = num%10;
num = num/10;
newnum = newnum + digit*10;
}
if (num == newnum)
return 1;
else
return 0;
}
long findlargestpalin () {
res = 0;
for (i = 9999 ; i > 1001 ; i--) {
for (j = i ; j > 1000; j = j - 11) {
if (j%11!=0) {
div = j/11;
j = div * j;
}
prod = i*j;
if (ispalin(prod) && prod > res)
res = prod;
}
}
return res;
}
i think it should be 9999*9901 = 9900 0099
- Anonymous January 28, 2012since we're looking for the largest palindrome no, it's natural to take 9999
as one of the factors.
mul by 9999 is easy, ie.: x * 9999 = x * 10000 - x
hence we need to find the largest 4-digit no, say, 'abcd', s.t.,
abcd 0000 - abcd = mnpq qpnm
from this equation one can derive that it is 9901