-
Notifications
You must be signed in to change notification settings - Fork 0
/
problem4.cpp
50 lines (44 loc) · 982 Bytes
/
problem4.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;
int nextLowestPalind(int val)
{
int currPalin = val;
int tens = (currPalin/10)%10;
int hundreds = (currPalin/100)%10;
if(hundreds!=0)
currPalin-=1100;
else
{
if(tens != 0)
currPalin -=110;
else
{
currPalin -=11;
}
}
return currPalin;
}
int main()
{ //999 * 999 = 998001
//assume answer is 6 digits
int currPalin = 997799;
int maxfactor = 999;
int minfactor = 100;
int x = maxfactor;
while(currPalin > 99999)//if reaches 5 digits my assumption failed
{
if((currPalin/x) > maxfactor || x < minfactor)
{
currPalin = nextLowestPalind(currPalin);
x = maxfactor;
}
else if(currPalin%x == 0)
{
cout<<x<<" * "<<currPalin/x<<" = "<< currPalin << "\n";
return 0;
}
else
x--;
}
cout<<"FAIL\n";
}