-
Notifications
You must be signed in to change notification settings - Fork 0
/
id0036.c
66 lines (51 loc) · 1.06 KB
/
id0036.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
56
57
58
59
60
61
62
63
64
65
66
// Licensed under the MIT License.
// Double-base Palindromes
#include "../lib/euler.h"
static bool math_is_binary_palindrome(unsigned long n)
{
unsigned long x = 0;
for (unsigned long y = n; y; y >>= 1)
{
x = (x << 1) | (y & 1);
}
return x == n;
}
static long math_odd_palindrome(long n)
{
long result = n;
for (long k = n / 10; k; k /= 10)
{
result = result * 10 + (k % 10);
}
return result;
}
static long math_even_palindrome(long n)
{
long result = n;
for (long k = n; k; k /= 10)
{
result = result * 10 + (k % 10);
}
return result;
}
int main(void)
{
long n;
long sum = 0;
clock_t start = clock();
for (long i = 1; (n = math_odd_palindrome(i)) < 1000000l; i++)
{
if (math_is_binary_palindrome(n))
{
sum += n;
}
}
for (long i = 1; (n = math_even_palindrome(i)) < 1000000l; i++)
{
if (math_is_binary_palindrome(n))
{
sum += n;
}
}
return euler_submit(36, sum, start);
}