-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecaman-like_sequence.sf
41 lines (30 loc) · 1.07 KB
/
recaman-like_sequence.sf
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
#!/usr/bin/ruby
# Daniel "Trizen" Șuteu
# Date: 14 January 2019
# https://github.com/trizen
# A Recamán-like sequence, defined as:
# a(0) = 0
# a(n) = a(a(n-1) mod n) + n, if not already in the sequence,
# a(n) = a(a(n-1) mod n), otherwise.
# See also:
# https://oeis.org/A005132
func recamán_like_sequence_first(n) {
var a = [0]
var s = Hash()
for k in (1 ..^ n) {
var t1 = (a[a[k-1]%k] + k)
var t2 = (a[a[k-1]%k])
if (!s.has(t1)) {
a[k] = t1
s{t1} = true
}
else {
a[k] = t2
s{t2} = true
}
}
return a
}
say recamán_like_sequence_first(100)
__END__
[0, 1, 3, 0, 4, 9, 6, 13, 17, 26, 16, 20, 29, 0, 14, 14, 30, 0, 18, 37, 0, 21, 43, 23, 47, 68, 56, 3, 28, 57, 33, 34, 35, 36, 3, 0, 0, 0, 38, 77, 40, 81, 119, 79, 44, 89, 125, 34, 51, 52, 53, 54, 55, 3, 0, 0, 0, 0, 58, 117, 60, 121, 179, 66, 67, 3, 0, 0, 0, 69, 139, 71, 143, 212, 141, 75, 151, 218, 257, 0, 80, 161, 82, 165, 245, 160, 227, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102]