Created
September 6, 2012 13:35
-
-
Save wangkuiyi/3656321 to your computer and use it in GitHub Desktop.
Find the largest palindrome made from the product of two 3-digit numbers. http://projecteuler.net/problem=4
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 <stdio.h> | |
#include <string> | |
std::string positive_to_string(int num) { | |
std::string s; | |
while (num > 0) { | |
s.push_back((char)('0' + (num % 10))); | |
num /= 10; | |
} | |
return s; | |
} | |
bool is_palindrome(int num) { | |
std::string str = positive_to_string(num); | |
for (int s = 0; s < str.length() / 2; ++s) { | |
if (str[s] != str[str.length() - s - 1]) { | |
return false; | |
} | |
} | |
return true; | |
} | |
int main() { | |
int max_palindrome = 0; | |
for (int i = 1; i < 1000; ++i) { | |
for (int j = 1; j < 1000; ++j) { | |
int p = i * j; | |
if (is_palindrome(p)) { | |
if (p > max_palindrome) { | |
max_palindrome = p; | |
} | |
} | |
} | |
} | |
printf("%d\n", max_palindrome); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment