📜  米迪定理

📅  最后修改于: 2021-05-07 00:08:19             🧑  作者: Mango

根据Midy定理,如果重复小数点的周期为a / p        ,其中p是素数a / p        是一个减少的分数,具有偶数个数字,然后将重复部分分成两半,相加得到字符串9s。

例子 :

给定分子和分母,任务是查找结果浮点数是否遵循Midy定理。

方法 :
让我们模拟一下将分数转换为十进制的过程。让我们看一下已经计算出整数部分的那部分,即楼板(分子/分母)。现在我们剩下(余数=分子%分母)/分母。
如果您还记得转换为十进制的过程,则在每个步骤中,请执行以下操作:

  1. 余数乘以10。
  2. 追加余数/分母以得出结果。
  3. 余数=余数%分母。

在任何时候,如果余数变为0,我们就完成了。
但是,当存在重复序列时,余数永远不会变为0。例如,如果您查看1/3,则余数永远不会变为0。

以下是一项重要观察:
如果我们以余数’rem’开头,并且余数在任何时间点重复,那么两次’rem’出现之间的数字将继续重复。
因此,想法是将可见的剩余物存储在地图中。每当余数重复时,我们将在下一次出现之前返回序列。

以下是Midy定理的实现:

C++
// C++ implementation as a
// proof of the 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. */
    map mp;
    mp.clear();
     
    // 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 Midy's theorem
void Midys(string str, int n)
{
    int l = str.length();
    int part1 = 0, part2 = 0;
    if (!isPrime(n))   
    {
        cout << "Denominator is not prime, "
             << "thus Midy's theorem is not applicable";
    }
    else if (l % 2 == 0)
    {
        for (int i = 0; i < l / 2; i++)
        {
            part1 = part1 * 10 + (str[i] - '0');
            part2 = part2 * 10 + (str[l / 2 + i] - '0');
        }
        cout << part1 << " + " << part2 << " = "
             << (part1 + part2) << endl;
        cout << "Midy's theorem holds!";
    }
    else
    {
        cout << "The repeating decimal is of odd length "
             << "thus Midy's theorem is not applicable";
    }
}
 
// Driver code
int main()
{
    int numr = 2, denr = 11;
    string res = fractionToDecimal(numr, denr);
    if (res == "-1")
        cout << "The fraction does not have repeating decimal";
    else {
        cout << "Repeating decimal = " << res << endl;
        Midys(res, denr);
    }
    return 0;
}


Java
// Java implementation as a
// proof of the 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 Midy's theorem
static void Midys(String str, int n)
{
    int l = str.length();
    int part1 = 0, part2 = 0;
     
    if (!isPrime(n))   
    {
        System.out.print("Denominator is not prime, " +
                         "thus Midy's theorem is not " +
                         "applicable");
    }
    else if (l % 2 == 0)
    {
        for(int i = 0; i < l / 2; i++)
        {
            part1 = part1 * 10 + (str.charAt(i) - '0');
            part2 = part2 * 10 + (str.charAt(l / 2 + i) - '0');
        }
        System.out.println(part1 + " + " + part2 +
                           " = " + (part1 + part2));
        System.out.print("Midy's theorem holds!");
    }
    else
    {
        System.out.print("The repeating decimal is " +
                         "of odd length thus Midy's "+
                         "theorem is not applicable");
    }
}
 
// Driver code
public static void main(String []args)
{
    int numr = 2, denr = 11;
    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);
        Midys(res, denr);
    }
}
}
 
// This code is contributed by rutvik_56


C#
// C# implementation as a
// proof of the 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 mp = new Dictionary();
     
    // 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 Midy's theorem
static void Midys(String str, int n)
{
    int l = str.Length;
    int part1 = 0, part2 = 0;  
    if (!isPrime(n))   
    {
        Console.Write("Denominator is not prime, " +
                         "thus Midy's theorem is not " +
                         "applicable");
    }
    else if (l % 2 == 0)
    {
        for(int i = 0; i < l / 2; i++)
        {
            part1 = part1 * 10 + (str[i] - '0');
            part2 = part2 * 10 + (str[l / 2 + i] - '0');
        }
        Console.WriteLine(part1 + " + " + part2 +
                           " = " + (part1 + part2));
        Console.Write("Midy's theorem holds!");
    }
    else
    {
        Console.Write("The repeating decimal is " +
                         "of odd length thus Midy's "+
                         "theorem is not applicable");
    }
}
 
// Driver code
public static void Main(string []args)
{
    int numr = 2, denr = 11;
    string res = fractionToDecimal(numr, denr);
     
    if (res == "-1")
        Console.Write("The fraction does not " +
                         "have repeating decimal");
    else
    {
        Console.WriteLine("Repeating decimal = " + res);
        Midys(res, denr);
    }
}
}
 
// This code is contributed by pratham76.


输出 :

Repeating decimal = 18
1 + 8 = 9
Midy's theorem holds!

有关Midy定理的更多信息,请参见
http://www.kurims.kyoto-u.ac.jp/EMIS/journals/INTEGERS/papers/h2/h2.pdf
http://digitalcommons.unl.edu/cgi/viewcontent.cgi?article=1047&context=mathfacpub