-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathreciprocal_cycle_length.pl
executable file
·47 lines (39 loc) · 1.04 KB
/
reciprocal_cycle_length.pl
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/usr/bin/perl
# Daniel "Trizen" Șuteu
# Date: 17 May 2020
# https://github.com/trizen
# Algorithm for finding the length of the recurring cycle of 1/n in base b.
use 5.020;
use ntheory qw(:all);
use experimental qw(signatures);
sub reciprocal_cycle_length ($n, $base = 10) {
for (my $g = gcd($n, $base) ; $g > 1 ; $g = gcd($n, $base)) {
$n /= $g;
}
($n == 1) ? 0 : znorder($base, $n);
}
foreach my $n (1 .. 20) {
my $r = reciprocal_cycle_length($n);
say "1/$n has cycle length of $r";
}
__END__
1/1 has cycle length of 0
1/2 has cycle length of 0
1/3 has cycle length of 1
1/4 has cycle length of 0
1/5 has cycle length of 0
1/6 has cycle length of 1
1/7 has cycle length of 6
1/8 has cycle length of 0
1/9 has cycle length of 1
1/10 has cycle length of 0
1/11 has cycle length of 2
1/12 has cycle length of 1
1/13 has cycle length of 6
1/14 has cycle length of 6
1/15 has cycle length of 1
1/16 has cycle length of 0
1/17 has cycle length of 16
1/18 has cycle length of 1
1/19 has cycle length of 18
1/20 has cycle length of 0