@Override
    public Polynomial gcd(Polynomial polynomial1, Polynomial polynomial2) {

        if (!isZero(polynomial1) && isZero(polynomial2)) {
            return polynomial1;
        }
        if (isZero(polynomial1) && !isZero(polynomial2)) {
            return polynomial2;
        }
        if (isZero(polynomial1) && isZero(polynomial2)) {
            return new Polynomial(0);
        }

        Polynomial result = new Polynomial(polynomial2.getSize());
        Polynomial remainder = polynomial2;
        Polynomial numerator = polynomial1;
        Polynomial denumerator = polynomial2;


        //find greatest greatest common divisor, last positive remainder
        while (!(remainder.getSize() == 0)) {
            result = remainder;
            remainder = new Polynomial(Math.max(numerator.getSize(), denumerator.getSize()));
            divide(numerator, denumerator, remainder);
            remainder.clearZeroValues();
            numerator = denumerator;
            denumerator = remainder;
        }

        //make gcd monic
        Polynomial monicDiv = new Polynomial(1);
        monicDiv.setElement(0, result.getElement(result.getSize() - 1));
        return divide(result, monicDiv);

    }