📜  扩展的米迪定理

📅  最后修改于: 2021-04-28 14:18:20             🧑  作者: Mango

根据Midy定理,如果重复小数点的周期为a / p      ,其中p是素数a / p      是一个减少的分数,具有偶数个数字,然后将重复部分分成两半,相加得到字符串9s。例如1/7 = 0.14285714285 ..是一个重复的小数,其中重复了142857。现在,根据该定理,它具有偶数个重复数字,即142857。此外,如果将其分为两半,我们得到142和857。因此,将这两个数相加后,我们得到999,它是9的字符串,并且与我们的匹配定理。

扩展Midy定理中,如果将a / p的重复部分划分为m个数字,则它们的和为10 m -1的倍数。

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 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


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 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 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.


输出:
Repeating decimal = 0588235294117647
588 2352 9411 7647 
Extended Midy's theorem holds!