forked from gideo/CodeWars
-
Notifications
You must be signed in to change notification settings - Fork 0
/
6kyu_1n-Cycle.js
31 lines (24 loc) · 869 Bytes
/
6kyu_1n-Cycle.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// 6kyu - 1/n-Cycle
// Let be n an integer prime with 10 e.g. 7.
// 1/7 = 0.142857 142857 142857 ....
// We see that the decimal part has a cycle: 142857. The length of this cycle is 6. In the same way:
// 1/11 = 0.09 09 09 .... Cycle length is 2.
// Task
// Given an integer n (n > 1), the function cycle(n) returns the length of the cycle if n and 10 are coprimes, otherwise returns -1.
// Exemples:
// cycle(5) = -1
// cycle(13) = 6 -> 0.076923 076923 0769
// cycle(21) = 6 -> 0.047619 047619 0476
// cycle(27) = 3 -> 0.037 037 037 037 0370
// cycle(33) = 2 -> 0.03 03 03 03 03 03 03 03
// cycle(37) = 3 -> 0.027 027 027 027 027 0
// cycle(94) = -1
// cycle(22) = -1 since 1/22 ~ 0.0 45 45 45 45 ...
function cycle(n) {
if (n % 2 == 0 || n % 5 == 0) return -1;
let i = 0, val = 1;
while (++i) {
val = val * 10 % n;
if (val == 1) return i;
}
}