public Polynomial[] Divide(Polynomial polynomial) { var newLength = coefficients.Length; var quotient = new Polynomial(new int[newLength]); var dividend = this; var divisorGreatestPower = polynomial.GreatestPower(); var dividendGreatestPower = dividend.GreatestPower(); //only divide if divisable while (dividendGreatestPower >= divisorGreatestPower) { var powerNeeded = dividendGreatestPower - divisorGreatestPower; var coefficientNeeded = dividend.coefficients[dividendGreatestPower] / polynomial.coefficients[divisorGreatestPower]; //correct sign coefficientNeeded = dividend.coefficients[dividendGreatestPower] > 0 ? Math.Abs(coefficientNeeded) : -1 * Math.Abs(coefficientNeeded); //build new polynomial var quotientCoefficient = new int[newLength]; quotientCoefficient[powerNeeded] = coefficientNeeded; var partialQuotient = new Polynomial(quotientCoefficient); //add onto the quotient quotient = quotient.Sum(partialQuotient); //multiply new quotient into divisor and subtract from current //dividend for new dividend dividend = dividend.Difference(polynomial.Product(partialQuotient)); //calculate top power of new dividend. dividendGreatestPower = dividend.GreatestPower(); } return(new Polynomial[] { quotient, dividend }); }