Given two integers representing the Numerator and Denominator of a fraction, return the fraction in string format. If the fractional part is repeating, enclose the repeating part in parentheses.
Input: Numerator = 1, Denominator = 2 Output: "0.5" 1/2 = 0.5 with no repeating part. Input: Numerator = 50, Denominator = 22 Output: "2.(27)" 50/22 = 2.27272727... Since fractional part (27) is repeating, it is enclosed in parentheses.
Prerequisites :Recurring Sequence in a Fraction
Approach :The idea is to first calculate the integral quotient (absolute part before decimal point) and then calculate the fractional part. To check if the fractional part is repeating, insert the remainder (numerator % denominator) in a map with key as remainder and value as the index position at which this remainder occurs. If at any point of time, the remainder becomes zero, then there doesn’t exist a repeating fraction otherwise if the remainder is already found in the map, then there exists a repeating fraction.
Below is the implementation of above approach.
- Find ΔX which is added to numerator and denominator both of fraction (a/b) to convert it to another fraction (c/d)
- Represent a number as sum of minimum possible psuedobinary numbers
- First occurrence of a digit in a given fraction
- Fraction module in Python
- Greedy Algorithm for Egyptian Fraction
- Reduce the fraction to its lowest form
- Convert Binary fraction to Decimal
- Find Recurring Sequence in a Fraction
- Represent n as the sum of exactly k powers of two | Set 2
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Expressing a fraction as a natural number under modulo 'm'
- Maximum rational number (or fraction) from an array
- as_integer_ratio() in Python for reduced fraction of a given rational
- Convert decimal fraction to binary number
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.