Nesbitts Inequality In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Nesbitts Inequality In C++

Nesbitts Inequality In C++

BLUF: Mastering Nesbitts Inequality In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Nesbitts Inequality In C++

C++ is renowned for its efficiency. Learn how Nesbitts Inequality In C++ enables low-level control and high-performance computing in the tutorial below.

Nesbitt's inequality is a mathematical statement that establishes a relationship between the arithmetic mean and the harmonic mean of three positive numbers a, b, and c. Specifically, it asserts that the total of the reciprocals of the arithmetic means of these number pairs is equal to or greater than 3/2. The inequality can be mathematically expressed as (a/(b+c)) + (b/(a+c)) + (c/(a+b)) ≥ (3/2). Known as Nesbitt's Inequality, it offers valuable insights into the correlation between harmonic and arithmetic means within three positive values.

This implies that the sum of the reciprocals of the arithmetic averages of the number pairs is equal to or exceeds 3/2.

The average of two numbers x and y is calculated as (x+y)/2.

The harmonic mean of two numbers x and y is calculated as (2xy/(x+y)).

Nesbitt's Inequality states that the sum of reciprocals of harmonic means between pairs of numbers will always be equal to or greater than 3/2. This principle is attributed to Edward Nesbitt, a renowned mathematician with significant contributions to the field. In C++, you can create a function that follows this Inequality by accepting three positive values, calculating the initial component, and subsequently verifying if the result is greater than or equal to 3/2.

Approach-1: Direct Implementation

The method of "Direct Implementation" in C++ entails developing a function that implements Nesbitt's Inequality without the need for templates or generic programming.

Program:

Example

#include <iostream>
// Template function to check Nesbitt's Inequality for any numeric type
template <typename T>
bool nesbittInequality(T a, T b, T c) {
    // Check for positive numbers
    if (a <= 0 || b <= 0 || c <= 0) {
        std::cerr << "Error: All numbers must be positive." << std::endl;
        return false;
    }
    // Check Nesbitt's inequality
    T left_side = a / (b + c) + b / (a + c) + c / (a + b);
    T right_side = static_cast<T>(3) / static_cast<T>(2);
    return left_side >= right_side;
}
int main() {
    // Example usage with different numeric types
    // Double precision floating-point numbers
    double num1, num2, num3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (double): ";
    std::cin >> num1 >> num2 >> num3;
    // Check and display the result for double
    if (nesbittInequality(num1, num2, num3)) {
        std::cout << "Nesbitt's inequality holds true for doubles." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for doubles." << std::endl;
    }
    // Integer numbers
    int intNum1, intNum2, intNum3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (int): ";
    std::cin >> intNum1 >> intNum2 >> intNum3;
    // Check and display the result for integers
    if (nesbittInequality(intNum1, intNum2, intNum3)) {
        std::cout << "Nesbitt's inequality holds true for integers." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for integers." << std::endl;
    }
    return 0;
}

Output:

Output

Enter three positive numbers (double): 5 6 8
Nesbitt's inequality holds true for doubles.

Explanation:

The

  • nesbittInequality Template Function:

The nesbittInequality template function is created to receive input parameters of multiple numeric data types - a T parameter signifies any specific number type for the latter.

  • Validating Input:

The Function integrates built-in input validation to detect if any of the arguments a, b, or c involved in the calculation process are equal to or less than zero. In such cases, an error message is displayed on the standard error stream (std::cerr), and the Function returns false to indicate an error.

  • Computation of Nesbitt's Inequality:

The computation of the left-hand side of Nesbitt's inequality involves applying the given expression: (a/(b+c)) + (b/(a+c)) + (c/(a+b)) ≥ 3/2. The right-hand side is defined as 3/2.

  • Contrast and Outcome:

The function evaluates Nesbitt's Inequality and outputs true if the left-hand side is greater than or equal to the right-hand side, indicating its validity. Otherwise, it returns false.

The process starts with prompting the user to input three values of a double-precision floating-point number. Subsequently, it examines Nesbitt's Inequality by utilizing the nesbittInequality Function with double data types. Next, the program prompts the user to input three positive natural numbers and applies Nesbitt's Inequality using the identical Function but with integer values.

In summary, the program demonstrates how the template function enables the use of different numeric data types without the need to specifically define each type.

Complexity Analysis:

Time Complexity Analysis:

The time complexity of the generic function primarily relies on the arithmetic computations required for evaluating Nesbitt's Inequality. These arithmetic operations (such as addition, division, and comparison) are all constant time operations denoted by O(1). The generic function iterates through each step once, resulting in a linear time complexity of O(1) with respect to the input size (the values of a, b, and c).

Input Handling in Main Function:

The main Function prompts the user to input three positive numbers, one as a double and the other two as integers. Typically, input operations are evaluated based on the number of characters entered. In practice, these operations are commonly considered to have a constant time complexity of O(1). The processing of input does not significantly affect the overall time complexity.

The time complexity of the program is primarily determined by the arithmetic operations within the template function. As a result, it exhibits O(1) complexity, indicating a constant time complexity.

Space Complexity Analysis:

The template function reserves a fixed amount of memory for local variables. This allocation is independent of the input size, resulting in a time complexity of O(1).

Memory allocated to variables within the main function remains constant. Additional memory may be reserved for standard input and output, but these are external to the program and are not factored into space complexity analysis.

With a space complexity of O(1), the program maintains a constant space complexity. In terms of space utilization, the program's space requirements remain constant regardless of the input size.

Approach 2: Using Function Overloading

Function overloading in C++ is a programming concept that enables the creation of multiple functions in the same scope, each with distinct parameter lists. Each version of the function is tailored to handle a particular parameter type or a specific number of parameters. In the context of Nesbitt's Inequality, function overloading can be used to handle different numeric types effectively.

Program:

Example

#include <iostream>
//Function to check Nesbitt's Inequality for double numbers
bool nesbittInequality(double a, double b, double c) {
    // Check for positive numbers
    if (a <= 0 || b <= 0 || c <= 0) {
        std::cerr << "Error: All numbers must be positive." << std::endl;
        return false;
    }
    // Check Nesbitt's inequality for double
    double left_side = a / (b + c) + b / (a + c) + c / (a + b);
    double right_side = 3.0 / 2.0;
    return left_side >= right_side;
}
//Function to check Nesbitt's Inequality for integer numbers
bool nesbittInequality(int a, int b, int c) {
    // Check for positive numbers
    if (a <= 0 || b <= 0 || c <= 0) {
        std::cerr << "Error: All numbers must be positive." << std::endl;
        return false;
    }
    // Check Nesbitt's inequality for integers
    double left_side = static_cast<double>(a) / (b + c) + static_cast<double>(b) / (a + c) + static_cast<double>(c) / (a + b);
    double right_side = 3.0 / 2.0;
    return left_side >= right_side;
}
int main() {
    // Example usage with different numeric types
    // Double precision floating-point numbers
    double num1, num2, num3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (double): ";
    std::cin >> num1 >> num2 >> num3;
    // Check and display the result for double
    if (nesbittInequality(num1, num2, num3)) {
        std::cout << "Nesbitt's inequality holds true for doubles." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for doubles." << std::endl;
    }
    // Integer numbers
    int intNum1, intNum2, intNum3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (int): ";
    std::cin >> intNum1 >> intNum2 >> intNum3;
    // Check and display the result for integers
    if (nesbittInequality(intNum1, intNum2, intNum3)) {
        std::cout << "Nesbitt's inequality holds true for integers." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for integers." << std::endl;
    }
    return 0;
}

Output:

Output

Enter three positive numbers (double): 1 2 3
Nesbitt's inequality holds true for doubles.

Explanation:

The function proceeds to compute Nesbitt's inequality by utilizing the equation (a/(b+c)) + (b/(a+c)) + (c/(a+b)) ≥ 3/2 and then contrasts it against 3/2.

  • Integer Version (nesbittInequality for int):

The next overloaded function with the same name handles integer values. It validates the positivity of the input integers and computes Nesbitt's Inequality similar to its double data type counterpart.

  • Validating Positivity:

The initial and subsequent iterations of the Function both include a common input validation process to ensure only positive numbers are accepted. If any negative or zero values are provided, an error message is triggered, and the Function will return false.

  • Enhancing Resilience:

Integrating input validation enhances the code's dependability by filtering out any incorrect inputs when performing Nesbitt's inequality calculations. This approach ensures that users receive error messages prompting them to provide valid, positive numerical values.

  • Illustrative Application within the Main Function:

The key Function illustrates how to implement overloaded nesbittInequality functions using both double and integer types. Users are prompted to enter three positive numbers for each numeric data type.

  • Clear Output and Explanation:

The program's output messages provide users with confirmation on whether Nesbitt's Inequality is satisfied by the provided numbers and data types. Users receive precise information regarding the validity of the Inequality for both double and integer data types.

  • Managing Types Explicitly:

While function overloading enhances clarity and simplicity, it explicitly separates the operations for distinct data types. This can result in code duplication when the fundamental logic is shared across various types. In contrast to template-driven methods, function overloading does not provide a one-size-fits-all resolution for managing diverse numeric types, as each type necessitates its own function implementation.

Complexity Analysis:

Time Complexity Analysis:

The idea of function overloading facilitates the provision of distinct functions tailored for double and integer types, ensuring readability and maintainability.

Function Overloading (nesbittInequality):

All the overloaded methods (nesbittInequality for double and int) follow similar paths for input validation, computing Nesbitt's Inequality, and performing a comparison. The computational complexity of individual arithmetic operations (such as addition and division) is consistent. Despite changes in input magnitudes, the program flow stays steady, resulting in a uniform O(1) time complexity for every overloaded function.

The primary function invokes two overloaded functions with a time complexity of O(1), indicating constant time efficiency.

Space Complexity Analysis:

Input Variables:

The variables declared inside the main function to store user-input values (num1, num2, num3; intNum1, intNum2, and intNum3) maintain constant space complexity regardless of the size of their input.

Function Arguments and Local Variables (nesbittInequality): As a result, The condition of the boundary, r, stayed the same.

Every Nesbitt Inequality function includes parameters (a, b, c) and local variables (left_ sides right ). These variables operate within a fixed space for every function call, irrespective of the input magnitude.

Template-Free Approach:

While a template-driven strategy may generate a greater number of function instances for various types, Function overloading leads to distinct functions tailored to specific types without any extra instantiations during runtime. The space complexity remains constant.

The program's memory usage remains constant regardless of the input size, indicating a space complexity of O(1) and thus demonstrating constant-space complexity.

Approach 3: Using Lambda functions:

Lambda expressions provide a practical method for writing anonymous code. They are particularly beneficial for encapsulating algorithms or tests. Nesbitt's Inequality characterizes lambda functions as illustrating the arithmetic rationale behind numeric data types.

Program:

Example

#include <iostream>
// Lambda function to check Nesbitt's Inequality for any numeric type
auto nesbittInequality = [](auto a, auto b, auto c) {
    // Check for positive numbers
    if (a <= 0 || b <= 0 || c <= 0) {
        std::cerr << "Error: All numbers must be positive." << std::endl;
        return false;
    }
    // Check Nesbitt's inequality
    auto left_side = a / (b + c) + b / (a + c) + c / (a + b);
    auto right_side = static_cast<decltype(left_side)>(3) / static_cast<decltype(left_side)>(2);
    return left_side >= right_side;
};
int main() {
    // Example usage with different numeric types using lambda function
    // Double precision floating-point numbers
    double num1, num2, num3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (double): ";
    std::cin >> num1 >> num2 >> num3;
    // Check and display the result for double
    if (nesbittInequality(num1, num2, num3)) {
        std::cout << "Nesbitt's inequality holds true for doubles." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for doubles." << std::endl;
    }
    // Integer numbers
    int intNum1, intNum2, intNum3;
    // Input three positive numbers
    std::cout << "Enter three positive numbers (int): ";
    std::cin >> intNum1 >> intNum2 >> intNum3;
    // Check and display the result for integers
    if (nesbittInequality(intNum1, intNum2, intNum3)) {
        std::cout << "Nesbitt's inequality holds true for integers." << std::endl;
    } else {
        std::cout << "Nesbitt's inequality does not hold true for integers." << std::endl;
    }
    return 0;
}

Output:

Output

Enter three positive numbers (double): 1 2 3
Nesbitt's inequality holds true for doubles.

Explanation:

The C++ program verifies Nesbitt's Inequality for various numeric data types through a lambda expression. It commences by creating a lambda function named nesbittInequality, which accepts three arguments: a, b, and c. Within the lambda function, it initially validates that all three input numbers are positive. If any of them is not positive, an error message is thrown, and the function returns false. The lambda function then applies Nesbitt's Inequality using a specific formula with the threshold set at 3/2. Finally, it returns true if the inequality is satisfied, and false otherwise.

In the main Function, the code demonstrates the application of a lambda function with double-precision floating-point numbers and illustrates its usage with integer values. It calculates the cube of three positive numbers for each data type. Following the user input, the nesbittInequality lambda function is called with the provided values, indicating true or false for each data type, based on Nesbitt's Inequality.

In essence, the software serves as compelling evidence of the extensive applicability of lambda functions in validating Nesbitt's Inequality across various numerical data types, showcasing the potential to eliminate the need for distinct implementations.

Complexity Analysis:

Time Complexity Analysis:

The time complexity for input operations with std:cin is constant, denoted as O(1). This means that the time taken for processing input does not increase with the size of cin itself. Therefore, the dependency factor cannot be considered constant; instead, it is variable. Regardless of the numbers entered by the user, the time taken for input processing remains consistent.

Lambda Function Execution:

The lambda expression incorporates mathematical functions such as addition, division, and comparison. These operations are considered to have a consistent time complexity (O(1)) as they are not influenced by the magnitude of the input values.

The lambda function's performance remains consistent and steady across all numeric input datasets. The primary function calls the lambda function two times, first with double-precision floating-point values and then with integers.

Total Time Complexity:

The lambda function's computational tasks run in constant time, with a set number of calls, resulting in an overall time complexity of O(1). This characteristic ensures that the execution time remains consistent regardless of the size of the problems, making it a versatile solution for various scenarios.

Space Complexity Analysis:

Input Variables:

The initial function declares various variables (such as num1, num2, etc.) to hold input values provided by the user. Regardless of the specific numerical inputs, these variables consistently require a fixed amount of memory space. This characteristic results in a space complexity of O(1), indicating a consistent memory usage.

Lambda Function Local Variables:

Local variables (leftside and rightside) are defined within the lambda function to perform interim computations, akin to the input parameters. These variables occupy a fixed memory space with every invocation of the lambda function, presenting a space complexity of O(1) and thereby enhancing the efficiency of the function.

Total Space Complexity:

The amount of memory occupied by the program remains constant regardless of different input sizes. The space complexity remains at O(1), indicating a consistent usage of memory. This fixed space complexity proves advantageous in situations where efficient memory usage is crucial and the program's memory needs do not increase with larger inputs.

Challenges of Nesbitt's Inequality:

Examining and applying Nesbitt's Inequality as a mathematical principle involving three positive values that relate the harmonic mean to their arithmetic means may pose several challenges.

Complexity in Interpretation:

The primary hurdle lies in grasping Nesbitt's Inequality. The relationship it establishes between the harmonic and arithmetic mean lacks intuitive clarity, unlike cultural references, making it essential to delve into it thoroughly for comprehension.

Application in Problem Solving:

In practical terms, implementing Nesbitt's Inequality in problem resolution can be quite challenging. Understanding the circumstances where the inequality can offer valuable insights or effective solutions may require a grasp of mathematical concepts.

Handling Non-Positive Numbers:

In the context of the Nesbitt Inequality, it specifically pertains to situations involving solely positive numbers. The utilization of this inequality becomes more intricate when it encompasses non-positive numbers or instances where the numbers lack a strictly positive nature. In such scenarios, additional attention and adjustments may be necessary to accommodate these variations.

Mathematical Rigor:

Ensuring the accuracy of proofs or applications involving Nesbitt's Inequality can be challenging at times. Errors in assessments or calculations could lead to incorrect outcomes, underscoring the significance of meticulous scrutiny.

Generalization and Extensions:

Expanding Nesbitt's Inequality to encompass three positive numbers presents challenges, especially when attempting to apply it to a larger dataset or different scenario. Extending its applicability to a broader context, involving a greater number of variables or diverse data types, demands innovative methodologies and a deeper comprehension of mathematical principles.

Executing Nesbitt's Inequality and proactively integrating it into their mathematical domains demands perseverance, resilience, and expertise founded on a strong base of analytical proficiency and problem-solving methodologies.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience