0

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:

  1. Repeatedly calculate the differences between adjacent numbers in the sequence, until every number in the sequence is the same:

  2. [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.

  3. 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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
  • 3
    Try to stop working with object's fields and start passing parameters to your methods instead, and return results. You'll see how your code becomes _hugely_ more transparent and composable. – 9000 Apr 16 '15 at 17:54
  • Um. I am not sure how i can do that. But i appreciate your response and will certainly try to do that. Thankyou – Maktoom Ahmad Apr 16 '15 at 17:59
  • consider `boolean allEquals(double[] sequence)`, `double[] differences(double[] sequence)` and then something like `new_sequence = differences(old_sequence); if (allEquals(new_sequence)) {...}`. – 9000 Apr 16 '15 at 18:19

1 Answers1

1

Well, one nice thing would be to change your differences method so it works more or less like this:

public double[] differences(double[] sequence) {
    double[] diffs = new double[sequence.length - 1];
    for(int i = 0;i<=sequence.length;i++){
        diffs[i] = sequence[i+1]- sequence[i];
    }
    return diffs;
}

By doing so we transformed this method into a function without side effect. It only works with the provided parameters and return some value (it has referential integrity). As metioned above in one of the comments there is a huge advantage in this approach. You can now reuse and compose calls to this function in many different ways without it to be tied up to certain field.

Now you need to calculate differences over a certain sequence until all the values are equal and return the amount of steps that took to do that. You can use a recursive solution here

public int stepsUntilEqual(double[] sequence) {
   if (allEqual(sequence)) {
      return 0;
   } else {
      double[] diffs = differences(sequence);
      return 1 + stepsUntilEqual(diffs); //One more steps that the steps needed to get the differences equal
   }
}

You will notice that the "allEqual" function also has to change to use parameters instead of fields

public boolean allEqual(double[] sequence) {
   ...
}

A non recursive solution Since accessing to the last "difference" array is needed maybe a non recursive solution is more clear in this case:

   double[] lastSequence = sequence;
   int steps = 0;
   while ( !allEqual(lastSequence)) {
      lastSequence = differences(lastSequence);
      steps++;
   }

   //Now steps contains the number of iterations
   //lastSequence contains the value you will to use for calculating the coefficient
Claudio
  • 1,848
  • 12
  • 26
  • BTW: Your allEqual function is also wrong (you are not returning anything) so I bet there is some work to do there. – Claudio Apr 16 '15 at 18:08
  • Thankyou so much. I have been in a real jam over this project and i can't believe how helpful and nice this community is. You guys are bloody amazing. Thankyou again. I appreciate this. – Maktoom Ahmad Apr 16 '15 at 18:14
  • Also. A tiny question. How would i access the number that was on the last sequence? as that would be the exponent. – Maktoom Ahmad Apr 16 '15 at 18:22
  • You are right. In that case a recursive function (stepsUntilEqual) is not such a nice solution because you would need it to return more than one thing (which is possible, but you will need to return some kind of structure and complicate the whole thing). A non recursive approach (see my edit) is simpler. – Claudio Apr 16 '15 at 18:28
  • Thankyou for doing that. Another question. Sorry. I feel quite stupid. Anyway, How would i be able to store that in a variable. Just use lastSequence? and i dont really understand what a recursive approach and a non recursive approach is. I am about to google it but it would be nice to hear in a human beings words rather than an encyclopedia. Thanks again. your awesome. – Maktoom Ahmad Apr 16 '15 at 18:32
  • Also. it gives me an error and i am not really sure why. it says method allEqual in class Sequence cannot be applied to given layers; Required: no arguments; found:double[]; so yeah. – Maktoom Ahmad Apr 16 '15 at 18:42
  • Did you add the parameter to the method allEqual? It sounds like it is expecting none. – Claudio Apr 16 '15 at 18:44
  • Lots of human beigns debated that in here: http://stackoverflow.com/questions/717725/understanding-recursion. Anyways, I bet you can forget about the "recursive" solution I gave you (the stepsUntilEqual function). The other piece of code does the same thing without recurssion. And yes, now you have the steps and the lastSequence variables, just use them – Claudio Apr 16 '15 at 18:46
  • Hi. Sorry. again one more problem. when i divide lastSequence/factorial(steps) .. that does not work due to the fact that i am dividing double[] by double. so .. how can i solve this? – Maktoom Ahmad Apr 16 '15 at 19:00
  • What do you want to do? Divide the whole array or just one number of it? – Claudio Apr 16 '15 at 19:00
  • Oh. Only the one number. As all numbers should be the same right now. Right? – Maktoom Ahmad Apr 16 '15 at 19:10