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.
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:
This article is contributed by Anuj Chauhan(anuj0503). If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Check whether given floating point number is even or odd
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Find the smallest number whose digits multiply to a given number n
- Find the k-th smallest divisor of a natural number N
- Program to find GCD of floating point numbers
- K-th smallest element after removing given integers from natural numbers | Set 2
- Multiply the given number by 2 such that it is divisible by 10
- Number of compositions of a natural number
- Find if given number is sum of first n natural numbers
- Find all divisors of a natural number | Set 1
- Find all divisors of a natural number | Set 2
- Find sum of modulo K of first N natural number
- Sum of all proper divisors of a natural number
- Expressing a fraction as a natural number under modulo 'm'
- Largest natural number that can be printed with M characters
Improved By : nitin mittal