Skip to content

Instantly share code, notes, and snippets.

@realsba
Created May 18, 2018 11:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save realsba/62aa1192efb47c6f9a1bb6dec31ea7db to your computer and use it in GitHub Desktop.
Save realsba/62aa1192efb47c6f9a1bb6dec31ea7db to your computer and use it in GitHub Desktop.
Find all palindromes made from the product of two 4-digit numbers
#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;
}
@realsba
Copy link
Author

realsba commented May 18, 2018

ntel(R) Core(TM) i5-4300M CPU @ 2.60GHz, 16 GB RAM

real    0m0.128s
user    0m0.109s
sys     0m0.017s

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment