( S <- stern_brocot_seq(92) )
# 1, 1, 2, 1, 3, 2, 3, 1, 4, 3, 5, 2, 5, 3, 4, 1, 5, 4, 7,
# 3, 8, 5, 7, 2, 7, 5, 8, 3, 7, 4, 5, 1, 6, 5, 9, 4, 11, 7, 10,
# 3, 11, 8, 13, 5, 12, 7, 9, 2, 9, 7, 12, 5, 13, 8, 11, 3, 10, 7, 11,
# 4, 9, 5, 6, 1, 7, 6, 11, 5, 14, 9, 13, 4, 15, 11, 18, 7, 17, 10, 13,
# 3, 14, 11, 19, 8, 21, 13, 18, 5, 17, 12, 19, 7, ...
table(S)
## S
## 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 17 18 19 21
## 7 5 9 7 12 3 11 5 5 3 7 3 5 2 1 2 2 2 1
which(S == 1) # 1 2 4 8 16 32 64
if (FALSE) {
# Find the rational number p/q in S
# note that 1/2^n appears in position S[c(2^(n-1), 2^(n-1)+1)]
occurs <- function(p, q, s){
# Find i such that (p, q) = s[i, i+1]
inds <- seq.int(length = length(s)-1)
inds <- inds[p == s[inds]]
inds[q == s[inds + 1]]
}
p = 3; q = 7 # 3/7
occurs(p, q, S) # S[28, 29]
'%//%' <- function(p, q) gmp::as.bigq(p, q)
n <- length(S)
S[1:(n-1)] %//% S[2:n]
## Big Rational ('bigq') object of length 91:
## [1] 1 1/2 2 1/3 3/2 2/3 3 1/4 4/3 3/5
## [11] 5/2 2/5 5/3 3/4 4 1/5 5/4 4/7 7/3 3/8 ...
as.double(S[1:(n-1)] %//% S[2:n])
## [1] 1.000000 0.500000 2.000000 0.333333 1.500000 0.666667 3.000000
## [8] 0.250000 1.333333 0.600000 2.500000 0.400000 1.666667 0.750000 ...
}
Run the code above in your browser using DataLab