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 email@example.com. 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.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- 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
- Sum of two large Floating-point numbers
- 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
- Multiply perfect number
- Queries to multiply the given subarray with given number X and print sum
- Number of compositions of a natural number
- Find if given number is sum of first n natural numbers
- Find the Nth natural number which is not divisible by A
- Find sum of modulo K of first N natural number
- Sum of all proper divisors of a natural number
- Find all divisors of a natural number | Set 2
- Find all divisors of a natural number | Set 1
- Expressing a fraction as a natural number under modulo 'm'
- Number of pairs from the first N natural numbers whose sum is divisible by K
- Largest natural number that can be printed with M characters
Improved By : nitin mittal