one-file-projects/euler4.c
2015-11-22 18:19:34 +01:00

49 lines
1 KiB
C

#include <stdio.h>
#include <stdlib.h>
struct palindrome_info {
int a;
int b;
int palindrome;
};
int makepalindrome(int n) {
int p = n*1000;
p += 100 * (n%10);
p += 10 * ((n/10)%10);
p += (n/100)%10;
return p;
}
struct palindrome_info* largestpalindrome() {
struct palindrome_info* info = malloc(sizeof(struct palindrome_info));
info->a = 0;
info->b = 0;
info->palindrome = 0;
int p,a;
for(int i = 999; i > 0; i--) {
p = makepalindrome(i);
a = 999;
while(a > 0 && p%a != 0) a--;
if(a > 0 && p/a < 1000) {
info->palindrome = p;
info->a = a;
info->b = p/a;
return info;
}
}
free(info);
return NULL;
}
int main( int argc, char *argv[] ) {
struct palindrome_info* info = largestpalindrome();
if(info != NULL) {
printf("A: %d\n",info->a);
printf("B: %d\n",info->b);
printf("Palindrom: %d\n",info->palindrome);
free(info);
}
return 0;
}