Enclosed sequence of digits in “()” in the floating-point representation expresses recurrence in the decimal representation.
For example, 1.(6) represents 1.666….
Input: N = “1.5”
Output: 3 / 2
The value of 3 / 2 will be equal to 1.5
Input: N = “1.(6)”
Output: 5 / 3
The value of 5 / 3 will be equal to 1.666… which is represented as 1.(6).
Approach: The idea is to use the Greatest Common Divisor of two numbers and some mathematical equations to solve the problem. Follow the below steps to solve the problem:
- Let there be x numbers after the decimal except for the recurring sequence.
- If there is no recurring sequence then multiply the given number with 10x and let the GCD of 10x and the resultant number be g. Print resultant divided by g as the numerator and 10x divided by g as the denominator.
For example, if N = “1.5” then x = 1.
Multiplying N with 10, the resultant will be 15 and the GCD of 10 and 15 is 3.
Therefore, print 15/3 = 5 as the numerator and 10/5 as the denominator.
- If the recurrence sequence is present, then multiply N with 10x. For example, if N = 23.98(231) multiply it with N*(102).
- Let the total number of digits in a sequence be y. For 102*N = 2398.(231), y becomes 3.
- Multiply 10y with N*10x. For 102*N = 2398.(231), multiply it with 103 i.e., 102*N*103 = 2398231.(231).
- Now, subtract, N*10y+x with N*10x and let the result be M. For the above example, 102*N*(103-1) = 2395833.
- Therefore, N = M / ((10x)*(10y – 1)). For the above example, N = 2395833 / 999000.
- Find the GCD of M and ((10x)*(10y – 1)) and print M / gcd as the numerator and ((10x)*(10y – 1)) as the denominator.
Below is the implementation of the above approach:
798611 / 33300
Time Complexity: O(log10N)
Auxiliary Space: O(1)
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.
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Convert the ASCII value sentence to its equivalent string
- Convert the given BCD to its equivalent Binary form
- Convert given Decimal number into an irreducible Fraction
- Program to convert Hexa-Decimal Number to its equivalent BCD
- Convert a sentence into its equivalent mobile numeric keypad sequence
- Convert decimal fraction to binary number
- Convert Binary fraction to Decimal
- Check whether two strings are equivalent or not according to given condition
- Check whether the binary equivalent of a number ends with given string or not
- Print given sentence into its equivalent ASCII form
- Largest proper fraction with sum of numerator and denominator equal to a given number
- First occurrence of a digit in a given fraction
- Find N fractions that sum upto a given fraction N/D
- Max count of unique ratio/fraction pairs in given arrays
- Decimal Equivalent of Gray Code and its Inverse
- Count of substrings whose Decimal equivalent is greater than or equal to K
- Check whether the binary equivalent of a number ends with "001" or not
- Maximize the decimal equivalent by flipping only a contiguous set of 0s
- Maximum rational number (or fraction) from an array
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.