@Test public void testfindSumPattern() { assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(2), BigDecimal.valueOf(6), BigDecimal.valueOf(8), BigDecimal.valueOf(10) }).doubleValue() == 4); assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(10), BigDecimal.valueOf(20), BigDecimal.valueOf(30), BigDecimal.valueOf(40), BigDecimal.valueOf(60), BigDecimal.valueOf(70) }).doubleValue() == 50); assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(110), BigDecimal.valueOf(176), BigDecimal.valueOf(242), BigDecimal.valueOf(374) }).doubleValue() == 308); assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(10), BigDecimal.valueOf(6), BigDecimal.valueOf(4), BigDecimal.valueOf(2) }).doubleValue() == 8); assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(663), BigDecimal.valueOf(326), BigDecimal.valueOf(-348), BigDecimal.valueOf(-685) }).doubleValue() == -11); assertTrue(FindMissingNumber.findSumPattern( new BigDecimal[] { BigDecimal.valueOf(4443), BigDecimal.valueOf(4412), BigDecimal.valueOf(4381), BigDecimal.valueOf(4319) }).doubleValue() == 4350); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(1), BigDecimal.valueOf(2), BigDecimal.valueOf(4), BigDecimal.valueOf(6), BigDecimal.valueOf(8), BigDecimal.valueOf(11) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(5), BigDecimal.valueOf(3), BigDecimal.valueOf(54353), BigDecimal.valueOf(54554), BigDecimal.valueOf(232), BigDecimal.valueOf(123) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(2), BigDecimal.valueOf(4), BigDecimal.valueOf(6), BigDecimal.valueOf(8), BigDecimal.valueOf(10), BigDecimal.valueOf(12), BigDecimal.valueOf(14) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(10), BigDecimal.valueOf(20), BigDecimal.valueOf(30), BigDecimal.valueOf(40), BigDecimal.valueOf(50), BigDecimal.valueOf(60) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(-15), BigDecimal.valueOf(-10), BigDecimal.valueOf(-5), BigDecimal.valueOf(0), BigDecimal.valueOf(5), BigDecimal.valueOf(10), BigDecimal.valueOf(15) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(2), BigDecimal.valueOf(4), BigDecimal.valueOf(6) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal.valueOf(2), BigDecimal.valueOf(4) }) == null); assertTrue(FindMissingNumber.findSumPattern(new BigDecimal[] { BigDecimal .valueOf(2) }) == null); }
protected static Number findSumPattern(BigDecimal[] numbers) { if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) { return null; } BigDecimal gap = null; Number missingNumber = null; BigDecimal a = numbers[0]; BigDecimal b = numbers[1]; BigDecimal c = numbers[2]; BigDecimal d = numbers[3]; if ( b.subtract( a ).equals( c.subtract( b ) ) ) { gap = b.subtract( a ); } else if ( c.subtract( b ).equals( d.subtract( c ) ) ) { gap = c.subtract( b ); } else if ( b.subtract( a ).equals( d.subtract( c ) ) ) { gap = b.subtract( a ); } else { return null; } for ( int i = 0; i < (numbers.length - 1); i++ ) { BigDecimal first = numbers[i]; BigDecimal second = numbers[i + 1]; if ( missingNumber == null && !second.subtract( first ).equals( gap ) ) { missingNumber = second.subtract( gap ); } else if ( !second.subtract( first ).equals( gap ) && missingNumber != null ) { return null; } } return missingNumber; }
FindMissingNumber { protected static Number findSumPattern(BigDecimal[] numbers) { if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) { return null; } BigDecimal gap = null; Number missingNumber = null; BigDecimal a = numbers[0]; BigDecimal b = numbers[1]; BigDecimal c = numbers[2]; BigDecimal d = numbers[3]; if ( b.subtract( a ).equals( c.subtract( b ) ) ) { gap = b.subtract( a ); } else if ( c.subtract( b ).equals( d.subtract( c ) ) ) { gap = c.subtract( b ); } else if ( b.subtract( a ).equals( d.subtract( c ) ) ) { gap = b.subtract( a ); } else { return null; } for ( int i = 0; i < (numbers.length - 1); i++ ) { BigDecimal first = numbers[i]; BigDecimal second = numbers[i + 1]; if ( missingNumber == null && !second.subtract( first ).equals( gap ) ) { missingNumber = second.subtract( gap ); } else if ( !second.subtract( first ).equals( gap ) && missingNumber != null ) { return null; } } return missingNumber; } }
FindMissingNumber { protected static Number findSumPattern(BigDecimal[] numbers) { if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) { return null; } BigDecimal gap = null; Number missingNumber = null; BigDecimal a = numbers[0]; BigDecimal b = numbers[1]; BigDecimal c = numbers[2]; BigDecimal d = numbers[3]; if ( b.subtract( a ).equals( c.subtract( b ) ) ) { gap = b.subtract( a ); } else if ( c.subtract( b ).equals( d.subtract( c ) ) ) { gap = c.subtract( b ); } else if ( b.subtract( a ).equals( d.subtract( c ) ) ) { gap = b.subtract( a ); } else { return null; } for ( int i = 0; i < (numbers.length - 1); i++ ) { BigDecimal first = numbers[i]; BigDecimal second = numbers[i + 1]; if ( missingNumber == null && !second.subtract( first ).equals( gap ) ) { missingNumber = second.subtract( gap ); } else if ( !second.subtract( first ).equals( gap ) && missingNumber != null ) { return null; } } return missingNumber; }  }
FindMissingNumber { protected static Number findSumPattern(BigDecimal[] numbers) { if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) { return null; } BigDecimal gap = null; Number missingNumber = null; BigDecimal a = numbers[0]; BigDecimal b = numbers[1]; BigDecimal c = numbers[2]; BigDecimal d = numbers[3]; if ( b.subtract( a ).equals( c.subtract( b ) ) ) { gap = b.subtract( a ); } else if ( c.subtract( b ).equals( d.subtract( c ) ) ) { gap = c.subtract( b ); } else if ( b.subtract( a ).equals( d.subtract( c ) ) ) { gap = b.subtract( a ); } else { return null; } for ( int i = 0; i < (numbers.length - 1); i++ ) { BigDecimal first = numbers[i]; BigDecimal second = numbers[i + 1]; if ( missingNumber == null && !second.subtract( first ).equals( gap ) ) { missingNumber = second.subtract( gap ); } else if ( !second.subtract( first ).equals( gap ) && missingNumber != null ) { return null; } } return missingNumber; }  static Number testForPattern(Collection<NumberRestriction> restrictions); }
FindMissingNumber { protected static Number findSumPattern(BigDecimal[] numbers) { if ( numbers == null || numbers.length < MIN_NUMBER_OF_RESTRICTIONS ) { return null; } BigDecimal gap = null; Number missingNumber = null; BigDecimal a = numbers[0]; BigDecimal b = numbers[1]; BigDecimal c = numbers[2]; BigDecimal d = numbers[3]; if ( b.subtract( a ).equals( c.subtract( b ) ) ) { gap = b.subtract( a ); } else if ( c.subtract( b ).equals( d.subtract( c ) ) ) { gap = c.subtract( b ); } else if ( b.subtract( a ).equals( d.subtract( c ) ) ) { gap = b.subtract( a ); } else { return null; } for ( int i = 0; i < (numbers.length - 1); i++ ) { BigDecimal first = numbers[i]; BigDecimal second = numbers[i + 1]; if ( missingNumber == null && !second.subtract( first ).equals( gap ) ) { missingNumber = second.subtract( gap ); } else if ( !second.subtract( first ).equals( gap ) && missingNumber != null ) { return null; } } return missingNumber; }  static Number testForPattern(Collection<NumberRestriction> restrictions); static final int MIN_NUMBER_OF_RESTRICTIONS; }