Given three integers a, b and c, find the first occurrence of c in a/b after the decimal point. If it does not exists, print -1.
Input : a = 2 b = 3 c = 6 Output : 1 Explanation: 0.666666.. so 6 occurs at first place of a/b after decimal point Input : a = 1 b = 4 c = 5 Output : 2 Explanation: 1 / 4 = 0.25 which gives 5's position to be 2.
A naive approach will be to perform the division and keep the decimal part and iterate and check if the given number exists or not. This will not work well when divisions such as 2/3 is done as it yields 0.666666666, but in programming language it will round it off to 0.666667 so we get a 7 also which does not exists in the original a/b
An efficient approach will be the mathematical one, if we modulate every-time a by b and multiply it with 10 we get the integers after the decimal part every-time. The number of modulations required will be b as it will have a maximum of b integers after decimal point. So, we compare it with c and get our desired value if it is present.
Below is the implementation of the above approach :
- 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
- Print first N terms of series (0.25, 0.5, 0.75, ...) in fraction representation
- Count the occurrence of digit K in a given number N using Recursion
- Count of N-digit numbers having digit XOR as single digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Find the remainder when First digit of a number is divided by its Last digit
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Largest proper fraction with sum of numerator and denominator equal to a given number
- as_integer_ratio() in Python for reduced fraction of a given rational
- Find N fractions that sum upto a given fraction N/D
- Max count of unique ratio/fraction pairs in given arrays
- Convert given Decimal number into an irreducible Fraction
- Convert given Float value to equivalent Fraction
- Count the occurrence of Nth term in first N terms of Van Eck's sequence
- Greedy Algorithm for Egyptian Fraction
- Convert decimal fraction to binary number
- Fraction module in Python
- 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 firstname.lastname@example.org. 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.
Improved By : vt_m