Given a fraction, find a recurring sequence of digits if it 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
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 implementation of the above idea.
C++
#include <bits/stdc++.h>
using namespace std;
string fractionToDecimal( int numr, int denr)
{
string res;
map< int , int > mp;
mp.clear();
int rem = numr % denr;
while ((rem != 0)
&& (mp.find(rem) == mp.end()))
{
mp[rem] = res.length();
rem = rem * 10;
int res_part = rem / denr;
res += to_string(res_part);
rem = rem % denr;
}
return (rem == 0) ? "" : res.substr(mp[rem]);
}
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;
}
|
Java
import java.util.*;
class GFG {
static String fractionToDecimal( int numr, int denr)
{
String res = "" ;
HashMap<Integer, Integer> mp = new HashMap<>();
mp.clear();
int rem = numr % denr;
while ((rem != 0 ) && (!mp.containsKey(rem)))
{
mp.put(rem, res.length());
rem = rem * 10 ;
int res_part = rem / denr;
res += String.valueOf(res_part);
rem = rem % denr;
}
if (rem == 0 )
return "" ;
else if (mp.containsKey(rem))
return res.substring(mp.get(rem));
return "" ;
}
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);
}
}
|
Python3
def fractionToDecimal(numr, denr):
res = ""
mp = {}
rem = numr % denr
while ((rem ! = 0 ) and (rem not in mp)):
mp[rem] = len (res)
rem = rem * 10
res_part = rem / / denr
res + = str (res_part)
rem = rem % denr
if (rem = = 0 ):
return ""
else :
return res[mp[rem]:]
numr, denr = 50 , 22
res = fractionToDecimal(numr, denr)
if (res = = ""):
print ( "No recurring sequence" )
else :
print ( "Recurring sequence is" , res)
|
C#
using System;
using System.Collections.Generic;
class GFG {
static string fractionToDecimal( int numr, int denr)
{
string res = "" ;
Dictionary< int , int > mp
= new Dictionary< int , int >();
int rem = numr % denr;
while ((rem != 0) && (!mp.ContainsKey(rem)))
{
mp[rem] = res.Length;
rem = rem * 10;
int res_part = rem / denr;
res += res_part.ToString();
rem = rem % denr;
}
if (rem == 0)
return "" ;
else if (mp.ContainsKey(rem))
return res.Substring(mp[rem]);
return "" ;
}
public static void Main( string [] args)
{
int numr = 50, denr = 22;
string res = fractionToDecimal(numr, denr);
if (res == "" )
Console.Write( "No recurring sequence" );
else
Console.Write( "Recurring sequence is " + res);
}
}
|
Javascript
<script>
function fractionToDecimal(numr, denr)
{
let res = "" ;
let mp = new Map();
mp.clear();
let rem = numr % denr;
while ((rem != 0) && (!mp.has(rem)))
{
mp.set(rem, res.length);
rem = rem * 10;
let res_part = Math.floor(rem / denr);
res += res_part.toString();
rem = rem % denr;
}
if (rem == 0)
return "" ;
else if (mp.has(rem))
return res.substr(mp.get(rem));
return "" ;
}
let numr = 50, denr = 22;
let res = fractionToDecimal(numr, denr);
if (res == "" )
document.write( "No recurring sequence" );
else
document.write( "Recurring sequence is "
+ res);
</script>
|
OutputRecurring sequence is 27
Time Complexity : O(N)
Auxiliary Space : O(N) , as we use map as extra space.
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.