Created
May 18, 2018 11:06
-
-
Save realsba/62aa1192efb47c6f9a1bb6dec31ea7db to your computer and use it in GitHub Desktop.
Find all palindromes made from the product of two 4-digit numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <fstream> | |
using Type = uint64_t; | |
bool isPalindrom(Type x) | |
{ | |
Type tmp {x}, y {0}; | |
while (tmp) { | |
y *= 10; | |
y += tmp % 10; | |
tmp /= 10; | |
} | |
return x == y; | |
} | |
int main(int argc, char** argv) | |
{ | |
Type MIN=1000, MAX=9999; | |
std::ofstream os("output.txt"); | |
for (Type a=MIN; a<=MAX; ++a) { | |
if (a % 10 == 0) continue; | |
for (Type b=a; b<=MAX; ++b) { | |
Type x = a * b; | |
Type l = x > 9999999 ? x / 1000000 : x / 100000; | |
Type r = (x % 10) * 10 + (x / 10) % 10; | |
if (l == r && isPalindrom(x)) { | |
os << a << ' ' << b << ' ' << x << std::endl; | |
} | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
ntel(R) Core(TM) i5-4300M CPU @ 2.60GHz, 16 GB RAM