Given a fraction, find recurring sequence of digits if exists, otherwise print “No recurring sequence”.
Input : Numerator = 8, Denominator = 3 Output : Recurring sequence is 6 Explanation : 8/3 = 2.66666666....... Input : Numerator = 50, Denominator = 22 Output : Recurring sequence is 27 Explanation : 50/22 = 2.272727272..... Input : Numerator = 11, Denominator = 2 Output : No recurring sequence Explanation : 11/2 = 5.5
When does the fractional part repeat ?
Let us simulate the process of converting fraction to decimal. Let us look at the part where we have already figured out the integer part which is floor(numerator/denominator). Now we are left with ( remainder = numerator%denominator ) / denominator.
If you remember the process of converting to decimal, at each step we do the following :
- Multiply the remainder by 10.
- Append remainder / denominator to result.
- Remainder = remainder % denominator.
At any moment, if remainder becomes 0, we are done.
However, when there is a recurring sequence, remainder never becomes 0. For example if you look at 1/3, the remainder never becomes 0.
Below is one important observation :
If we start with remainder ‘rem’ and if the remainder repeats at any point of time, the digits between the two occurrence of ‘rem’ keep repeating.
So the idea is to store seen remainders in a map. Whenever a remainder repeats, we return the sequence before the next occurrence. Below is C++ implementation of above idea.
Recurring sequence is 27
This article is contributed by Dhruv Mahajan. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find F(n) when F(i) and F(j) of a sequence are given
- Program to find sum of the given sequence
- Find n-th term in sequence 1, 1, 2, 1, 2, 3, 1, 2, 3, 4, ....
- Find nth term of the Dragon Curve Sequence
- Find if the given number is present in the infinite sequence or not
- Find element position in given monotonic sequence
- Find the final sequence of the array after performing given operations
- Find a sequence of N prime numbers whose sum is a composite number
- Fraction module in Python
- First occurrence of a digit in a given fraction
- Find the lexicographically smallest sequence which can be formed by re-arranging elements of second array
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]
- Convert Binary fraction to Decimal