OFFSET
0,3
COMMENTS
Without the initial 0, a(n) is the lexicographically minimal sequence of distinct positive integers such that all values of a(n) mod n are distinct and nonnegative. - Ivan Neretin, Apr 27 2015
A002487(n)/A002487(n+1), n > 0, runs through all the reduced nonnegative rationals exactly once. A002487 is the Stern's sequence. Permutation from denominators (A002487(n+1))
1 2 1 3 2 3 1 4 3 5 2 5 3 4 1
where labels are
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
to numerators (A002487(n))
1 1 2 1 3 2 3 1 4 3 5 2 5 3 4
where changed labels are
1 3 2 7 4 5 6 15 8 9 10 11 12 13 14
LINKS
MAPLE
a := n -> if n < 2 then n elif convert(convert(n, base, 2), `+`) = 1 then 2*n-1 else n-1 fi: seq(a(n), n=0..70); # Peter Luschny, Jul 16 2016
MATHEMATICA
Table[Which[n < 2, n, IntegerQ[Log[2, n]], 2 n - 1, True, n - 1], {n, 0, 70}] (* Michael De Vlieger, Apr 27 2015 *)
PROG
(MIT/GNU Scheme) (define (A153151 n) (cond ((< n 2) n) ((pow2? n) (- (* 2 n) 1)) (else (- n 1))))
(define (pow2? n) (and (> n 0) (zero? (A004198bi n (- n 1)))))
(Python)
def ok(n): return n&(n - 1)==0
def a(n): return n if n<2 else 2*n - 1 if ok(n) else n - 1 # Indranil Ghosh, Jun 09 2017
(R)
nmax <- 126 # by choice
a <- c(1, 3, 2)
for(n in 3:nmax) a[n+1] <- n
for(m in 0:floor(log2(nmax))) a[2^m] <- 2^(m+1) - 1
a <- c(0, a)
# Yosu Yurramendi, Sep 05 2020
CROSSREFS
KEYWORD
nonn,base
AUTHOR
Antti Karttunen, Dec 20 2008
STATUS
approved