Friday, 21 August 2020

How To Find Recurring Sequence In A Fraction

Suppose we have a decimal number. We need to check if the fraction part contains any non-ending repeating sequence or it ends normally.
Let's take an example. Numerator is 5 and denominator is 33.
5 / 33 = 0
5 % 33 = 5

So we need to get the fraction part.
5 * 10 = 50
50 / 33 = 1
Now we have 0.1

50 % 33 = 17
17 * 10 = 170
170 / 33 = 5
So now we have 0.15

170 % 33 = 5
5 * 10 = 50
But we have already done 50 / 33. So if you just think through, we have come back to a previous state where numerator & denominator are same. So we know if we do state transition, we will come back to this state again.
So this is the starting point of the recurring pattern which will continuously go on & never end. So our answer is (15).
Hope it helps in understanding the concept. 

No comments:

Post a comment

How To Solve "Caused by: org.hibernate.HibernateException: Missing table" When Table Is Present In Database

If you are using JPA or Hibernate directly and got that exception while starting your application, there is one obvious reason for that. You...