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.
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)
- Find the Nth digit in the proper fraction of two numbers
- Represent K^N as the sum of exactly N numbers
- Represent N as sum of K even numbers
- Represent K as sum of N-bonacci numbers
- Represent N as sum of K odd numbers with repetitions allowed
- Represent N as sum of K even or K odd numbers with repetitions allowed
- Represent the given number as the sum of two composite numbers
- Represent a number as sum of minimum possible psuedobinary numbers
- Minimum count of numbers required from given array to represent S
- Count of sticks required to represent the given string
- Sort numbers based on count of letters required to represent them in words
- Fraction module in Python
- First occurrence of a digit in a given fraction
- Find Recurring Sequence in a Fraction
- Find N fractions that sum upto a given fraction N/D
- Reduce the fraction to its lowest form
- Greedy Algorithm for Egyptian Fraction
- Convert Binary fraction to Decimal