Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers In C++

Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers In C++

BLUF: Mastering Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers 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: Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers In C++

C++ is renowned for its efficiency. Learn how Nicomachuss Theorem Sum Of K Th Group Of Odd Positive Numbers In C++ enables low-level control and high-performance computing in the tutorial below.

In this tutorial, we will explore Nicomachus’s Theorem (which deals with the sum of k-th group of odd positive numbers) in the C++ programming language along with various strategies to tackle it. Prior to delving into the methodologies, it is essential to have a fundamental understanding of Nicomachus’s Theorem in C++.

Explaining Nicomachus’s Theorem using an Example

A square of a given value k is equal to the sum of all odd positive integers from 1 to k. This mathematical concept can be represented as the sum of 1+3+5+⋯+(2k−1) being equivalent to k squared.

For example:

The total of 1, 3, and 5, which are the initial 3 odd numbers, amounts to 9 = 3 2.

The total of the initial 5 odd integers (1+3+5+7+9 = 25) remains constant at 25 and 5 2 .

This sophisticated outcome demonstrates how basic mathematical calculations reveal the inherent balance of numbers and can lead to profound mathematical discoveries.

Approach-1: Mathematical Inductions

Mathematical induction serves as a highly effective method for demonstrating propositions concerning natural numbers. It consists of two fundamental components: the base case and the inductive step.

  • Base Case: Initially, the task involves confirming the validity of the assertion for the lowest value of k (typically k = 1) within the context.
  • Inductive Step: Subsequently, in this phase, we establish that if the proposition is valid for a given arbitrary k=n (referred to as the inductive hypothesis), it logically follows that the statement holds true for k=n+1.

Following this, it is demonstrated to hold true for every positive integer k by iteratively deriving each successive value starting from the initial condition.

When converting mathematical induction to programming, we follow a series of steps to validate a theorem. Take for example Niomachus’ theorem, which states that the total of the initial k odd numbers equals k^2, a concept we can directly translate into code. Subsequently, we validate the base case (k=1), where the total of the first odd number 1 equals 1^2. Then, we demonstrate that the total for k=n+1 is likewise (n+1)^{2}, and continue by assuming the theorem for k=n.

It is also applicable in programming by employing recursion, loops, and other techniques. Instead of adding up individual numbers and verifying against k squared, we aggregate the initial k odd numbers, equate it to k squared, compare it with the subsequent value, and iterate the process. This approach operates akin to mathematical induction, ensuring the validity of the theorem across all k values.

Example:

Let's consider a scenario to demonstrate Nicomachus's theorem in C++ by employing the method of mathematical induction.

Example

#include <iostream>
#include <cmath>  // For pow() function
#include <iomanip> // For formatted output
// Function to calculate the sum of the first k odd numbers
int sumOddNumbers(int k) {
    int sum = 0;
    for (int i = 1; i <= k; ++i) {
        sum += (2 * i - 1);  // Odd number formula: 2*i - 1
    }
    return sum;
}
// Function to verify the base case of Nicomachus' theorem (k=1)
bool verifyBaseCase() {
    int sum = sumOddNumbers(1);  // The sum of the first odd number
    int square = pow(1, 2);  // Square of 1, which is 1^2
    std::cout << "Base Case Check: Sum = " << sum << ", 1^2 = " << square << std::endl;
    return sum == square;
}
// Function to verify the inductive hypothesis and step
bool verifyInductiveStep(int k) {
    // Base case already checked before this function call
    int sum = sumOddNumbers(k);
    int square = pow(k, 2);  // k^2
    std::cout << "Inductive Hypothesis Check: Sum of first " << k << " odd numbers = " << sum
              << ", k^2 = " << square << std::endl;
    // Now check the inductive step: sum of first k+1 odd numbers equals (k+1)^2
    int sumNext = sumOddNumbers(k + 1);
    int squareNext = pow(k + 1, 2);  // (k+1)^2
    std::cout << "Inductive Step Check: Sum of first " << k + 1 << " odd numbers = " << sumNext
              << ", (k+1)^2 = " << squareNext << std::endl;
    return sumNext == squareNext;
}
// Function to verify the theorem for all values from 1 to maxK
void inductionVerification(int maxK) {
    if (!verifyBaseCase()) {
        std::cout << "Base case failed. Nicomachus' theorem does not hold for k = 1." << std::endl;
        return;
    }
    for (int k = 1; k <= maxK; ++k) {
        std::cout << "\nVerifying for k = " << k << "..." << std::endl;
        if (!verifyInductiveStep(k)) {
            std::cout << "Inductive step failed for k = " << k << ". Nicomachus' theorem does not hold for k = " << k << "." << std::endl;
            return;
        }
    }

    std::cout << "\nInduction completed successfully. Nicomachus' theorem holds for all k from 1 to " << maxK << "." << std::endl;
}
// Function to print the sum of odd numbers up to k in a table format
void printSumTable(int maxK) {
    std::cout << "\nSum of Odd Numbers up to k:\n";
    std::cout << std::setw(10) << "k" << std::setw(25) << "Sum of Odd Numbers" << std::setw(15) << "k^2" << std::endl;
    std::cout << "-----------------------------------------------\n";
    for (int k = 1; k <= maxK; ++k) {
        int sum = sumOddNumbers(k);
        int square = pow(k, 2);
        std::cout << std::setw(10) << k << std::setw(25) << sum << std::setw(15) << square << std::endl;
    }
}
// Function to check and print the detailed verification for k
void detailedVerification(int k) {
    std::cout << "\nDetailed Verification for k = " << k << ":\n";
    int sum = sumOddNumbers(k);
    int square = pow(k, 2);  // k^2
    std::cout << "Sum of the first " << k << " odd numbers: " << sum << std::endl;
    std::cout << "k^2 (square of " << k << "): " << square << std::endl;
    if (sum == square) {
        std::cout << "The theorem holds for k = " << k << ".\n";
    } else {
        std::cout << "The theorem does not hold for k = " << k << ".\n";
    }
    // Checking next k (k+1)
    int sumNext = sumOddNumbers(k + 1);
    int squareNext = pow(k + 1, 2);  // (k+1)^2
    std::cout << "Sum of the first " << k + 1 << " odd numbers: " << sumNext << std::endl;
    std::cout << "(k+1)^2 (square of " << k + 1 << "): " << squareNext << std::endl;
    if (sumNext == squareNext) {
        std::cout << "The theorem holds for k = " << k + 1 << ".\n";
    } else {
        std::cout << "The theorem does not hold for k = " << k + 1 << ".\n";
    }
}
// Function to demonstrate sum of odd numbers for a larger value of k
int optimizedSumOddNumbers(int k) {
    return k * k;
}
int main() {
    int maxK;
    std::cout << "Enter the value of k up to which to verify Nicomachus' theorem: ";
    std::cin >> maxK;
    // Induction verification for range of k
    inductionVerification(maxK);
    // Print sum table for the first 'maxK' odd numbers and check k^2
    printSumTable(maxK);
    // Alternatively, simulate detailed verification for each k
    for (int k = 1; k <= maxK; ++k) {
        detailedVerification(k);
    }
    // Optimized verification for large k values (based on k^2 formula)
    std::cout << "\nOptimized verification for large k values:\n";
    for (int k = 1; k <= maxK; ++k) {
        int sum = optimizedSumOddNumbers(k);
        std::cout << "Sum of first " << k << " odd numbers (Optimized): " << sum << ", k^2 = " << k * k << std::endl;
    }
    return 0;
}

Output:

Output

Enter the value of k up to which to verify Nicomachus' theorem: 3
Base Case Check: Sum = 1, 1^2 = 1
Verifying for k = 1...
Inductive Hypothesis Check: Sum of first 1 odd numbers = 1, k^2 = 1
Inductive Step Check: Sum of first 2 odd numbers = 4, (k+1)^2 = 4
Verifying for k = 2...
Inductive Hypothesis Check: Sum of first 2 odd numbers = 4, k^2 = 4
Inductive Step Check: Sum of first 3 odd numbers = 9, (k+1)^2 = 9
Verifying for k = 3...
Inductive Hypothesis Check: Sum of first 3 odd numbers = 9, k^2 = 9
Inductive Step Check: Sum of first 4 odd numbers = 16, (k+1)^2 = 16
Induction completed successfully. Nicomachus' theorem holds for all k from 1 to 3.
Sum of Odd Numbers up to k:
k Sum of Odd Numbers k^2
-----------------------------------------------
1 1 1
2 4 4
3 9 9
Detailed Verification for k = 1:
Sum of the first 1 odd numbers: 1
k^2 (square of 1): 1
The theorem holds for k = 1.
Sum of the first 2 odd numbers: 4
(k+1)^2 (square of 2): 4
The theorem holds for k = 2.
Detailed Verification for k = 2:
Sum of the first 2 odd numbers: 4
k^2 (square of 2): 4
The theorem holds for k = 2.
Sum of the first 3 odd numbers: 9
(k+1)^2 (square of 3): 9
The theorem holds for k = 3.
Detailed Verification for k = 3:
Sum of the first 3 odd numbers: 9
k^2 (square of 3): 9
The theorem holds for k = 3.
Sum of the first 4 odd numbers: 16
(k+1)^2 (square of 4): 16
The theorem holds for k = 4.
Optimized verification for large k values:
Sum of first 1 odd numbers (Optimized): 1, k^2 = 1
Sum of first 2 odd numbers (Optimized): 4, k^2 = 4
Sum of first 3 odd numbers (Optimized): 9, k^2 = 9

Explanation:

  • sumOddNumbers(int k) This function calculates the sum of the first k-odd integers or numbers. The formula for the nth odd number is 2i −1, where i is the index of the sequence, and i starts at 1. At the first iteration, we run through the first ‘k’ odd numbers, adding them together and returning the summation.
  • verifyBaseCase This function checks the base case of mathematical induction for k=1. It compares the sum of the first odd number (which is 1) to 1, (which is also 1). If they match, the base case is valid, and the theorem holds for k=1.
  • verifyInductiveStep(int k) In this function, we first check that the sum of the first k odd numbers equals k2. After that, we verify the inductive step by ensuring that the sum of the first k+1 odd numbers equals (k+1) 2. This process ensures that the theorem holds for k+1 if it holds for k.
  • inductionVerification(int maxK) This function performs the induction verification process for all values from 1 to maxK. It first verifies the base case and then checks the inductive step for each k up to maxK. If the theorem fails at any point, it halts and outputs an error.
  • printSumTable(int maxK) This function prints a formatted table displaying the sum of odd numbers up to k and the corresponding value of k^2 for each k from 1 to maxK. The table is printed in a nice format by adjusting the width of column entries with the help of the setw function of <iomanip>.
  • detailedVerification(int k) It provides an individual theorem for a particular k and gives a detailed output of the sum of the odd numbers against k^2. It also includes an assertion k+1, and to prove the theorem is step by step.
  • optimizedSumOddNumbers(int k) Similar to previous functions, this function calculates the sum of the first k odd numbers directly from the formula as 𝑘^2.
  • Complexity Analysis:

Time Complexity:

  • sumOddNumbers(int k): It iterates over the first k odd numbers. The loop is run k times, and in each iteration, we do a constant time operation, that is, (adding an odd number). Hence, the time complexity is O(k).
  • verifyBaseCase: This function only does the base case for k=1, which compares the sum (1) to 2. These are all constant-time operations, so time complexity becomes O(1).
  • verifyInductiveStep(int k): The first one is a function that calculates the sum of the first k odd numbers, which compares it with k 2 (which takes O(k) as sumOddNumbers(k) is called). Secondly, it performs the same steps for k+1, and its complexity stays O(k) for each verification.

Space Complexity:

Each function's memory usage is primarily determined by the storage allocated for variables, making space complexity a key consideration. Given that our functions predominantly utilize fixed amounts of memory (such as sums and counters), the space complexity is O(1), barring the input and output data.

Approach-2: Formula Based Approach

Nicomachus’s Principle asserts that the total of the initial k odd numbers equals k squared. The summation of the initial k odd numbers can be directly written as: S(k) = 1 + 3 + 5 + ⋯ + (2k - 1) = k squared.

Instead of adding up odd numbers one by one, we can compute their sum efficiently using a direct formula. To do this, we just have to compare the outcome of k squared with the sum obtained by applying the mathematical formula for the sum of the initial k odd numbers.

Example:

Let's consider an instance to demonstrate Nicomachus's theorem in C++ through a formula-centric method.

Example

#include <iostream>
#include <iomanip>
#include <cmath>  // For pow() function
// Function to calculate the sum of the first k odd numbers using the formula
int sumOddNumbers(int k) {
    return k * k;  // The sum of the first k odd numbers is k^2
}
// Function to calculate the sum of first k odd numbers using an iterative approach
int sumOddNumbersIterative(int k) {
    int sum = 0;
    for (int i = 1; i <= k; ++i) {
        sum += (2 * i - 1);  // Sum the odd numbers: 1, 3, 5, ...
    }
    return sum;
}
// Function to verify Nicomachus' theorem using the formula approach
void verifyTheoremFormula(int maxK) {
    std::cout << "\nVerifying Nicomachus' Theorem using the mathematical formula:\n";    
    for (int k = 1; k <= maxK; ++k) {
        // Calculate the sum using the formula k^2
        int sum = sumOddNumbers(k);        
        // Calculate k^2 for comparison
        int square = pow(k, 2);  // k^2     
        std::cout << "For k = " << k << ":\n";
        std::cout << "Sum of first " << k << " odd numbers (calculated): " << sum << std::endl;
        std::cout << "k^2 (calculated): " << square << std::endl;       
        if (sum == square) {
            std::cout << "The theorem holds for k = " << k << ".\n";
        } else {
            std::cout << "The theorem does not hold for k = " << k << ".\n";
        }       
    }
}
// Function to verify Nicomachus' theorem using the iterative approach
void verifyTheoremIterative(int maxK) {
    std::cout << "\nVerifying Nicomachus' Theorem using the iterative approach:\n";  
    for (int k = 1; k <= maxK; ++k) {
        // Calculate the sum using the iterative method
        int sum = sumOddNumbersIterative(k);      
        // Calculate k^2 for comparison
        int square = pow(k, 2);  // k^2        
        std::cout << "For k = " << k << ":\n";
        std::cout << "Sum of first " << k << " odd numbers (iterative): " << sum << std::endl;
        std::cout << "k^2 (calculated): " << square << std::endl;      
        if (sum == square) {
            std::cout << "The theorem holds for k = " << k << ".\n";
        } else {
            std::cout << "The theorem does not hold for k = " << k << ".\n";
        }
    }
}
// Function to display a table of odd numbers and their sums for verification
void printSumTable(int maxK) {
    std::cout << "\nSum of first k odd numbers for k = 1 to " << maxK << ":\n";
    std::cout << std::setw(10) << "k" << std::setw(30) << "Sum of first k odd numbers" << std::setw(20) << "k^2\n";   
    for (int k = 1; k <= maxK; ++k) {
        int sum = sumOddNumbers(k);
        int square = pow(k, 2);
        std::cout << std::setw(10) << k << std::setw(30) << sum << std::setw(20) << square << std::endl;
    }
}
// Function for input validation and to ensure the value of maxK is positive
int getMaxK() {
    int maxK;
    while (true) {
        std::cout << "Enter the value of k (positive integer) up to which to verify Nicomachus' theorem: ";
        std::cin >> maxK;        
        if (std::cin.fail() || maxK <= 0) {
            std::cin.clear();  // Clears the error flag
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  // Ignores invalid input
            std::cout << "Invalid input. Please enter a positive integer.\n";
        } else {
            break;
        }
    }
    return maxK;
}
int main() {
    int maxK = getMaxK();  // Input validation for maxK
    // Verify the theorem using both approaches
    verifyTheoremFormula(maxK);  // Verification using the formula approach
    verifyTheoremIterative(maxK);  // Verification using the iterative approach  
    // Print the sum table for k = 1 to maxK
    printSumTable(maxK);  // it display a table showing the sum of odd numbers and k^2
    return 0;
}

Output:

Output

Enter the value of k (positive integer) up to which to verify Nicomachus' theorem: 3
Verifying Nicomachus' Theorem using the mathematical formula:
For k = 1:
Sum of first 1 odd number (calculated): 1
k^2 (calculated): 1
The theorem holds for k = 1.
For k = 2:
Sum of first 2 odd numbers (calculated): 4
k^2 (calculated): 4
The theorem holds for k = 2.
For k = 3:
Sum of first 3 odd numbers (calculated): 9
k^2 (calculated): 9
The theorem holds for k = 3.
Verifying Nicomachus' Theorem using the iterative approach:
For k = 1:
Sum of first 1 odd numbers (iterative): 1
k^2 (calculated): 1
The theorem holds for k = 1.
For k = 2:
Sum of first 2 odd numbers (iterative): 4
k^2 (calculated): 4
The theorem holds for k = 2.
For k = 3:
Sum of first 3 odd numbers (iterative): 9
k^2 (calculated): 9
The theorem holds for k = 3.
Sum of first k odd numbers for k = 1 to 3:
         k    Sum of first k odd numbers                k^2
         1                             1                   1
         2                             4                   4
         3                             9                   9

Explanation:

  • The sumOddNumbersIterative(int k) function sums first k odd numbers by iterating over them one by one. It will help us to see how to get the result as well.
  • The verifyTheoremFormula(int maxK) function runs the theorem formula for all values k from 1 to maxK and then verifies. After that, we prove that the formula we calculate for the sum as formula 𝑘^2 for each k found (which we denote verifyTheoremIterative) is equal to k2, since the theorem is correct for any k.
  • The printSumTable(int maxK) function adds a visual representation to the results of the table. Each pair has a sum of 𝑘 odd numbers up to 𝑘th (as calculated) and 𝑘^2 next to it.
  • In the getMaxK function, we prompt the user to enter a positive integer. If the input is invalid, we repeatedly ask until a valid input is provided. Once a valid number is entered, we print the results, handle the user input appropriately, and call the necessary functions to test the theorem and display the results.
  • Complexity Analysis:

Time Complexity:

  • The functions sumOddNumbers and sumOddNumbersIterative are dictate the complexity of the code. It computes the sum using the direct formula, k^2, which is a constant time operation, so now it is O(1). The other hand is sumOddNumbersIterative(int k), which loops through the first k odd numbers and does addition for each of them, so it is O(k) time complexity. Using the direct formula k^2, which is a constant-time operation, its time complexity is O(1).
  • On the other hand, the sumOddNumbersIterative(int k) function involves looping through the first k odd numbers and performing an addition operation for each, resulting in a time complexity of O(k).
  • For each k, the sumOddNumbers(k) function has O(1) complexity. The verifyTheoremFormula(int maxK) function iterates from 1. This function’s total complexity is O(maxK).
  • Similarly, the sumOddNumbersIterative calls itself for each k, which gives us an O(k⋅maxK) time because the latter ones run O(k) for each one.
  • The printSumTable(int maxK) is a function that will iterate from 1 to maxK, doing a constant time operation for every k. Therefore, the complexity of this algorithm becomes O(maxK).

Space Complexity:

The space complexity of the program is O(1) due to its utilization of constant memory for tasks such as handling integers, absence of memory-intensive operations, and lack of additional data structures being employed.

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