Skip to content

Instantly share code, notes, and snippets.

@CraigRodrigues
Created August 12, 2016 01:01
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 CraigRodrigues/2f5dee93078abf2ed5c456e200957048 to your computer and use it in GitHub Desktop.
Save CraigRodrigues/2f5dee93078abf2ed5c456e200957048 to your computer and use it in GitHub Desktop.
Project Euler 4 - Largest Palindrome Product
#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;
}
@CraigRodrigues
Copy link
Author

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

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