Write a function that takes one double input value and returns only the repeating decimal, if any, as a string. Only decimals for which all digits are part of the repeating pattern will be counted. (See the 3rd example below.) Inputs will be in the range [0 1].
If no repeating decimal is found, the function should return an empty string. Of course the repeating decimal may break down in the last bit due to floating point arithmetic, but we will ignore that.
Examples:
T = repeatingdec(7/11) % Returns T = '63' T = repeatingdec(1/3) % Returns T = '3' T = repeatingdec(5/6) % Returns T = '' T = repeatingdec(0) % Returns T = '0'
Solution Stats
Problem Comments
Solution Comments
Show commentsProblem Recent Solvers14
Suggested Problems
-
Select every other element of a vector
36255 Solvers
-
Determine whether a vector is monotonically increasing
22893 Solvers
-
Flag largest magnitude swings as they occur
690 Solvers
-
Right Triangle Side Lengths (Inspired by Project Euler Problem 39)
2039 Solvers
-
Given a matrix, swap the 2nd & 3rd columns
1263 Solvers
More from this Author6
Problem Tags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
The tests are inconsistent with the statement of the problem. Finite strings like ..123451234512345 only repeat to the end of the string, then switch to zeros.
The problem should specify the precision required, there are numbers that cannot be represented with doubles and are loosing significant digits. And this really becomes an issue when your test suite use less significant digits to identify repeating patterns such as in cases 7 and 10. I was able to overcome this issue by using 14 digits, but some people used 16 (If someone else is having problems that's the way to go). Moreover, Andrew is right, a number like 0.333333 is technically not a repeating decimal.