%I #32 May 04 2015 22:02:53
%S 1,2,3,4,6,5,7,8,10,12,9,11,14,13,15,16,18,20,17,19,22,24,21,23,26,30,
%T 25,28,27,29,31,32,34,36,33,35,38,40,37,39,42,46,48,41,44,43,45,47,50,
%U 54,58,49,52,51,53,56,60,55,57,62,59,61,63,64,66,68,65,67,70,72,69
%N Start with 1. For each term m, the next term is the smallest positive integer k such that k - (sum of base 2 digits of k) = m. If no such k exists, use the smallest natural number not already in the sequence.
%C The sequence is a permutation of the positive integers.
%H Antti Karttunen, <a href="/A218252/b218252.txt">Table of n, a(n) for n = 1..8192</a>
%H <a href="/index/Per#IntegerPermutation">Index entries for sequences that are permutations of the natural numbers</a>
%e To obtain the 2nd term, take the first, 1. What is the smallest integer k so that k - the number of 1's in k's binary representation is 1? The answer, obviously, is 2. [A213723(1) = 2.]
%e There is no number that is 2 more than its binary weight [as A213723(2) = 0], therefore we just take 3 as the next term.
%e Following 3 we can choose either 4 or 5, but 4 is smaller, and is thus the next term of the sequence. [A213723(3) = 4.]
%o (Scheme, with defineperm1-macro from _Antti Karttunen_'s IntSeq-library)
%o (defineperm1 (A218252 n) (cond ((<= n 1) n) ((A213723 (A218252 (- n 1))) => (lambda (next_maybe) (if (not (zero? next_maybe)) next_maybe (let loop ((i (A000012 (- n 1)))) (if (not-lte? (A257683 i) n) i (loop (+ i 1)))))))))
%o ;; We consider a > b (i.e. not less than b) also in case a is nil.
%o ;; (Because of the stateful caching system used by defineperm1-macro):
%o (define (not-lte? a b) (cond ((not (number? a)) #t) (else (> a b))))
%o ;; _Antti Karttunen_, May 04 2015
%Y Inverse permutation: A257683.
%Y Cf. A000120, A005187, A011371, A213723, A218253, A218254.
%Y Cf. also A257676.
%K nonn
%O 1,2
%A _Nico Brown_, Oct 24 2012
%E Name slightly edited and links to A213723 in examples added by _Antti Karttunen_, May 04 2015
|