Created
August 12, 2016 01:01
-
-
Save CraigRodrigues/2f5dee93078abf2ed5c456e200957048 to your computer and use it in GitHub Desktop.
Project Euler 4 - Largest Palindrome Product
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 <math.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdbool.h> | |
bool isPalindrome(int num); | |
int main(void) | |
{ | |
int max = 0; | |
int num; | |
for (int i = 999; i >= 100; i--) | |
{ | |
for (int j = i; j >= 100; j--) | |
{ | |
num = i*j; | |
if (isPalindrome(num) && num > max) | |
{ | |
max = num; | |
} | |
} | |
} | |
printf("%i\n", max); | |
} | |
bool isPalindrome(int num) | |
{ | |
int reverse = 0; | |
int n = num; | |
while (n != 0) | |
{ | |
reverse *= 10; | |
reverse += n%10; | |
n /= 10; | |
} | |
if (reverse == num) | |
return true; | |
else | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Decided to just straight brute force it assuming it wouldn't actually take that long to run through all the products.
https://projecteuler.net/problem=4