Smallest number to multiply to convert floating point to natural
Given a positive floating point number n, the task is to find the smallest integer k, such that when we multiply k with n, we get a natural number.
Examples:
Input : 30.25
Output : 4
30.25 * 4 = 321, there is no number less than 4
which can convert 30.25 into natural number.
Input : 5.5
Output : 2
5.5 * 2 = 11, there is no number less than 2
which can convert 5.5 into natural number.
Input : 5.33
Output : 100
The idea is to convert given floating point number into a fraction (not necessarily in reduced form) and find the GCD of numerator and denominator. For example, if input floating point number is 30.25, we convert into fraction as 3025/100. This can be easily done by finding the position of dot.
Finally to get the answer, we divide the denominator of the converted fraction by GCD of denominator and numerator. For example, GCD of 3025 and 100 is 25. We divide 100 by 25 and get the answer as 4.
Below is implementation of this approach:
C++
#include<bits/stdc++.h>
using namespace std;
int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
int findnum(string &str)
{
int n = str.length();
int count_after_dot = 0;
bool dot_seen = false ;
int num = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] != '.' )
{
num = num*10 + (str[i] - '0' );
if (dot_seen == true )
count_after_dot++;
}
else
dot_seen = true ;
}
if (dot_seen == false )
return 1;
int dem = ( int ) pow (10, count_after_dot);
return (dem / gcd(num, dem));
}
int main()
{
string str = "5.125" ;
cout << findnum(str) << endl;
return 0;
}
|
Java
class GFG {
static int gcd( int a, int b) {
if (b == 0 )
return a;
return gcd(b, a % b);
}
static int findnum(String str) {
int n = str.length();
int count_after_dot = 0 ;
boolean dot_seen = false ;
int num = 0 ;
for ( int i = 0 ; i < n; i++) {
if (str.charAt(i) != '.' ) {
num = num * 10 + (str.charAt(i) - '0' );
if (dot_seen == true )
count_after_dot++;
} else
dot_seen = true ;
}
if (dot_seen == false )
return 1 ;
int dem = ( int )Math.pow( 10 , count_after_dot);
return (dem / gcd(num, dem));
}
public static void main(String[] args) {
String str = "5.125" ;
System.out.print(findnum(str));
}
}
|
Python3
import math
def gcd(a, b):
if (b = = 0 ):
return a
return gcd(b, a % b)
def findnum( str ):
n = len ( str )
count_after_dot = 0
dot_seen = 0
num = 0
for i in range (n):
if ( str [i] ! = '.' ):
num = num * 10 + int ( str [i])
if (dot_seen = = 1 ):
count_after_dot + = 1
else :
dot_seen = 1
if (dot_seen = = 0 ):
return 1
dem = int (math. pow ( 10 , count_after_dot))
return (dem / / gcd(num, dem))
str = "5.125"
print (findnum( str ))
|
C#
using System;
class GFG {
static int gcd( int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static int findnum(String str)
{
int n = str.Length;
int count_after_dot = 0;
bool dot_seen = false ;
int num = 0;
for ( int i = 0; i < n; i++)
{
if (str[i] != '.' )
{
num = num * 10 + (str[i] - '0' );
if (dot_seen == true )
count_after_dot++;
}
else
dot_seen = true ;
}
if (dot_seen == false )
return 1;
int dem = ( int )Math.Pow(10, count_after_dot);
return (dem / gcd(num, dem));
}
public static void Main()
{
String str = "5.125" ;
Console.Write(findnum(str));
}
}
|
PHP
<?php
function gcd( $a , $b )
{
if ( $b == 0)
return $a ;
return gcd( $b , $a % $b );
}
function findnum( $str )
{
$n = strlen ( $str );
$count_after_dot = 0;
$dot_seen = false;
$num = 0;
for ( $i = 0; $i < $n ; $i ++)
{
if ( $str [ $i ] != '.' )
{
$num = $num *10 + ( $str [ $i ] - '0' );
if ( $dot_seen == true)
$count_after_dot ++;
}
else
$dot_seen = true;
}
if ( $dot_seen == false)
return 1;
$dem = pow(10, $count_after_dot );
return ( $dem / gcd( $num , $dem ));
}
{
$str = "5.125" ;
echo findnum( $str ) ;
return 0;
}
?>
|
Javascript
<script>
function gcd(a , b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
function findnum( str)
{
var n = str.length;
var count_after_dot = 0;
let dot_seen = false ;
var num = 0;
for (i = 0; i < n; i++) {
if (str.charAt(i) != '.' ) {
num = num * 10 + (str.charAt(i) - '0' );
if (dot_seen == true )
count_after_dot++;
} else
dot_seen = true ;
}
if (dot_seen == false )
return 1;
var dem = parseInt( Math.pow(10, count_after_dot));
return (dem / gcd(num, dem));
}
let str = "5.125" ;
document.write(findnum(str));
</script>
|
Output:
8
Time Complexity: O(n)
Auxiliary Space: O(log(min(a,b)))
Last Updated :
23 Jun, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...