algorithm - Write a program that takes as input numerators and denominators of fractions and determines their repeating cycles -
for example :
76/25 = 3.04(0) 1 = number of digits in repeating cycle
5/43 = 0.(116279069767441860465) 21 = number of digits in repeating cycle
1/397 = 0.(00251889168765743073047858942065491183879093198992...) 99 = number of digits in repeating cycle
this might ugly solution, should work:
- simplify fraction using greatest common divisor (gcd) - optional , used simplify calculations.
do paper-and-pencil division. division encounters repeating remainder can break off, since after point numbers repeat. example:
5 / 27 = 0.185... 50 <------------------------------| -27 -> (1) | 230 | -216 -> (8) | 140 | -135 -> (5) | 50 <-------- same remainer here, can break off -27 -> (1)
sorry ugly ascii-"art".
Comments
Post a Comment