-
Notifications
You must be signed in to change notification settings - Fork 0
/
p_55.c
51 lines (46 loc) · 1.1 KB
/
p_55.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
#include <stdbool.h>
#include <stdio.h>
/* Find number Lychrel numbers are there below 10,000. */
unsigned long reverseNumber(unsigned long num) {
unsigned long reverse = 0;
while (num != 0) {
reverse *= 10;
reverse += num % 10;
num /= 10;
}
return reverse;
}
bool isPalindrome(unsigned long x) {
if (x < 0)
return false;
unsigned long div = 1;
while (x / div >= 10) {
div *= 10;
}
while (x != 0) {
unsigned long l = x / div;
unsigned long r = x % 10;
if (l != r)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
int main() {
int lychrel_num = 10000;
for (int i = 0; i < 10000; ++i) {
unsigned long num = i;
for (int j = 0; j < 50; ++j) {
unsigned long reverse = reverseNumber(num);
unsigned long sum = num + reverse;
if (isPalindrome(sum)) {
--lychrel_num;
break;
} else {
num = sum;
}
}
}
printf("%d\n", lychrel_num);
}