project36

Problem 36</h2>

31 January 2003</div>

The decimal number, 585 = 10010010012</sub> (binary), is palindromic in both bases.

Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2.

(Please note that the palindromic number, in either base, may not include leading zeros.)</p>

</div>
</div>
 

Answer:</div></td>

872187</b></td>
</tr>
</tbody>
</table>
#include
using namespace std;
int digits[6];
int bins[20];
int binNum=0;

bool ispaldec(long i)
{
int j=0;
for (; i>0; i/=10)
digits[j++]=i%10;
for (i = 0; i < j/2; i++) {
if(digits[i]!=digits[j-i-1])
return false;
}
return true;
}
/*
* alternaltive way recursive
void ispaldectobin(long i)
{
binNum=0;
if(i<=1)
{
bins[binNum++]=i;
return;
}
ispaldectobin(i/2);
bins[binNum++]=i%2;
}
*/
//while loop
void ispaldectobin(long i)
{
binNum=0;
do {
bins[binNum++]=i%2;
i/=2;
}while(i);
}
bool ispalbin(int *bins)
{
int i=0;
for (i = 0; i < binNum/2; i++) {
if(bins[i]!=bins[binNum-i-1])
return false;
}
return true;
}
int main (int argc, char const* argv[]) {
int i=1,j=0,sum=0;
for (i = 1; i < 1000000; i++) {
ispaldectobin(i);
if(ispalbin(bins)&&ispaldec(i))
{
ispaldectobin(i);
sum+=i;
}
}
cout< return 0;
}