Count number of digits after decimal on dividing a number
Last Updated :
07 Jul, 2021
We are given two numbers A and B. We need to calculate the number of digits after decimal. If in case the numbers are irrational then print “INF”.
Examples:
Input : x = 5, y = 3
Output : INF
5/3 = 1.666....
Input : x = 3, y = 6
Output : 1
3/6 = 0.5
The idea is simple we follow school division and keep track of remainders while dividing one by one. If remainder becomes 0, we return count of digits seen after decimal. If remainder repeats, we return INF.
C++
#include <bits/stdc++.h>
using namespace std;
int count( int x, int y)
{
int ans = 0;
unordered_map< int , int > m;
while (x % y != 0) {
x = x % y;
ans++;
if (m.find(x) != m.end())
return -1;
m[x] = 1;
x = x * 10;
}
return ans;
}
int main()
{
int res = count(1, 2);
(res == -1)? cout << "INF" : cout << res;
cout << endl;
res = count(5, 3);
(res == -1)? cout << "INF" : cout << res;
cout << endl;
res = count(3, 5);
(res == -1)? cout << "INF" : cout << res;
return 0;
}
|
Java
import java.util.*;
class GFG
{
static int count( int x, int y)
{
int ans = 0 ;
Map<Integer,Integer> m = new HashMap<>();
while (x % y != 0 )
{
x = x % y;
ans++;
if (m.containsKey(x))
return - 1 ;
m.put(x, 1 );
x = x * 10 ;
}
return ans;
}
public static void main(String[] args)
{
int res = count( 1 , 2 );
if ((res == - 1 ))
System.out.println( "INF" );
else
System.out.println(res);
res = count( 5 , 3 );
if ((res == - 1 ))
System.out.println( "INF" );
else
System.out.println(res);
res = count( 3 , 5 );
if ((res == - 1 ))
System.out.println( "INF" );
else
System.out.println(res);
}
}
|
Python3
def count(x, y):
ans = 0
m = dict ()
while x % y ! = 0 :
x % = y
ans + = 1
if x in m:
return - 1
m[x] = 1
x * = 10
return ans
if __name__ = = "__main__" :
res = count( 1 , 2 )
print ( "INF" ) if res = = - 1 else print (res)
res = count( 5 , 3 )
print ( "INF" ) if res = = - 1 else print (res)
res = count( 3 , 5 )
print ( "INF" ) if res = = - 1 else print (res)
|
C#
using System;
using System.Collections.Generic;
class GFG
{
static int count( int x, int y)
{
int ans = 0;
Dictionary< int , int > m = new Dictionary< int , int >();
while (x % y != 0)
{
x = x % y;
ans++;
if (m.ContainsKey(x))
return -1;
m.Add(x, 1);
x = x * 10;
}
return ans;
}
public static void Main(String[] args)
{
int res = count(1, 2);
if ((res == -1))
Console.WriteLine( "INF" );
else
Console.WriteLine(res);
res = count(5, 3);
if ((res == -1))
Console.WriteLine( "INF" );
else
Console.WriteLine(res);
res = count(3, 5);
if ((res == -1))
Console.WriteLine( "INF" );
else
Console.WriteLine(res);
}
}
|
Javascript
<script>
function count(x,y)
{
let ans = 0;
let m = new Map();
while (x % y != 0)
{
x = x % y;
ans++;
if (m.has(x))
return -1;
m.set(x, 1);
x = x * 10;
}
return ans;
}
let res = count(1, 2);
if ((res == -1))
document.write( "INF" + "<br>" );
else
document.write(res+ "<br>" );
res = count(5, 3);
if ((res == -1))
document.write( "INF" + "<br>" );
else
document.write(res+ "<br>" );
res = count(3, 5);
if ((res == -1))
document.write( "INF" + "<br>" );
else
document.write(res+ "<br>" );
</script>
|
Output:
1
INF
1
Time Complexity: O(N * log(N) )
Auxiliary Space: O(N)
Share your thoughts in the comments
Please Login to comment...