I wrote this code to find the variables that will satisfy the diophantine equation (i.e. a^4 + b^4 + c^4 = d^4), for all variables belong to: (0 , 430000), and it takes forever to find the answer (I left the code running for two hours and got nothing). I know that this is caused by the algorithm or the approach I took, but I really couldn’t think of any better solution…
here’s the code:
#include <iostream> using namespace std; void main() { //Here I use double because max value for int variables is too small for what I need. double n = 0, LHS, RHS, valA, valB, valC, valD; double *arrA, *arrB, *arrC, *arrD; arrA = new double[430000]; //array to store all the powers of 4 for var A arrB = new double[430000]; //array to store all the powers of 4 for var B arrC = new double[430000]; //array to store all the powers of 4 for var C arrD = new double[430000]; //array to store all the powers of 4 for var D for (int count = 0; count < 430000; count++) { arrA[count] = n*n*n*n; arrB[count] = n*n*n*n; arrC[count] = n*n*n*n; arrD[count] = n*n*n*n; n++; } //this loop is a counter, it increments "b" by 1 when "a" reaches max value, and so on for each variable... /* N.B: The variables that satisfy the equation should be the following: a = 95800 b = 217519 c = 414560 d = 422481 Plug it in as initializers for the variables to check that the program is working fine. */ int a = 0, b = 0, c = 0, d = 0; while (d < 430000) { if (a == 430000) { a = 0; b++; } if (b == 430000) { a = 0; b = 0; c++; } if (c == 430000) { a = 0; b = 0; c = 0; d++; } if (d == 430000) { break; } LHS = arrA[a] + arrB[b] + arrC[c]; RHS = arrD[d]; if (LHS == RHS && (arrA[a] != arrB[b] != arrC[c] != arrD[d])) { valA = a; valB = b; valC = c; valD = d; break; } a++; } if (d == 430000){ cout << "Couldn't find an answer! \n" } else { cout << "value of A is: " << valA << endl; cout << "value of B is: " << valB << endl; cout << "value of C is: " << valC << endl; cout << "value of D is: " << valD << endl; } }
If anyone could tell me how to speed up the code or could think of a better algorithm. There’s nothing wrong (IMO) in the code’s syntax or logic, but it is very very slow…
N.B: here’s a link to an article regarding the equation for further reading: https://www.jstor.org/stable/2008781?seq=1#page_scan_tab_contents
I’m using a 2013 macbook pro “running windows 10”, (intel i5 2.4GHz, 8GB RAM), MS Visual Studio 2017…