Skip to content

Instantly share code, notes, and snippets.

@musoftware
Last active August 29, 2015 13:56
Show Gist options
  • Save musoftware/9020972 to your computer and use it in GitHub Desktop.
Save musoftware/9020972 to your computer and use it in GitHub Desktop.
Largest palindrome product
#include "stdafx.h"
#include <time.h>
#include <iostream>
using namespace std;
int reverse(int number);
int is_palindromic(int n);
int main()
{
int biggest = 0;
clock_t begin = clock();
for (int x = 999; x >= 899; x--)
for (int y = 999; y >= x; y--)
if (is_palindromic(x * y) > biggest)
biggest = x * y;
clock_t end = clock();
cout << "The largest palindrome is " << biggest << endl << "elapsed_secs are: " << double(end - begin) / CLOCKS_PER_SEC << endl;
system("pause");
return 0;
}
int reverse(int x)
{
int r = 0;
while (x != 0) {
r = r * 10 + x % 10; x = x / 10;
}
return r;
}
int is_palindromic(int n){
return (reverse(n) == n) ? n : 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment