Filename: prob44.c
/*Pentagonal numbers are generated by the formula, Pn=n(3n1)/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 is pentagonal and D = |Pk Pj| is minimised; what is the value of D? */ #include <stdio.h> #include <math.h> typedef unsigned long long number; inline number pentagonal(const number n) { return n*(3*n-1)/2; } inline number diff(const number x, const number y) { if(x>y) { return x-y; } else { return y-x; } } inline int isPentagonal(const number x) { // if(ceil((1+sqrt(1-24*x))/6) == floor((1+sqrt(1-24*x))/6)) // { // return 1; // } // else // { // return 0; // } int i; for(i=1; pentagonal(i) < x; ++i) ; return pentagonal(i) == x; } inline int meetsCriteria(const number x, const number y) { return isPentagonal(x+y) && isPentagonal(diff(x,y)); } int main() { int x = 1; int y = 1; // printf("pentagonal(20000) = %llu\n", pentagonal(20000)); // return 0; for(x=1; ; ++x) { for(y=1; y<=x; ++y) { if(meetsCriteria(pentagonal(x), pentagonal(y))) { printf("pentagonal(x)=%lld, pentagonal(y)=%lld, diff(x,y)=%lld\n", pentagonal(x), pentagonal(y), diff(pentagonal(x), pentagonal(y))); return 0; } // if(x % 100 == 0 || y % 100 == 0) // { // printf("(x,y) : (%d,%d)\n", x, y); // } } } return 0; }
syntax highlighted by Code2HTML, v. 0.9.1
No comments:
Post a Comment