Skip to main content
deleted 84 characters in body
Source Link
q-l-p
  • 189
  • 1
  • 10
#include <iostream>
#include <tuple>
using std::cout;

std::tuple<int, int> maxRepetendDenom(int denomaxdMax)
{
    int denomd, repetendrep = 0;
    int i, j, value, counter;
    for (i = 3; i <= denomax;dMax; i += 2) {
        counter = 1;
        value = 10%i;
        j = denomax;dMax;
        while (value != 1 && j > 0) {
            value *= 10;
            value %= i;
            counter++;
            j--;
        }
        if (counter > repetendrep && j > 1) {
            repetendrep = counter;
            denomd = i;
        }
    }
    return {denomd, repetendrep};
}

int main()
{
    while (true) {
        int denomd, repetendrep, denomax;dMax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> denomax;dMax;
        if (denomaxdMax <= 0) {
            return 0;
        }
        if (denomaxdMax < 3) {
            cout << "No fraction with an absolute value of the denominator"
                 << " less than 3 can have a repetend.\n\n";
        } else {
            std::tie(denomd, repetendrep) = maxRepetendDenom(denomaxdMax);
            std::string digits = repetendrep > 1 ? " digits"digits)" : " digit";digit)";
            cout << "A fraction"Fractions with an absolute value of the denominator no greater "greater"
                 << "than" than " << denomaxdMax << " will have the longest possible repetend "
                 << "(of " << repetendrep << digits << ") in itstheir decimal expansion if it"they"
                 << " isare in lowest terms and itstheir denominator is " << denomd << ".\n\n";
        }
    }
    return 0;
}
#include <iostream>
#include <tuple>
using std::cout;

std::tuple<int, int> maxRepetendDenom(int denomax)
{
    int denom, repetend = 0;
    int i, j, value, counter;
    for (i = 3; i <= denomax; i += 2) {
        counter = 1;
        value = 10%i;
        j = denomax;
        while (value != 1 && j > 0) {
            value *= 10;
            value %= i;
            counter++;
            j--;
        }
        if (counter > repetend && j > 1) {
            repetend = counter;
            denom = i;
        }
    }
    return {denom, repetend};
}

int main()
{
    while (true) {
        int denom, repetend, denomax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> denomax;
        if (denomax <= 0) {
            return 0;
        }
        if (denomax < 3) {
            cout << "No fraction with an absolute value of the denominator"
                 << " less than 3 can have a repetend.\n\n";
        } else {
            std::tie(denom, repetend) = maxRepetendDenom(denomax);
            std::string digits = repetend > 1 ? " digits" : " digit";
            cout << "A fraction with an absolute value of the denominator no greater "
                 << "than " << denomax << " will have the longest possible repetend "
                 << "(of " << repetend << digits << ") in its decimal expansion if it"
                 << " is in lowest terms and its denominator is " << denom << ".\n\n";
        }
    }
    return 0;
}
#include <iostream>
#include <tuple>
using std::cout;

std::tuple<int, int> maxRepetendDenom(int dMax)
{
    int d, rep = 0;
    int i, j, value, counter;
    for (i = 3; i <= dMax; i += 2) {
        counter = 1;
        value = 10%i;
        j = dMax;
        while (value != 1 && j > 0) {
            value *= 10;
            value %= i;
            counter++;
            j--;
        }
        if (counter > rep && j > 1) {
            rep = counter;
            d = i;
        }
    }
    return {d, rep};
}

int main()
{
    while (true) {
        int d, rep, dMax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> dMax;
        if (dMax <= 0) {
            return 0;
        }
        if (dMax < 3) {
            cout << "No fraction with an absolute value of the denominator"
                 << " less than 3 can have a repetend.\n\n";
        } else {
            std::tie(d, rep) = maxRepetendDenom(dMax);
            std::string digits = rep > 1 ? " digits)" : " digit)";
            cout << "Fractions with an absolute value of the denominator no greater"
                 << " than " << dMax << " will have the longest possible repetend "
                 << "(of " << rep << digits << " in their decimal expansion if they"
                 << " are in lowest terms and their denominator is " << d << ".\n\n";
        }
    }
    return 0;
}
deleted 67 characters in body
Source Link
q-l-p
  • 189
  • 1
  • 10

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;
}

The following C++ program can be used find the denominator (given an absolute upper bound) of an irreducible fraction whose decimal representation will have the longest possible repetend:

#include <iostream>
using std::cout;

int main()
{
    while (true) {
        int repetend = 0;
        int i, j, value, counter, denominator, denominatorMax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> denominatorMax;      
        if (denominatorMax <= 0) {
            return 0;
        }
        if (denominatorMax < 3) {
            cout << "No simple fraction with an absolute value of the denominator less "
                 << "than 3 can have a repetend in its decimal representation.\n\n";
        } else {
            for (i = 3; i <= denominatorMax; i += 2) {
                counter = 1;
                value = 10%i;
                j = denominatorMax;
                while (value != 1 && j > 0) {
                    value *= 10;
                    value %= i;
                    counter++;
                    j--;
                }
                if (counter > repetend && j > 1) {
                    repetend = counter;
                    denominator = i;
                }
            }
            cout << "All irreducible fractions of the form n/" << denominator
                 << " will have a decimal representation with the longest "
                 << "possible repetend of " << repetend << " digit";
            if (repetend > 1) {
                std::cout << "s";
            }
            cout << ", given the constraint that the absolute value of the denominator"
                 << " can be no greater than " << denominatorMax << ".\n\n";
        }
    }
    return 0;
}

The following C++ program can be used find the denominator, that is no greater than a certain value, of an irreducible fraction whose decimal expansion will have the longest possible repetend:

#include <iostream>
#include <tuple>
using std::cout;

std::tuple<int, int> maxRepetendDenom(int denomax)
{
    int denom, repetend = 0;
    int i, j, value, counter;
    for (i = 3; i <= denomax; i += 2) {
        counter = 1;
        value = 10%i;
        j = denomax;
        while (value != 1 && j > 0) {
            value *= 10;
            value %= i;
            counter++;
            j--;
        }
        if (counter > repetend && j > 1) {
            repetend = counter;
            denom = i;
        }
    }
    return {denom, repetend};
}

int main()
{
    while (true) {
        int denom, repetend, denomax;
        cout << "Enter the maximum allowed absolute value of the denominator: ";
        std::cin >> denomax;
        if (denomax <= 0) {
            return 0;
        }
        if (denomax < 3) {
            cout << "No fraction with an absolute value of the denominator"
                 << " less than 3 can have a repetend.\n\n";
        } else {
            std::tie(denom, repetend) = maxRepetendDenom(denomax);
            std::string digits = repetend > 1 ? " digits" : " digit";
            cout << "A fraction with an absolute value of the denominator no greater "
                 << "than " << denomax << " will have the longest possible repetend "
                 << "(of " << repetend << digits << ") in its decimal expansion if it"
                 << " is in lowest terms and its denominator is " << denom << ".\n\n";
        }
    }
    return 0;
}
deleted 171 characters in body
Source Link
q-l-p
  • 189
  • 1
  • 10

This question is a variation of Problem 26 on Project Euler. Given the fact that the absolute value of the denominator can be no greater than 10,000, all irreducible fractions of the form n/9967 will have a digital representation with the longest possible repetend of 9966 digits.

The This is because 9967 is the largest full repetend primefull repetend prime smaller than 10,000 is 9967. Any irreducible fraction with a full repetend primes as denominator will generate the longest possible repetend.

This question is a variation of Problem 26 on Project Euler. Given the fact that the absolute value of the denominator can be no greater than 10,000, all irreducible fractions of the form n/9967 will have a digital representation with the longest possible repetend of 9966 digits.

The largest full repetend prime smaller than 10,000 is 9967. Any irreducible fraction with a full repetend primes as denominator will generate the longest possible repetend.

This question is a variation of Problem 26 on Project Euler. Given the fact that the absolute value of the denominator can be no greater than 10,000, all irreducible fractions of the form n/9967 will have a digital representation with the longest possible repetend of 9966 digits. This is because 9967 is the largest full repetend prime smaller than 10,000.

deleted 171 characters in body
Source Link
q-l-p
  • 189
  • 1
  • 10
Loading
added a loop in the c++ program and clarified the language
Source Link
q-l-p
  • 189
  • 1
  • 10
Loading
clarified the answer
Source Link
q-l-p
  • 189
  • 1
  • 10
Loading
Source Link
q-l-p
  • 189
  • 1
  • 10
Loading