Home > Backend Development > C++ > body text

In C++, Midy's theorem

WBOY
Release: 2023-09-11 22:57:02
forward
1362 people have browsed it

In C++, Midys theorem

We get the integer value a_num which will store the numerator and p_den which will store the denominator which should be a prime number. The task is to check whether the operation after dividing a_num by p_den proves the midy theorem.

The steps to prove Midy's theorem are-

  • The input numerator is a_num and the denominator is p_den, which should always be a prime number.

  • Divide numbers. Check for repeated decimal values.

  • Store decimal values ​​until they do not repeat.

  • Check if the numbers are duplicates even, if so then split them in half

  • Add the two numbers. If the output is a string of 9's, then it proves Midy's theorem.

Let us see various input and output scenarios for this situation -

In − int a_num = 1 and int p_den = 19

Output− The repeated decimal is: 052631578947368421 Proving Midy’s theorem

Explanation− Follow the above steps to check Midy’s theorem, that is,

  • Division 1 / 19 = 052631578947368421

  • The repeated decimal value is -: 052631578947368421.

  • Cut the number in half, which is 052631578 947368421.

  • Add the two halves, which is 052631578 947368421 = 999,999,999.

  • As we can see, 999,999,999 is a string of 9, which proves Midi’s theorem.

  • ul>

    Input −int a_num = 49, int p_den = 7

    Output − No repeating decimals

    Explanation− As we can see, 49/7 does not generate a decimal value because 49 is perfectly divisible by 7. Therefore, the output is "no repeating decimals".

    The method used in the following program is as follows

    • Enter integer values ​​as int a_num and int p_den.

    • Call the function as Midys_theorem(a_num, p_den) to prove Midy’s theorem.

    • In the function check_Midys()

      • Create a variable for int first to 0, int to 0 at the end

      • Check whether the function check(val) returns FALSE, and then print Midy's theorem does not apply.

      • ELSE IF len % 2 = 0 Then start looping FOR from i to 0 until i is less than len/2 and set first to first * 10 (str[i] - '0' ) and set last to last * 10 (str[len / 2 i] - '0') and print the proved Midy theorem.

    • ELSE, print Midy’s theorem is not applicable.

    • Inside the function Midys_theorem(int a_num, int p_den)

      • Create a map type variable to map the integer type value to map_val and clear the map.

      • Set reminder to a_num % p_den.

      • Start equal to 0 when there is no reminder and map_val.find(reminder) is equal to map_val.end() and then set map_val[reminder] to result.length(), reminder to reminder * 10 , temp is reminder/p_den, result is result to_string(temp) and reminder % p_den.

      • Check IF remainder = 0, then return -1 ELSE, set count to result.substr(map_val[reminder])

      • Return count

    • Function internal bool check(int val)

      • Loop FOR from i to 2 until i is less than val/2. Checks IF val % i = 0 and returns FALSE, otherwise TRUE.

    Example

    #include <bits/stdc++.h>
    using namespace std;
    bool check(int val){
       for(int i = 2; i <= val / 2; i++){
          if(val % i == 0){
             return false;
          }
       }
       return true;
    }
    void check_Midys(string str, int val){
       int len = str.length();
       int first = 0;
       int last = 0;
    
       if(!check(val)){
          cout<<"\nNot applicable for Midy&#39;s theorem";
       }
       else if(len % 2 == 0){
          for(int i = 0; i < len / 2; i++){
             first = first * 10 + (str[i] - &#39;0&#39;);
             last = last * 10 + (str[len / 2 + i] - &#39;0&#39;);
          }
          cout<<"\nProved Midy&#39;s theorem";
       }
       else{
          cout<<"\nNot applicable for Midy&#39;s theorem";
       }
    }
    string Midys_theorem(int a_num, int p_den){
       string result;
       map<int, int> map_val;
       map_val.clear();
    
       int reminder = a_num % p_den;
    
       while((reminder != 0) && (map_val.find(reminder) == map_val.end())){
          map_val[reminder] = result.length();
          reminder = reminder * 10;
          int temp = reminder / p_den;
          result += to_string(temp);
          reminder = reminder % p_den;
       }
       if(reminder == 0){
          return "-1";
       }
       else{
          string count = result.substr(map_val[reminder]);
          return count;
       }
    }
    int main(){
       int a_num = 1;
       int p_den = 19;
       string result = Midys_theorem(a_num, p_den);
       if(result == "-1"){
          cout<<"No Repeating Decimal";
       }
       else{
          cout<<"Repeating decimals are: "<<result;
          check_Midys(result, p_den);
       }
       return 0;
    }
    Copy after login

    Output

    If we run the above code it will generate the following output

    Repeating decimals are: 052631578947368421
    Proved Midy&#39;s theorem
    Copy after login

The above is the detailed content of In C++, Midy's theorem. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template