Midys Theorem In C++ - C++ Programming Tutorial
C++ Course / Miscellaneous / Midys Theorem In C++

Midys Theorem In C++

BLUF: Mastering Midys Theorem 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: Midys Theorem In C++

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

Introduction:

Mathematics and coding frequently intersect to tackle intricate challenges effectively. Midy's theorem, a lesser-known yet captivating outcome in number theory, sheds light on the periodic decimal fractions of rational numbers. In this guide, we will delve into the mathematical principles behind Midy's theorem, explore its historical context, provide illustrative examples, and demonstrate its computational importance by coding it in C++.

What is Midy's Theorem?

Midy's theorem, coined in honor of the mathematician E. Midy from the 19th century, focuses on the recurring decimal representations of specific fractions. Specifically, the theorem states:

If a fraction, represented by the coprime integers ```

include <iostream>

include <vector>

include <string>

include <cmath>

include <algorithm>

Example

int findDecimalPeriod(int p, int q) {
    std::vector<int> remainders(q, -1);
    int remainder = p % q;
    int position = 0;

    while (remainder != 0 && remainders[remainder] == -1) {
        remainders[remainder] = position;
        remainder = (remainder * 10) % q;
position++;
        }
        return (remainder == 0)? 0 : position - remainders[remainder];
    }

std::string getRepeatingDecimal(int p, int q, int period) {

std::string decimalPart;

int remainder = p % q;

for (int i = 0; i < period; ++i) {

remainder *= 10;

decimalPart += (remainder / q) + '0';

remainder %= q;

}

return decimalPart;

}

Example

bool verifyMidysTheorem(const std::string& decimalPart) {
    int length = decimalPart.size();

    if (length % 2!= 0) {
        std::cerr << "Period is not even, Midy's theorem does not apply." << std::endl;
        return false;
    }

    int halfLength = length / 2;
std::string part1 = decimalPart.substr(0, halfLength);
    std::string part2 = decimalPart.substr(halfLength);

    int sum = std::stoi(part1) + std::stoi(part2);
    int expectedSum = std::pow(10, halfLength) - 1;

    return sum == expectedSum;
}

Example:

Let's take:

  • The decimal expansion of 1/7=0.142857‾1/7 = 0.\overline{142857}1/7=0.142857. The repeating part is 142857142857142857 with a period of 6 (even).
  • Divide the repeating part into two halves: First part: 142142142 Second part: 857857857
  • Add the two parts: 142+857=999142 + 857 = 999142+857=999
  • Conclusion:

The total of the two identical segments amounts to 999, validating Midy's theorem.

Midy's theorem explains the potential for analyzing recurring decimals and uncovering patterns or characteristics that could be useful in fields such as cryptography and numerical analysis.

Historical Background of Midy's Theorem:

E. Midy introduced Midy's theorem during the 19th century within the realm of decimal representations of rational numbers. While not as widely recognized as some other theorems in number theory, it plays a significant role in exploring recurring decimals and modular arithmetic. This theorem serves as a crucial link connecting basic arithmetic to more profound understandings of the cyclic nature of decimal representations.

Breaking down the Mathematics

In order to apply Midy's theorem, we have to do the following:

  • Check Coprimality The numerator and the denominator must be coprime in order for Midy's theorem to be applied. It ensures that the fraction is in its simplest form.
  • Determine the Repeating Decimal Use long division or modular arithmetic to find the repeating portion of the decimal expansion because this repeating part is essential for applying Midy's theorem.
  • Check Periodicity Ensure the decimal period is even. If the period is odd, Midy's theorem does not apply.
  • Split and Sum Separate the repeating decimal into two equal segments. Check that their sum equals, where n is the length of each segment.
  • Appreciation for Decimal Periods

    Terminating vs. Non-Terminating Decimals

Decimals are a way to represent fractions, with some fractions ending and others repeating endlessly:

  • When the denominator has prime factors limited to 2s and 5s, the decimal is finite.
  • Decimals that don't terminate have recurring patterns, occurring when the denominator includes different prime factors.
  • Non-Recurring Decimals Periodicity

The period refers to the repeating pattern of a non-recurring decimal. For example,

  • has a period of 1, while
  • has a period of 6.

The duration of the period is determined by the denominator and can be calculated through operations in modular arithmetic.

Programming Midy's Theorem in C++

C++ is a suitable option for this particular issue due to its high speed and capability to handle modular arithmetic. Now, let's delve into the code.

Step 1: Setup

Create a fresh C++ document, like for instance, midys_theorem.cpp, and import all the essential headers:

Example

#include <iostream>
#include <vector>
#include <string>
#include <cmath>
#include <algorithm>

Step 2: Find the Decimal Period

The recurring decimal pattern can be found through the application of modular arithmetic. The sequence of remainders obtained upon division will eventually start repeating, and this recurring section is known as the period in C++ programming guide.

Example

int findDecimalPeriod(int p, int q) {
    std::vector<int> remainders(q, -1);
    int remainder = p % q;
    int position = 0;

    while (remainder != 0 && remainders[remainder] == -1) {
        remainders[remainder] = position;
        remainder = (remainder * 10) % q;
position++;
        }
        return (remainder == 0)? 0 : position - remainders[remainder];
    }

Step 3: Extracting the Repeating Decimal

Once the decimal point is located, we can extract the recurring decimal.

Example

std::string getRepeatingDecimal(int p, int q, int period) {
    std::string decimalPart;
    int remainder = p % q;

for (int i = 0; i < period; ++i) {
        remainder *= 10;
        decimalPart += (remainder / q) + '0';
        remainder %= q;
    }

    return decimalPart;
}

Step 4: Checking Midy's Theorem

Separate the repeating decimal and check the sum:

Example

bool verifyMidysTheorem(const std::string& decimalPart) {
    int length = decimalPart.size();

    if (length % 2!= 0) {
        std::cerr << "Period is not even, Midy's theorem does not apply." << std::endl;
        return false;
    }

    int halfLength = length / 2;
std::string part1 = decimalPart.substr(0, halfLength);
    std::string part2 = decimalPart.substr(halfLength);

    int sum = std::stoi(part1) + std::stoi(part2);
    int expectedSum = std::pow(10, halfLength) - 1;

    return sum == expectedSum;
}

Step 5: Main Function

Combine all steps in the main function :

Example

int main() {
    int p, q;
    std::cout << "Enter numerator (p): ";
std::cin >> p;
    std::cout << "Enter denominator (q): ";
    std::cin >> q;

    if (std::__gcd(p, q) != 1) {
        std::cerr << "p and q are not coprime." << std::endl;
        return 1;
    }

    int period = findDecimalPeriod(p, q);

if (period == 0) {
        std::cout << "The fraction has a terminating decimal." << std::endl;
        return 0;
    }

    std::cout << "Decimal period: " << period << std::endl;
    std::string repeatingDecimal = getRepeatingDecimal(p, q, period);
std::cout << "Repeating part: " << repeatingDecimal << std::endl;

    if (verifyMidysTheorem(repeatingDecimal)) {
        std::cout << "Midy's theorem is verified." << std::endl;
    } else {
        std::cout << "Midy's theorem does not hold for this fraction." << std::endl;
    }

    return 0;
}

Output:

Example 1:

Input:

Example

Enter numerator (p): 1
Enter denominator (q): 7

Output:

Output

Decimal period: 6
Repeating part: 142857
Midy's theorem is valid.

Example 2:

Input:

Example

Enter numerator (p): 1
Enter denominator (q): 6

Output:

Output

The decimal expansion terminates.

Applications of Midy's Theorem:

Several applications of the Midy's Theorem in C++ are as follows:

  • Cryptography: Repeating decimals helps decipher modular arithmetic.
  • Decimal Analysis: Look for patterns of non-terminating decimals that can be applied to mathematical theorems
  • Teaching Tools: Teaching number theory and properties of rational numbers.
  • Conclusion:

In summary, Midy's theorem uncovers hidden connections within recurring decimals, proving its significance in the realm of number theory. By integrating this concept into C++, theoretical mathematics seamlessly merges with practical computational applications. This task exemplifies the synergy between theoretical concepts and coding to tackle intricate challenges, uncover intriguing patterns, and deepen comprehension of mathematical fundamentals.

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