I was given this assignment:
There is one integral solution to the equation (a^5)+(b^5)+(c^5)+(d^5)+(e^5)=(f^5) that satisfies 1 <= a <= b <= c <= d <= e <= f <= 75. Find the solution.
My algorithm works btw, the answers are a = 19, b = 43, c = 46, d = 47, e = 67, f = 72.
I have done this assignment in C and Java (it has to be turned in as Java code and I don't know any good Java BBSs. I did it in C so I could find help here), but its a sucky algorithm. Its of O(n^5), maybe O(n^6). I think that its the best it can be. It is just one of those problems that requires brute force. I've looked at this for a few days and can't see a better way.
The code:
Any hints & help would be appreciated.Code:#include<stdio.h> #include<math.h> #define NOT_FOUND -1 int binarySearch(double *n, int n_size, double z) { int low = 0; int mid = 0; int high = n_size; while(low <= high) { mid = (low + high) / 2; if(n[mid] < z) { low = mid + 1; } else if(n[mid] > z) { high = mid - 1; } else { return mid; } } return NOT_FOUND; } int main(int argc, char *argv[]) { int i = 0, a = 0, b = 0, c = 0, d = 0, e = 0; int f = 0; double x[75]; for(i = 0; i < 75; i++) { x[i] = pow((i + 1), 5); } for(a = 0; a < 75; a++) for(b = a; b < 75; b++) for(c = b; c < 75; c++) for(d = c; d < 75; d++) for(e = d; e < 75; e++) { if((f = binarySearch(x, 74, (x[a] + x[b] + x[c] + x[d] + x[e]))) != NOT_FOUND) { printf("a = %d, b = %d, c = %d, d = %d, e = %d\nf = %d", (a + 1), (b + 1), (c + 1), (d + 1), (e + 1), (f + 1)); return 0; } } return 0; }
spoon_



LinkBack URL
About LinkBacks


