According to Midy’s theorem, if the period of a repeating decimal for , where p is prime and is a reduced fraction, has an even number of digits, then dividing the repeating portion into halves and adding gives a string of 9s.
a = 1 and p = 7
1/7 = 0.14285714285..
So 1/7 is a repeating decimal with 142857 being repeated. Now, according to the theorem, it has even number of repeating digits i.e. 142857. Further if we divide this into two halves, we get 142 and 857. Thus, on adding these two, we get 999 which is string of 9s and matches our theorem.
a = 2 and p = 11
2/11 = 0.18181818181..
Here, repeating decimal is 18. Now this is even in number therefore 1+8 = 9 which again shows the validity of Midy’s theorem.
Given numerator and denominator, the task is to find if the resultant floating point number follows Midy’s theorem or not.
Let us simulate the process of converting fraction to decimal. Let us look at the part where we have already figured out the integer part which is floor(numerator/denominator). Now we are left with ( remainder = numerator%denominator ) / denominator.
If you remember the process of converting to decimal, at each step we do the following :
- Multiply the remainder by 10.
- Append remainder / denominator to result.
- Remainder = remainder % denominator.
At any moment, if remainder becomes 0, we are done.
However, when there is a recurring sequence, remainder never becomes 0. For example if you look at 1/3, the remainder never becomes 0.
Below is one important observation :
If we start with remainder ‘rem’ and if the remainder repeats at any point of time, the digits between the two occurrence of ‘rem’ keep repeating.
So the idea is to store seen remainders in a map. Whenever a remainder repeats, we return the sequence before the next occurrence.
Below is CPP implementation of Midy’s theorem :
Repeating decimal = 18 1 + 8 = 9 Midy's theorem holds!
More about Midy’s theorem can be found on
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Chinese Remainder Theorem | Set 1 (Introduction)
- Wilson's Theorem
- Zeckendorf's Theorem (Non-Neighbouring Fibonacci Representation)
- Compute nCr % p | Set 2 (Lucas Theorem)
- Chinese Remainder Theorem | Set 2 (Inverse Modulo based Implementation)
- Combinatorial Game Theory | Set 4 (Sprague - Grundy Theorem)
- Using Chinese Remainder Theorem to Combine Modular equations
- Corollaries of Binomial Theorem
- Fermat's little theorem
- Nicomachus’s Theorem (Sum of k-th group of odd positive numbers)
- Extended Midy's theorem
- Fermat's Last Theorem
- Nicomachu's Theorem
- Rosser's Theorem
- Euclid Euler Theorem
- Hardy-Ramanujan Theorem
- Lagrange's four square theorem
- Vantieghems Theorem for Primality Test
- Dilworth's Theorem
- An application on Bertrand's ballot theorem
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.