In C++, Midy's 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>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!

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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

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

The article discusses dynamic dispatch in C , its performance costs, and optimization strategies. It highlights scenarios where dynamic dispatch impacts performance and compares it with static dispatch, emphasizing trade-offs between performance and

C 20 ranges enhance data manipulation with expressiveness, composability, and efficiency. They simplify complex transformations and integrate into existing codebases for better performance and maintainability.

This article details effective exception handling in C , covering try, catch, and throw mechanics. It emphasizes best practices like RAII, avoiding unnecessary catch blocks, and logging exceptions for robust code. The article also addresses perf

The article discusses using move semantics in C to enhance performance by avoiding unnecessary copying. It covers implementing move constructors and assignment operators, using std::move, and identifies key scenarios and pitfalls for effective appl

Article discusses effective use of rvalue references in C for move semantics, perfect forwarding, and resource management, highlighting best practices and performance improvements.(159 characters)

C memory management uses new, delete, and smart pointers. The article discusses manual vs. automated management and how smart pointers prevent memory leaks.
