Given a fraction, find a recurring sequence of digits if exists, otherwise, print “No recurring sequence”.**Examples:**

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

## We strongly recommend that you click here and practice it, before moving on to the solution.

**When does the fractional part repeat?**

Let us simulate the process of converting fractions to decimals. 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 the remainder/denominator to the result.
- Remainder = remainder % denominator.

At any moment, if the remainder becomes 0, we are done.

However, when there is a recurring sequence, the 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 the remainder ‘rem’ and if the remainder repeats at any point in time, the digits between the two occurrences 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 the C++ implementation of the above idea.

## C++

`// C++ program to find repeating sequence in a fraction` `#include <bits/stdc++.h>` `using` `namespace` `std;` `// This function returns repeating sequence of` `// a fraction. If repeating sequence doesn't` `// exits, then returns empty string` `string fractionToDecimal(` `int` `numr, ` `int` `denr)` `{` ` ` `string res; ` `// Initialize result` ` ` `// Create a map to store already seen remainders` ` ` `// remainder is used as key and its position in` ` ` `// result is stored as value. Note that we need` ` ` `// position for cases like 1/6. In this case,` ` ` `// the recurring sequence doesn't start from first` ` ` `// remainder.` ` ` `map <` `int` `, ` `int` `> mp;` ` ` `mp.clear();` ` ` `// Find first remainder` ` ` `int` `rem = numr%denr;` ` ` `// Keep finding remainder until either remainder` ` ` `// becomes 0 or repeats` ` ` `while` `( (rem!=0) && (mp.find(rem) == mp.end()) )` ` ` `{` ` ` `// Store this remainder` ` ` `mp[rem] = res.length();` ` ` `// Multiply remainder with 10` ` ` `rem = rem*10;` ` ` `// Append rem / denr to result` ` ` `int` `res_part = rem / denr;` ` ` `res += to_string(res_part);` ` ` `// Update remainder` ` ` `rem = rem % denr;` ` ` `}` ` ` `return` `(rem == 0)? ` `""` `: res.substr(mp[rem]);` `}` `// Driver code` `int` `main()` `{` ` ` `int` `numr = 50, denr = 22;` ` ` `string res = fractionToDecimal(numr, denr);` ` ` `if` `(res == ` `""` `)` ` ` `cout << ` `"No recurring sequence"` `;` ` ` `else` ` ` `cout << ` `"Recurring sequence is "` `<< res;` ` ` `return` `0;` `}` |

*chevron_right*

*filter_none*

## Java

`// Java program to find ` `// repeating sequence ` `// in a fraction` `import` `java.util.*;` `class` `GFG{` `// This function returns repeating ` `// sequence of a fraction. If ` `// repeating sequence doesn't` `// exits, then returns empty String` `static` `String fractionToDecimal(` `int` `numr, ` ` ` `int` `denr)` `{ ` ` ` `// Initialize result` ` ` `String res=` `""` `; ` ` ` `// Create a map to store already ` ` ` `// seen remainders. Remainder is ` ` ` `// used as key and its position in` ` ` `// result is stored as value. ` ` ` `// Note that we need position for ` ` ` `// cases like 1/6. In this case,` ` ` `// the recurring sequence doesn't ` ` ` `// start from first remainder.` ` ` `HashMap <Integer, ` ` ` `Integer> mp = ` `new` `HashMap<>();` ` ` `mp.clear();` ` ` `// Find first remainder` ` ` `int` `rem = numr%denr;` ` ` `// Keep finding remainder until` ` ` `// either remainder becomes 0 or repeats` ` ` `while` `((rem!=` `0` `) && ` ` ` `(!mp.containsValue(rem)))` ` ` `{` ` ` `// Store this remainder` ` ` `mp.put(rem, res.length());` ` ` `// Multiply remainder with 10` ` ` `rem = rem * ` `10` `;` ` ` `// Append rem / denr to result` ` ` `int` `res_part = rem / denr;` ` ` `res += String.valueOf(res_part);` ` ` `// Update remainder` ` ` `rem = rem % denr;` ` ` `}` ` ` ` ` `if` `(rem == ` `0` `)` ` ` `return` `" "` `;` ` ` `else` ` ` `if` `(mp.containsKey(rem))` ` ` `return` `res.substring(mp.get(rem));` ` ` ` ` `return` `""` `;` `}` `// Driver code` `public` `static` `void` `main(String[] args)` `{` ` ` `int` `numr = ` `50` `, denr = ` `22` `;` ` ` `String res = fractionToDecimal(numr, denr);` ` ` `if` `(res == ` `""` `)` ` ` `System.out.print(` `"No recurring sequence"` `);` ` ` `else` ` ` `System.out.print(` `"Recurring sequence is "` `+ res);` `}` `}` `// This code is contributed by gauravrajput1` |

*chevron_right*

*filter_none*

**Output : **

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Find the Nth digit in the proper fraction of two numbers
- Find N fractions that sum upto a given fraction N/D
- Greedy Algorithm for Egyptian Fraction
- Convert decimal fraction to binary number
- Convert Binary fraction to Decimal
- Fraction module in Python
- Largest proper fraction with sum of numerator and denominator equal to a given number
- as_integer_ratio() in Python for reduced fraction of a given rational
- First occurrence of a digit in a given fraction
- Maximum rational number (or fraction) from an array
- Represent the fraction of two numbers in the string format
- Expressing a fraction as a natural number under modulo 'm'
- Fraction
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Reduce the fraction to its lowest form
- Max count of unique ratio/fraction pairs in given arrays
- Convert given Decimal number into an irreducible Fraction
- k-th missing element in increasing sequence which is not present in a given sequence
- Minimum operations required to transform a sequence of numbers to a sequence where a[i]=a[i+2]