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.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Fast method to calculate inverse square root of a floating point number in IEEE 754 format
- Check whether given floating point number is even or odd
- Program to find GCD of floating point numbers
- Sum of two large Floating-point numbers
- Find the smallest number whose digits multiply to a given number n
- Rotation of a point about another point in C++
- Reflection of a point at 180 degree rotation of another point
- Find the k-th smallest divisor of a natural number N
- Multiply the given number by 2 such that it is divisible by 10
- Queries to multiply the given subarray with given number X and print sum
- Multiply perfect number
- K-th smallest element after removing given integers from natural numbers | Set 2
- Smallest number to be subtracted to convert given number to a palindrome
- Efficient way to multiply with 7
- Multiply two integers without using multiplication, division and bitwise operators, and no loops
- Russian Peasant (Multiply two numbers using bitwise operators)
- Multiply two polynomials
- Multiply Large Numbers represented as Strings
- Multiply large integers under large modulo
- Ways to multiply n elements with an associative operation
Improved By : nitin mittal