The following C++ program can be used find the denominator (given an absolute upper bound), that is no greater than a certain value, of an irreducible fraction whose decimal representationexpansion will have the longest possible repetend:
#include <iostream>
#include <tuple>
using std::cout;
int main()
{
std::tuple<int, whileint> maxRepetendDenom(true)int denomax)
{
int denom, repetend = 0;
int i, j, value, counter, denominator, denominatorMax;counter;
cout <<for "Enter(i the= maximum3; allowedi absolute<= valuedenomax; ofi the+= denominator:2) ";{
std::cin >> denominatorMax; counter = 1;
if (denominatorMax <=value 0)= {10%i;
j return= 0;denomax;
}
while (value != 1 if&& (denominatorMaxj <> 30) {
coutvalue <<*= "No10;
simple fraction with an absolute value of the denominator less "
value %= i;
counter++;
<< "than 3 can have a repetend in its decimal representation.\n\n";j--;
} else {
forif (i = 3;counter i> <=repetend denominatorMax;&& ij +=> 21) {
counterrepetend = 1;counter;
valuedenom = 10%i;i;
j = denominatorMax;}
}
return {denom, repetend};
}
int main()
{
while (value != 1 && j > 0true) {
int valuedenom, *=repetend, 10;denomax;
cout << "Enter the maximum allowed absolute value %= i;
of the denominator: ";
std::cin >> counter++;denomax;
if (denomax <= 0) j--;{
return }0;
}
if (counter > repetend && jdenomax >< 13) {
cout << "No fraction with an absolute value repetendof =the counter;denominator"
<< " less denominatorthan =3 i;
can have a repetend.\n\n";
} else {
}
std::tie(denom, repetend) = }maxRepetendDenom(denomax);
cout <<std::string "Alldigits irreducible= fractionsrepetend of> the1 form? n/" << denominator
digits" : " digit";
cout << ""A willfraction havewith aan decimalabsolute representationvalue withof the longestdenominator no greater "
<< "possible repetend of"than " << repetenddenomax << " digit";
will have the longest ifpossible (repetend > 1) {"
std::cout << "s";
}
"(of " << repetend << coutdigits << ", given the constraint that) thein absoluteits valuedecimal ofexpansion theif denominator"it"
<< " canis bein nolowest greaterterms thanand its denominator is " << denominatorMaxdenom << ".\n\n";
}
}
return 0;
}