← Back to challenges

Repeating Cycle

JavaScriptHardalgorithmscontrol_flowmathnumbers

Instructions

Given an integer n, create a function that returns the length of the repeating cycle of the sequence 1/n. If the cycle is non-repetitive, return -1.

repeatingCycle(13) ➞ 6
// 1/13 = 0.076923 076923 076923 076923 ...
// length of `076923` is 6.

Examples

repeatingCycle(5) ➞ -1
// 1/5 = 0.2 (non-repeative)

repeatingCycle(33) ➞ 2
// 1/33 = 0.03 03 03 03 03 03 03 03
// length of `03` is 2

repeatingCycle(197) ➞ 98

Notes

Return -1 if the repeating cycle does not start from the first decimal place. As an example, 1/22 = 0.0 45 45 45 45, your function should return -1 in this case.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.