1

I'm working on a Rational number class in c++. The Rational number is defined by two int (numerator and denominator). I would like to display it properly as digit number. for now, I determine if the number is an "illimited" or a limited digit rational number.

Here is a little pseudo code to illustrate:

define print_rational(num, denom):
   if(isUnlimited(num, denom):
       ?
   else:
       //"limited" rational, no problem for them

I would like to display illimited numbers like this : print one time the repetitives digits, then "..." (Example : 1/3 -> 0.3..., 1/11 -> 0.09...)

So, is there an algorithm to find the block of digit who will be repeat in a rational number ?

Hugo Hismans
  • 173
  • 1
  • 1
  • 8
  • 1
    long division works by a series of divisions with quotient and remainder. When the same remainder pops up a second time (as it must by the pigeon hole principle since it is between 0 and denom - 1 inclusive) the process cycles and the same block repeats. Thus, it is essentially a problem of cycle-detection. – John Coleman May 13 '17 at 01:36
  • 1
    I find the same question answered. https://softwareengineering.stackexchange.com/questions/192070/what-is-a-efficient-way-to-find-repeating-decimal – Hugo Hismans May 13 '17 at 01:44
  • By "digit number" do you mean a number shown with decimal digits? You do you mean by "illimited" and "limited" here? Do you mean with finitely or infinitely many decimal places--i.e. terminating versus repeating? – Rory Daulton May 13 '17 at 11:05
  • Sorry for my approximative "english math". Math and english are both not in my specialities ! I get my answer, I just needed that someone remind me that long division algorithm exist. I guess I should read my primary courses more often ! I'll post my code as answer to close this question once I finished it. – Hugo Hismans May 13 '17 at 14:54

1 Answers1

0

Convert your result into string and then apply Longest repeated substring problem to it.

ayusha
  • 474
  • 4
  • 12