-
Notifications
You must be signed in to change notification settings - Fork 1
/
problem04.c
55 lines (44 loc) · 996 Bytes
/
problem04.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <math.h>
bool isPalindrome(uint32_t num){
if(num == 0) return true;
uint8_t digits[10];
uint32_t tmpNum = num;
uint8_t digitNumber = 0;
while(tmpNum){
digits[digitNumber] = tmpNum % 10;
tmpNum /= 10;
digitNumber++;
}
if(digitNumber == 1) return true;
uint8_t numIters = digitNumber >> 1;
digitNumber--;
for(uint8_t i = 0; i != numIters; i++)
if(digits[i] != digits[digitNumber-i])
return false;
return true;
}
int main(void){
// this is the largest polidrome number which can be a product of two 3-digit numbers
uint32_t number = 997799;
uint32_t i, j;
while(number > 9999){
if(isPalindrome(number)){
for(i = 999; i != 99; i--){
if(!(number % i)){
j = number / i;
if((j > 99) && (j < 1000)){
goto getout;
}
}
}
}
number--;
}
getout:
printf("the number is %u = %u x %u\n", number, i, j);
return EXIT_SUCCESS;
}