/* Pentagonal numbers are generated by the formula, Pn=n(3nāˆ’1)/2. The first ten pentagonal numbers are: * * 1, 5, 12, 22, 35, 51, 70, 92, 117, 145, ... * * It can be seen that P4 + P7 = 22 + 70 = 92 = P8. However, their difference, 70 āˆ’ 22 = 48, is not pentagonal. * * Find the pair of pentagonal numbers, Pj and Pk, for which their sum and difference are pentagonal and D = |Pk āˆ’ Pj| is minimised; * what is the value of D?*/ #define _POSIX_C_SOURCE 199309L #include #include #include #include #include "projecteuler.h" int main(int argc, char **argv) { int n, m, pn, pm, found = 0; double elapsed; struct timespec start, end; clock_gettime(CLOCK_MONOTONIC, &start); n = 2; /* Check all couples of pentagonal numbers until the right one * is found. Use the function implemented in projecteuler.c to * check if the sum and difference ot the two numbers is pentagonal.*/ while(!found) { pn = n * (3 * n - 1) / 2; for(m = 1; m < n; m++) { pm = m * (3 * m - 1) / 2; if(is_pentagonal(pn+pm) && is_pentagonal(pn-pm)) { found = 1; break; } } n++; } clock_gettime(CLOCK_MONOTONIC, &end); elapsed = (end.tv_sec - start.tv_sec) + (double)(end.tv_nsec - start.tv_nsec) / 1000000000; printf("Project Euler, Problem 44\n"); printf("Answer: %d\n", pn-pm); printf("Elapsed time: %.9lf seconds\n", elapsed); return 0; }