# Extended Midy’s theorem

• Last Updated : 03 Aug, 2022

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. For example 1/7 = 0.14285714285.. 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.

In Extended Midy’s theorem if we divide the repeating portion of a/p into m digits, then their sum is a multiple of 10m -1.

Suppose a = 1 and p = 17,
a/p = 1/17 = 0.0588235294117647…
So, 0588235294117647 is the repeating portion of the decimal expansion of 1/17. Its repeating portion has 16 digits and it can be divided into m digits where m can be 2, 4, 8.
If we consider m = 4 then 0588235294117647 can be divided into 16/4 = 4 numbers and if we add these 4 numbers then result should be a multiple of 104 – 1 = 9999 i.e,
0588 + 2352 + 9411 + 7647 = 19998 = 2*9999

## C++

 `// C++ program to demonstrate extended``// Midy's theorem``#include ``using` `namespace` `std;` `// Returns repeating sequence of a fraction.``// If repeating sequence doesn't exits,``// then returns -1``string fractionToDecimal(``int` `numerator,``                         ``int` `denominator)``{``    ``string res;` `    ``/* Create a map to store already seen remainders``    ``remainder is used as key and its position in``    ``result is stored as value. Note that we need``    ``position for cases like 1/6. In this case,``    ``the recurring sequence doesn't start from first``    ``remainder. */``    ``unordered_map<``int``, ``int``> mp;` `    ``// Find first remainder``    ``int` `rem = numerator % denominator;` `    ``// Keep finding remainder until either remainder``    ``// becomes 0 or repeats``    ``while` `((rem != 0) && (mp.find(rem) == mp.end())) {` `        ``// Store this remainder``        ``mp[rem] = res.length();` `        ``// Multiply remainder with 10``        ``rem = rem * 10;` `        ``// Append rem / denr to result``        ``int` `res_part = rem / denominator;``        ``res += to_string(res_part);` `        ``// Update remainder``        ``rem = rem % denominator;``    ``}` `    ``return` `(rem == 0) ? ``"-1"` `: res.substr(mp[rem]);``}` `// Checks whether a number is prime or not``bool` `isPrime(``int` `n)``{``    ``for` `(``int` `i = 2; i <= n / 2; i++)``        ``if` `(n % i == 0)``            ``return` `false``;``    ``return` `true``;``}` `// If all conditions are met,``// it proves Extended Midy's theorem``void` `ExtendedMidys(string str, ``int` `n, ``int` `m)``{``    ``if` `(!isPrime(n)) {``        ``cout << ``"Denominator is not prime, "``             ``<< ``"thus Extended Midy's "``             ``<< ``"theorem is not applicable"``;``        ``return``;   ``    ``}` `    ``int` `l = str.length();``    ``int` `part1 = 0, part2 = 0;``    ``if` `(l % 2 == 0 && l % m == 0) {` `        ``// Dividing repeated part into m parts``        ``int` `part[m] = { 0 }, sum = 0, res = 0;``        ``for` `(``int` `i = 0; i < l; i++) {``            ``int` `var = i / m;``            ``part[var] = part[var] * 10 + (str[i] - ``'0'``);``        ``}` `        ``// Computing sum of parts.``        ``for` `(``int` `i = 0; i < m; i++) {``            ``sum = sum + part[i];``            ``cout << part[i] << ``" "``;``        ``}``        ` `        ``// Checking for Extended Midy``        ``cout << endl;``        ``res = ``pow``(10, m) - 1;``        ``if` `(sum % res == 0)``            ``cout << ``"Extended Midy's theorem holds!"``;       ``        ``else``            ``cout << ``"Extended Midy's theorem"``                 ``<< ``" doesn't hold!"``;       ``    ``}``    ``else` `if` `(l % 2 != 0) {``        ``cout << ``"The repeating decimal is"``             ``<< ``" of odd length thus Extended "``            ``<< ``"Midy's theorem is not applicable"``;``    ``}``    ``else` `if` `(l % m != 0) {``        ``cout << ``"The repeating decimal can "``             ``<< ``"not be divided into m digits"``;``    ``}``}` `// Driver code``int` `main()``{``    ``int` `numr = 1, denr = 17, m = 4;``    ``string res = fractionToDecimal(numr, denr);``    ``if` `(res == ``"-1"``)``        ``cout << ``"The fraction does not"``             ``<< ``" have repeating decimal"``;``    ``else` `{``        ``cout << ``"Repeating decimal = "` `<< res << endl;``        ``ExtendedMidys(res, denr, m);``    ``}``    ``return` `0;``}`

## Java

 `// Java program to demonstrate extended``// Midy's theorem``import` `java.util.*;` `class` `GFG{` `// Returns repeating sequence of a fraction.``// If repeating sequence doesn't exits,``// then returns -1``static` `String fractionToDecimal(``int` `numerator,``                                ``int` `denominator)``{``    ``String res = ``""``;` `    ``/* Create a map to store already seen remainders``    ``remainder is used as key and its position in``    ``result is stored as value. Note that we need``    ``position for cases like 1/6. In this case,``    ``the recurring sequence doesn't start from first``    ``remainder. */``    ``HashMap mp = ``new` `HashMap<>();` `    ``// Find first remainder``    ``int` `rem = numerator % denominator;` `    ``// Keep finding remainder until either remainder``    ``// becomes 0 or repeats``    ``while` `((rem != ``0``) && !mp.containsKey(rem))``    ``{``        ` `        ``// Store this remainder``        ``mp.put(rem, res.length());` `        ``// Multiply remainder with 10``        ``rem = rem * ``10``;` `        ``// Append rem / denr to result``        ``int` `res_part = rem / denominator;``        ``res += res_part + ``""``;` `        ``// Update remainder``        ``rem = rem % denominator;``    ``}` `    ``return` `(rem == ``0``) ? ``"-1"` `: res.substring(mp.get(rem));``}` `// Checks whether a number is prime or not``static` `boolean` `isPrime(``int` `n)``{``    ``for``(``int` `i = ``2``; i <= n / ``2``; i++)``        ``if` `(n % i == ``0``)``            ``return` `false``;``            ` `    ``return` `true``;``}` `// If all conditions are met,``// it proves Extended Midy's theorem``static` `void` `ExtendedMidys(String str, ``int` `n, ``int` `m)``{``    ``if` `(!isPrime(n))``    ``{``        ``System.out.print(``"Denominator is not prime, "` `+``                         ``"thus Extended Midy's theorem "` `+``                         ``"is not applicable"``);``        ``return``;   ``    ``}` `    ``int` `l = str.length();``    ``int` `part1 = ``0``, part2 = ``0``;``    ` `    ``if` `(l % ``2` `== ``0` `&& l % m == ``0``)``    ``{``        ` `        ``// Dividing repeated part into m parts``        ``int` `[]part = ``new` `int``[m];``        ``int` `sum = ``0``, res = ``0``;``        ``for``(``int` `i = ``0``; i < l; i++)``        ``{``            ``int` `var = i / m;``            ``part[var] = part[var] * ``10` `+``                   ``(str.charAt(i) - ``'0'``);``        ``}` `        ``// Computing sum of parts.``        ``for``(``int` `i = ``0``; i < m; i++)``        ``{``            ``sum = sum + part[i];``            ``System.out.print(part[i] + ``" "``);``        ``}``        ` `        ``// Checking for Extended Midy``        ``System.out.println();``        ``res = (``int``)Math.pow(``10``, m) - ``1``;``        ` `        ``if` `(sum % res == ``0``)``            ``System.out.print(``"Extended Midy's "` `+``                             ``"theorem holds!"``);       ``        ``else``            ``System.out.print(``"Extended Midy's "` `+``                             ``"theorem doesn't hold!"``);       ``    ``}``    ``else` `if` `(l % ``2` `!= ``0``)``    ``{``        ``System.out.print(``"The repeating decimal is of "` `+``                         ``"odd length thus Extended Midy's "` `+``                         ``"theorem is not applicable"``);``    ``}``    ``else` `if` `(l % m != ``0``)``    ``{``        ``System.out.print(``"The repeating decimal can "` `+``                         ``"not be divided into m digits"``);``    ``}``}` `// Driver code``public` `static` `void` `main(String []args)``{``    ``int` `numr = ``1``, denr = ``17``, m = ``4``;``    ``String res = fractionToDecimal(numr, denr);``    ` `    ``if` `(res == ``"-1"``)``        ``System.out.print(``"The fraction does not "` `+``                         ``"have repeating decimal"``);``    ``else``    ``{``        ``System.out.println(``"Repeating decimal = "` `+ res);``        ``ExtendedMidys(res, denr, m);``    ``}``}``}` `// This code is contributed by rutvik_56`

## Python3

 `# Python3 program to demonstrate extended``# Midy's theorem` `# Returns repeating sequence of a fraction.``# If repeating sequence doesn't exits,``# then returns -1``def` `fractionToDecimal(numerator, denominator):``    ``res ``=` `"";` `    ``''' Create a map to store already seen remainders``    ``remainder is used as key and its position in``    ``result is stored as value. Note that we need``    ``position for cases like 1/6. In this case,``    ``the recurring sequence doesn't start from first``    ``remainder. '''``    ``mp ``=` `dict``();` `    ``# Find first remainder``    ``rem ``=` `numerator ``%` `denominator;` `    ``# Keep finding remainder until either remainder``    ``# becomes 0 or repeats``    ``while` `((rem !``=` `0``) ``and` `(rem ``not` `in` `mp)):` `        ``# Store this remainder``        ``mp[rem] ``=` `len``(res);` `        ``# Multiply remainder with 10``        ``rem ``=` `rem ``*` `10``;` `        ``# Append rem / denr to result``        ``res_part ``=` `rem ``/``/` `denominator;``        ``res ``+``=` `str``(res_part);` `        ``# Update remainder``        ``rem ``=` `rem ``%` `denominator;``    ` `    ``if` `rem ``=``=` `0``:``        ``return` `"-1"``    ``return` `res[mp[rem]:]``    `  `# Checks whether a number is prime or not``def` `isPrime(n):``    ``for` `i ``in` `range``(``2``, n ``/``/` `2` `+` `1``):``        ``if` `(n ``%` `i ``=``=` `0``):``            ``return` `False``;``    ``return` `True``;`  `# If all conditions are met,``# it proves Extended Midy's theorem``def` `ExtendedMidys(``str``, n, m):``    ``if` `(``not` `isPrime(n)):``        ``print``(``"Denominator is not prime, thus Extended Midy's theorem is not applicable"``);``        ``return``;``    `  `    ``l ``=` `len``(``str``);``    ``part1 ``=` `0``    ``part2 ``=` `0``;``    ``if` `(l ``%` `2` `=``=` `0` `and` `l ``%` `m ``=``=` `0``):` `        ``# Dividing repeated part into m parts``        ``part ``=` `[``0` `for` `_ ``in` `range``(m)];``        ``sum` `=` `0``        ``res ``=` `0``;``        ``for` `i ``in` `range``(l):``            ``var ``=` `i ``/``/` `m``            ``part[var] ``=` `part[var] ``*` `10` `+` `int``(``str``[i]);``        `  `        ``# Computing sum of parts.``        ``for` `i ``in` `range``(m):``            ``sum` `=` `sum` `+` `part[i];``            ``print``(part[i], end ``=`  `" "``);``        `  `        ``# Checking for Extended Midy``        ``print``()``        ``res ``=` `pow``(``10``, m) ``-` `1``;``        ``if` `(``sum` `%` `res ``=``=` `0``):``            ``print``(``"Extended Midy's theorem holds!"``);``        ``else``:``            ``print``(``"Extended Midy's theorem doesn't hold!"``);``    ` `    ``elif` `(l ``%` `2` `!``=` `0``):``        ``print``(``"The repeating decimal is of odd length thus Extended Midy's theorem is not applicable"``);``    ` `    ``elif` `(l ``%` `m !``=` `0``):``        ``print``(``"The repeating decimal can not be divided into m digits"``);``    `   `# Driver code``numr ``=` `1``denr ``=` `17``m ``=` `4``;``res ``=` `fractionToDecimal(numr, denr);``if` `(res ``=``=` `"-1"``):``    ``print``(``"The fraction does not have repeating decimal"``);``else``:``    ``print``(``"Repeating decimal ="``, res);``    ``ExtendedMidys(res, denr, m);`   `# This code is contributed by phasing17`

## C#

 `// C# program to demonstrate extended``// Midy's theorem``using` `System;``using` `System.Collections;``using` `System.Collections.Generic;` `class` `GFG{` `// Returns repeating sequence of a fraction.``// If repeating sequence doesn't exits,``// then returns -1``static` `string` `fractionToDecimal(``int` `numerator,``                                ``int` `denominator)``{``    ``string` `res = ``""``;` `    ``/* Create a map to store already seen remainders``    ``remainder is used as key and its position in``    ``result is stored as value. Note that we need``    ``position for cases like 1/6. In this case,``    ``the recurring sequence doesn't start from first``    ``remainder. */``    ``Dictionary<``int``,``int``> mp = ``new` `Dictionary<``int``,``int``>();` `    ``// Find first remainder``    ``int` `rem = numerator % denominator;` `    ``// Keep finding remainder until either remainder``    ``// becomes 0 or repeats``    ``while` `((rem != 0) && !mp.ContainsKey(rem))``    ``{``        ` `        ``// Store this remainder``        ``mp[rem]= res.Length;` `        ``// Multiply remainder with 10``        ``rem = rem * 10;` `        ``// Append rem / denr to result``        ``int` `res_part = rem / denominator;``        ``res += res_part + ``""``;` `        ``// Update remainder``        ``rem = rem % denominator;``    ``}` `    ``return` `(rem == 0) ? ``"-1"` `: res.Substring(mp[rem]);``}` `// Checks whether a number is prime or not``static` `bool` `isPrime(``int` `n)``{``    ``for``(``int` `i = 2; i <= n / 2; i++)``        ``if` `(n % i == 0)``            ``return` `false``;``            ` `    ``return` `true``;``}` `// If all conditions are met,``// it proves Extended Midy's theorem``static` `void` `ExtendedMidys(``string` `str, ``int` `n, ``int` `m)``{``    ``if` `(!isPrime(n))``    ``{``        ``Console.Write(``"Denominator is not prime, "` `+``                         ``"thus Extended Midy's theorem "` `+``                         ``"is not applicable"``);``        ``return``;   ``    ``}` `    ``int` `l = str.Length;``    ` `    ``if` `(l % 2 == 0 && l % m == 0)``    ``{``        ` `        ``// Dividing repeated part into m parts``        ``int` `[]part = ``new` `int``[m];``        ``int` `sum = 0, res = 0;``        ``for``(``int` `i = 0; i < l; i++)``        ``{``            ``int` `var` `= i / m;``            ``part[``var``] = part[``var``] * 10 +``                   ``(str[i] - ``'0'``);``        ``}` `        ``// Computing sum of parts.``        ``for``(``int` `i = 0; i < m; i++)``        ``{``            ``sum = sum + part[i];``            ``Console.Write(part[i] + ``" "``);``        ``}``        ` `        ``// Checking for Extended Midy``        ``Console.WriteLine();``        ``res = (``int``)Math.Pow(10, m) - 1;``        ` `        ``if` `(sum % res == 0)``            ``Console.Write(``"Extended Midy's "` `+``                             ``"theorem holds!"``);       ``        ``else``            ``Console.Write(``"Extended Midy's "` `+``                             ``"theorem doesn't hold!"``);       ``    ``}``    ``else` `if` `(l % 2 != 0)``    ``{``        ``Console.Write(``"The repeating decimal is of "` `+``                         ``"odd length thus Extended Midy's "` `+``                         ``"theorem is not applicable"``);``    ``}``    ``else` `if` `(l % m != 0)``    ``{``        ``Console.Write(``"The repeating decimal can "` `+``                         ``"not be divided into m digits"``);``    ``}``}` `// Driver code``public` `static` `void` `Main(``string` `[]args)``{``    ``int` `numr = 1, denr = 17, m = 4;``    ``string` `res = fractionToDecimal(numr, denr);``    ` `    ``if` `(res == ``"-1"``)``        ``Console.Write(``"The fraction does not "` `+``                         ``"have repeating decimal"``);``    ``else``    ``{``        ``Console.WriteLine(``"Repeating decimal = "` `+ res);``        ``ExtendedMidys(res, denr, m);``    ``}``}``}` `// This code is contributed by pratham76.`

## Javascript

 `// JavaScript program to demonstrate extended``// Midy's theorem` `// Returns repeating sequence of a fraction.``// If repeating sequence doesn't exits,``// then returns -1``function` `fractionToDecimal(numerator, denominator)``{``    ``let res = ``""``;` `    ``/* Create a map to store already seen remainders``    ``remainder is used as key and its position in``    ``result is stored as value. Note that we need``    ``position for cases like 1/6. In this case,``    ``the recurring sequence doesn't start from first``    ``remainder. */``    ``let mp = {};` `    ``// Find first remainder``    ``let rem = numerator % denominator;` `    ``// Keep finding remainder until either remainder``    ``// becomes 0 or repeats``    ``while` `((rem != 0) && (!mp.hasOwnProperty(rem))) {` `        ``// Store this remainder``        ``mp[rem] = res.length;` `        ``// Multiply remainder with 10``        ``rem = rem * 10;` `        ``// Append rem / denr to result``        ``let res_part = Math.floor(rem / denominator);``        ``res += res_part.toString();` `        ``// Update remainder``        ``rem = rem % denominator;``    ``}` `    ``return` `(rem == 0) ? ``"-1"` `: res.substr(mp[rem]);``}` `// Checks whether a number is prime or not``function` `isPrime(n)``{``    ``for` `(``var` `i = 2; i <= n / 2; i++)``        ``if` `(n % i == 0)``            ``return` `false``;``    ``return` `true``;``}` `// If all conditions are met,``// it proves Extended Midy's theorem``function` `ExtendedMidys(str, n, m)``{``    ``if` `(!isPrime(n)) {``        ``console.log(``            ``"Denominator is not prime, thus Extended Midy's theorem is not applicable"``);``        ``return``;``    ``}` `    ``let l = str.length;``    ``let part1 = 0, part2 = 0;``    ``if` `(l % 2 == 0 && l % m == 0) {` `        ``// Dividing repeated part into m parts``        ``let part = ``new` `Array(m).fill(0);``        ``let sum = 0, res = 0;``        ``for` `(``var` `i = 0; i < l; i++) {``            ``var` `var_ = Math.floor(i / m);``            ``part[var_] = part[var_] * 10 + parseInt(str[i]);``        ``}` `        ``// Computing sum of parts.``        ``for` `(``var` `i = 0; i < m; i++) {``            ``sum = sum + part[i];``            ``process.stdout.write(part[i] + ``" "``);``        ``}` `        ``// Checking for Extended Midy``        ``console.log();``        ``res = Math.pow(10, m) - 1;``        ``if` `(sum % res == 0)``            ``console.log(``"Extended Midy's theorem holds!"``);``        ``else``            ``console.log(``                ``"Extended Midy's theorem doesn't hold!"``);``    ``}``    ``else` `if` `(l % 2 != 0) {``        ``console.log(``            ``"The repeating decimal is of odd length thus Extended Midy's theorem is not applicable"``);``    ``}``    ``else` `if` `(l % m != 0) {``        ``console.log(``            ``"The repeating decimal can not be divided into m digits"``);``    ``}``}` `// Driver code``let numr = 1, denr = 17, m = 4;``let res = fractionToDecimal(numr, denr);``if` `(res == ``"-1"``)``    ``console.log(``        ``"The fraction does not have repeating decimal"``);``else` `{``    ``console.log(``"Repeating decimal = "` `+ res);``    ``ExtendedMidys(res, denr, m);``}` `// This code is contributed by phasing17`

Output:

```Repeating decimal = 0588235294117647
588 2352 9411 7647
Extended Midy's theorem holds!```

My Personal Notes arrow_drop_up