Project Euler Homework
|
00001 #include <iostream> 00002 using namespace std; 00003 00004 unsigned 00005 reverse_bin (unsigned x) 00006 { 00007 unsigned y = x & 1; 00008 x >>= 1; 00009 while (x > 0) 00010 { 00011 y = (y << 1) + (x & 1); 00012 x >>= 1; 00013 } 00014 return y; 00015 } 00016 00017 unsigned 00018 reverse_dec (unsigned x) 00019 { 00020 unsigned y = x % 10; 00021 x /= 10; 00022 while (x > 0) 00023 { 00024 y = (y * 10) + (x % 10); 00025 x /= 10; 00026 } 00027 return y; 00028 } 00029 00030 int 00031 main () 00032 { 00033 unsigned sum = 0; 00034 00035 for (unsigned k = 1; k < 1000000; k += 2) 00036 if (reverse_bin (k) == k) 00037 if (reverse_dec (k) == k) 00038 sum += k; 00039 00040 cout << sum << endl; 00041 return 0; 00042 } 00043