private static long countNumPatty ( int N , long X ) {
  if ( X == 0 ) {
    return 0L ;
  }
  final long burgerLength = burgerLength ( N ) ;
  boolean isWholeBurgerEaten = X >= burgerLength ;
  if ( isWholeBurgerEaten ) {
    return numPatty ( N ) ;
  }
  else {
    boolean isOnlyTopSliceRemaining = X == burgerLength - 1 ;
    final long oneSmallerLayerLength = burgerLength ( N - 1 ) ;
    final long oneSmallerLayerPatty = numPatty ( N - 1 ) ;
    if ( isOnlyTopSliceRemaining ) {
      return numPatty ( N ) ;
    }
    else {
      boolean canFinishHalfBurger = X >= ( burgerLength - FIRST_SLICE_IS_BUN - oneSmallerLayerLength ) ;
      if ( canFinishHalfBurger ) {
        return oneSmallerLayerPatty + MIDDLE_PATTY + countNumPatty ( N - 1 , X - FIRST_SLICE_IS_BUN - oneSmallerLayerLength - MIDDLE_PATTY ) ;
      }
      else {
        boolean canFinishOneSmallerLayer = ( X - FIRST_SLICE_IS_BUN ) >= ( FIRST_SLICE_IS_BUN + oneSmallerLayerLength ) ;
        if ( canFinishOneSmallerLayer ) {
          return oneSmallerLayerPatty ;
        }
        else {
          boolean canEatSomePatty = X > FIRST_SLICE_IS_BUN ;
          if ( canEatSomePatty ) {
            return countNumPatty ( N - 1 , X - FIRST_SLICE_IS_BUN ) ;
          }
          else {
            return 0L ;
          }
        }
      }
    }
  }
}

private static long countNumPatty ( int N , long X ) { if ( X == 0 ) { return 0L ; } final long burgerLength = burgerLength ( N ) ; boolean isWholeBurgerEaten = X >= burgerLength ; if ( isWholeBurgerEaten ) { return numPatty ( N ) ; } else { boolean isOnlyTopSliceRemaining = X == burgerLength - 1 ; final long oneSmallerLayerLength = burgerLength ( N - 1 ) ; final long oneSmallerLayerPatty = numPatty ( N - 1 ) ; if ( isOnlyTopSliceRemaining ) { return numPatty ( N ) ; } else { boolean canFinishHalfBurger = X >= ( burgerLength - FIRST_SLICE_IS_BUN - oneSmallerLayerLength ) ; if ( canFinishHalfBurger ) { return oneSmallerLayerPatty + MIDDLE_PATTY + countNumPatty ( N - 1 , X - FIRST_SLICE_IS_BUN - oneSmallerLayerLength - MIDDLE_PATTY ) ; } else { boolean canFinishOneSmallerLayer = ( X - FIRST_SLICE_IS_BUN ) >= ( FIRST_SLICE_IS_BUN + oneSmallerLayerLength ) ; if ( canFinishOneSmallerLayer ) { return oneSmallerLayerPatty ; } else { boolean canEatSomePatty = X > FIRST_SLICE_IS_BUN ; if ( canEatSomePatty ) { return countNumPatty ( N - 1 , X - FIRST_SLICE_IS_BUN ) ; } else { return 0L ; } } } } } }
(method_declaration (modifiers) type: (integral_type) name: (identifier) parameters: (formal_parameters (formal_parameter type: (integral_type) name: (identifier)) (formal_parameter type: (integral_type) name: (identifier))) body: (block (if_statement condition: (parenthesized_expression (binary_expression left: (identifier) right: (decimal_integer_literal))) consequence: (block (return_statement (decimal_integer_literal)))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (identifier))))) (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (identifier)))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (return_statement (method_invocation name: (identifier) arguments: (argument_list (identifier))))) alternative: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (binary_expression left: (identifier) right: (decimal_integer_literal))))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (local_variable_declaration (modifiers) type: (integral_type) declarator: (variable_declarator name: (identifier) value: (method_invocation name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)))))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (return_statement (method_invocation name: (identifier) arguments: (argument_list (identifier))))) alternative: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (parenthesized_expression (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)))))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (return_statement (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (method_invocation name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (binary_expression left: (binary_expression left: (binary_expression left: (identifier) right: (identifier)) right: (identifier)) right: (identifier))))))) alternative: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (parenthesized_expression (binary_expression left: (identifier) right: (identifier))) right: (parenthesized_expression (binary_expression left: (identifier) right: (identifier)))))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (return_statement (identifier))) alternative: (block (local_variable_declaration type: (boolean_type) declarator: (variable_declarator name: (identifier) value: (binary_expression left: (identifier) right: (identifier)))) (if_statement condition: (parenthesized_expression (identifier)) consequence: (block (return_statement (method_invocation name: (identifier) arguments: (argument_list (binary_expression left: (identifier) right: (decimal_integer_literal)) (binary_expression left: (identifier) right: (identifier)))))) alternative: (block (return_statement (decimal_integer_literal))))))))))))))