/* The decimal number, 585 = 1001001001_2 (binary), is palindromic in both bases. * * Find the sum of all numbers, less than one million, which are palindromic in base 10 and base 2. * * (Please note that the palindromic number, in either base, may not include leading zeros.)*/ #define _POSIX_C_SOURCE 199309L #include #include #include #include "projecteuler.h" #define N 1000000 int main(int argc, char **argv) { int i, sum = 0; double elapsed; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); /* Brute force approach. For every number below 1 million, * check if they're palindrome in base 2 and 10 using the * function implemented in projecteuler.c.*/ for(i = 1; i < N; i += 2) { if(is_palindrome(i, 10) && is_palindrome(i, 2)) { sum += i; } } clock_gettime(CLOCK_MONOTONIC, &end); elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; printf("Project Euler, Problem 36\n"); printf("Answer: %d\n", sum); printf("Elapsed time: %.9lf seconds\n", elapsed); return 0; }