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

This article details C function return types, encompassing basic (int, float, char, etc.), derived (arrays, pointers, structs), and void types. The compiler determines the return type via the function declaration and the return statement, enforcing

Gulc is a high-performance C library prioritizing minimal overhead, aggressive inlining, and compiler optimization. Ideal for performance-critical applications like high-frequency trading and embedded systems, its design emphasizes simplicity, modul

This article explains C function declaration vs. definition, argument passing (by value and by pointer), return values, and common pitfalls like memory leaks and type mismatches. It emphasizes the importance of declarations for modularity and provi

This article details C functions for string case conversion. It explains using toupper() and tolower() from ctype.h, iterating through strings, and handling null terminators. Common pitfalls like forgetting ctype.h and modifying string literals are

This article examines C function return value storage. Small return values are typically stored in registers for speed; larger values may use pointers to memory (stack or heap), impacting lifetime and requiring manual memory management. Directly acc

This article analyzes the multifaceted uses of the adjective "distinct," exploring its grammatical functions, common phrases (e.g., "distinct from," "distinctly different"), and nuanced application in formal vs. informal

This article details efficient STL algorithm usage in C . It emphasizes data structure choice (vectors vs. lists), algorithm complexity analysis (e.g., std::sort vs. std::partial_sort), iterator usage, and parallel execution. Common pitfalls like

This article explains the C Standard Template Library (STL), focusing on its core components: containers, iterators, algorithms, and functors. It details how these interact to enable generic programming, improving code efficiency and readability t


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor
