Skip to content

Instantly share code, notes, and snippets.

@JohnMertz JohnMertz/fourth.c
Last active Nov 23, 2015

Embed
What would you like to do?
Matt Parker's Fourth Power Problem
#include <stdio.h>
int main(void){
// define variables
long long int x, fourth[32], total, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, remaining[16], solved;
total = 0;
solved = 0;
// the list of fourth powers, just for reference
for (x = 0; x < 32; x++){
fourth[x] = x*x*x*x;
printf("%i^4 = %i\n", x, fourth[x]);
total += x*x*x*x;
}
//the sum of the fourth power and the halved value that each set must reach
printf("Total = %i\nHalf of Total = %i\n",total, total/2);
//test every combination of 16 numbers until a good one is found
for (a = 0; a < 17; a++){
for (b = a+1; b < 18; b++){
for (c = b+1; c < 19; c++){
for (d = c+1; d < 20; d++){
for (e = d+1; e < 21; e++){
for (f = e+1; f < 22; f++){
for (g = f+1; g < 23; g++){
for (h = g+1; h < 24; h++){
for (i = h+1; i < 25; i++){
for (j = i+1; j < 26; j++){
for (k = j+1; k < 27; k++){
for (l = k+1; l < 28; l++){
for (m = l+1; m < 29; m++){
for (n = m+1; n < 30; n++){
for (o = n+1; o < 31; o++){
for (p = o+1; p < 32; p++){
// test condition. If the fourth powers of all above variables sum to half of the total, print them out, else try again
if (fourth[a]+fourth[b]+fourth[c]+fourth[d]+fourth[e]+fourth[f]+fourth[g]+fourth[h]+fourth[i]+fourth[j]+fourth[k]+fourth[l]+fourth[m]+fourth[n]+fourth[o]+fourth[p] == total/2){
printf("Set1: %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i, %i\nor %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i + %i = %i\n\n", a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, fourth[a], fourth[b], fourth[c], fourth[d], fourth[e], fourth[f], fourth[g], fourth[h], fourth[i], fourth[j], fourth[k], fourth[l], fourth[m], fourth[n], fourth[o], fourth[p], fourth[a]+fourth[b]+fourth[c]+fourth[d]+fourth[e]+fourth[f]+fourth[g]+fourth[h]+fourth[i]+fourth[j]+fourth[k]+fourth[l]+fourth[m]+fourth[n]+fourth[o]+fourth[p]);
//determine the unused 16 numbers and put them into an array
total = 0;
q = 0;
for (x = 0; x < 32; x++) {
if (x != a && x != b && x != c && x != d && x != e && x != f && x != g && x != h && x != i && x != j && x != k && x != l && x != m && x != n && x != o && x != p) {
remaining[q] = x;
q++;
}
}
// Mirror the printed format from set one with the remaining array
printf("Set2: ");
for (q = 0; q < 16; q++) {
printf("%i", remaining[q]);
if (q < 15) {
printf(", ");
}
}
// Mirror the calculation output summing the powers as it goes. Print confirmed sum.
printf("\nor ");
for (q = 0; q < 16; q++) {
printf("%i", fourth[remaining[q]]);
if (q < 15) {
printf(" + ");
}
total += fourth[remaining[q]];
}
printf(" = %i\n\n", total);
solved = 1;
// keep it from doing all of the rest of the comparisons for no reason
break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
if (solved == 1) break;
}
return 0;
}
@JohnMertz

This comment has been minimized.

Copy link
Owner Author

commented Nov 23, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.