class FindMissingNumber {

    protected static Number findMultiplicationPattern(BigDecimal[] numbers) {
        if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) {
            return null;
        }
        try {

            BigDecimal gap = null;
            Number missingNumber = null;

            BigDecimal a = numbers[0];
            BigDecimal b = numbers[1];
            BigDecimal c = numbers[2];
            BigDecimal d = numbers[3];

            // Uses first four numbers to check if there is a pattern and to
            // calculate the gap between them. One missing value is allowed.
            if ( b.divide( a ).equals( c.divide( b ) ) ) {
                gap = b.divide( a );
            } else if ( c.divide( b ).equals( d.divide( c ) ) ) {
                gap = c.divide( b );
            } else if ( b.divide( a ).equals( d.divide( c ) ) ) {
                gap = b.divide( a );
            } else {
                // No pattern found.
                return null;
            }

            BigDecimal first = null;
            BigDecimal second = null;
            for ( int i = 0; i < (numbers.length - 1); i++ ) {
                first = numbers[i];
                second = numbers[i + 1];

                if ( missingNumber == null && !second.divide( first ).equals( gap ) ) {
                    missingNumber = first.multiply( gap );
                } else if ( !second.divide( first ).equals( gap ) && missingNumber != null ) {
                    // Happends if there is no pattern found, or more than 1
                    // missing number.
                    return null;
                }
            }
            return missingNumber;
        } catch ( Exception e ) {
            return null;
        }
    }

}