I am working on a project to create a system where entering a number sequence will provide me with the simplest polynomial equation for it. Now I have some problems as most novices would have by now. I am writing a method to perform the following:
Repeatedly calculate the differences between adjacent numbers in the sequence, until every number in the sequence is the same:
[1, 1, 5, 13, 25] ⇒ [0, 4, 8, 12] ⇒ [4, 4, 4]
The exponent of the new term is the number of steps above, i.e. 2.
The coefficient of the new term is the number on the final sequence divided by the factorial of the exponent, i.e. 4/2! = 2. Thus the new term is 2x^2.
What I have done is create three methods. I am not sure if this is the right way but its what I have done.
I have created a method to find the differences between the sequence thats entered and then store them in another array. (help from the awesome community here was great in this)
Secondly, I have created a method to find the factorial of a term. I am writing down the two methods below as well as a method for finding if all terms are equal.
Factorial:
public int factorial(int x) {
for (int i=1;i<=x;i++){
x = x*i;
}
return x;
}
AllEqual:
public boolean allEqual() {
boolean checker = true;
double first = sequence[1];
for (int i = 1; i<sequence.length ; i++){
if(sequence[i] == first){
checker = true;
}
}
}
Differences:
public double[] differences() {
double[] diffs = new double[sequence.length - 1];
for(int i = 0;i<=sequence.length;i++){
diffs[i] = sequence[i+1]- sequence[i];
}
return null;
}
I understand that I need to use the differences method in maybe a loop. I am not sure about this because if i use it in a loop then in then next iteration of the loop, the loop should be running on array named diffs and not sequence.