/** * Sequence models sequences. * * @author Lyndon While * @version 1.0 */ import java.util.ArrayList; public class Sequence { // the numbers in the sequence private ArrayList sequence; // sets up sequence by parsing s // e.g. Sequence("3, -4, 8.5") sets sequence to <3, -4, 8.5> public Sequence(String s) { // can't be TODO because of the array returned by split? sequence = new ArrayList<>(); for (String x : s.split(",")) sequence.add(Double.parseDouble(x)); } // returns sequence public ArrayList getSequence() { return sequence; } // returns the product of 1..x // e.g. factorial(4) = 1 * 2 * 3 * 4 returns 24 public int factorial(int x) { // TODO int soln = 1; for (int k = 2; k <= x; k++) soln = soln * k; return soln; } // returns true iff all of the items on seq are equal // e.g. allEqual(<4, 4, 4>) returns true, and allEqual(<3, 3, -2>) returns false public boolean allEqual(ArrayList seq) { // TODO // for (double d : seq) if (d != seq.get(0)) return false; double d = seq.get(0); for (int i = 1; i < seq.size(); i++) if (d != seq.get(i)) return false; return true; } // returns a new ArrayList holding the differences between adjacent items on seq // e.g. differences(<4, 6, 1, 1>) returns <2, -5, 0> public ArrayList differences(ArrayList seq) { // TODO ArrayList diffs = new ArrayList<>(); for (int k = 0; k < seq.size() - 1; k++) diffs.add(seq.get(k+1) - seq.get(k)); return diffs; } // returns the next term in the simplest polynomial that generates sequence // implements Steps 1-3 of the algorithm description on the project web page public Term nextTerm() { // TODO int exponent = 0; ArrayList temp = sequence; while (!allEqual(temp)) { temp = differences(temp); exponent++; } return new Term(temp.get(0) / factorial(exponent), exponent); } // returns the value to subtract from the kth index of term t // e.g. termUpdate(Term<2, 3>, 4) returns 128 public double termUpdate(Term t, int k) { // TODO return t.getCoefficient() * Math.pow(k, t.getExponent()); } // subtracts from each item in sequence the effect of the term t // implements Step 4 of the algorithm description on the project web page public void updateSequence(Term t) { // TODO for (int k = 0; k < sequence.size(); k++) sequence.set(k, sequence.get(k) - termUpdate(t, k + 1)); } // returns the simplest polynomial that generates sequence // implements the algorithm description on the project web page // and also displays the polynomial as a String public Polynomial solveSequence() { // TODO Polynomial soln = new Polynomial(); while (!allEqual(sequence)) { Term t = nextTerm(); soln.addTerm(t); updateSequence(t); } if (soln.numberOfTerms() == 0 || sequence.get(0) != 0) soln.addTerm(new Term(sequence.get(0), 0)); System.out.println(soln.displayPolynomial()); return soln; } // reads the file filename, solves the sequences therein, and displays the results public static void solveFileSequences(String filename) { for (String s : (new FileIO(filename)).lines) if (s.length() > 0 && "0123456789-".indexOf(s.charAt(0)) != -1) { System.out.print("Sequence: " + s + "\nPolynomial: "); (new Sequence(s)).solveSequence(); System.out.println(); } } }