
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Midy's Theorem in C++
We are given integer values as a_num that will store the numerator and p_den that will store the denominator which should be a prime number. The task is to check whether the operations performed on a_num after dividing with p_den proves the midy’s theorem or not.
Steps to prove Midy’s theorem are-
Input numerator as a_num and denominator as p_den which should always be a prime value.
Divide the numbers. Check for the repeating decimal values.
Store the decimal values until they are not repeating.
Check whether the digits are even, if yes, then break them into halves
Add both the numbers. If the output is a string of 9 then it proves Midy’s theorem.
Let us see various input output scenarios for this -
In − int a_num = 1 and int p_den = 19
Out − Repeating decimals are: 052631578947368421 Proved Midy's theorem
Explanation − Follow the steps mentioned above to check for Midy’s theorem i.e.
Divide 1 / 19 = 052631578947368421
Repeating decimal values are-: 052631578947368421.
Divide the digits into halves i.e. 052631578 947368421.
Add both the halves i.e. 052631578 + 947368421 = 999,999,999.
As we can see, 999,999,999 is the string of 9 which proves the Midy’s theorem.
In −int a_num = 49, int p_den = 7
Out − No Repeating Decimal
Explanation − As we can see 49/7 generates no decimal value since 49 is completely divisible by 7. So, the output is No Repeating Decimal.
Approach used in the below program is as follows
Input to integer values as int a_num and int p_den.
Call function as Midys_theorem(a_num, p_den) to prove Midy's theorem.
-
Inside the function check_Midys()
Create variables as int first to 0 and int last to 0
Check IF the function check(val) returns FALSE then print Midy’s theorem not applicable.
ELSE IF len % 2 = 0 then start loop FOR from i to 0 till i less than len/2 and set first to first * 10 + (str[i] - '0') and last to last * 10 + (str[len / 2 + i] - '0') and print proved Midy’s theorem.
ELSE, print Midy’s theorem not applicable.
-
Inside the function Midys_theorem(int a_num, int p_den)
Create a map type variable to map integer type values as map_val and clear the map.
Set reminder as a_num % p_den.
Start while reminder not equals to 0 AND map_val.find(reminder) equals to map_val.end() then set map_val[reminder] to result.length(), reminder to reminder * 10, temp to reminder / p_den, result to result + to_string(temp) and reminder to reminder % p_den.
Check IF remainder = 0 then return -1 ELSE, set count to result.substr(map_val[reminder])
Return count
-
Inside the function bool check(int val)
Start loop FOR from i to 2 till i less than val/2. Check IF val % i = 0 then return FALSE otherwise return 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's theorem"; } else if(len % 2 == 0){ for(int i = 0; i < len / 2; i++){ first = first * 10 + (str[i] - '0'); last = last * 10 + (str[len / 2 + i] - '0'); } cout<<"\nProved Midy's theorem"; } else{ cout<<"\nNot applicable for Midy'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; }
Output
If we run the above code it will generate the following Output
Repeating decimals are: 052631578947368421 Proved Midy's theorem