Generate the sequence number of bases in which n
is a palindrome (OEIS A126071).
Specifically, the sequence is defined as follows: given a number n
, express it in base a
for a = 1,2, ..., n
, and count how many of those expressions are palindromic. "Palindromic" is understood in terms of reversing the base-a
digits of the expression as atomic units (thanks, @Martin Büttner). As an example, consider n= 5
:
a=1
: the expression is11111
: palindromica=2
: the expression is101
: palindromica=3
: the expression is12
: not palindromica=4
: the expression is11
: palindromica=5
: the expression is10
: not palindromic
Therefore the result for n=5
is 3
. Note that OEIS uses bases 2, ..., n+1
instead of 1, ..., n
(thanks, @beaker). It's equivalent, because the expressions in base 1
and n+1
are always palindromic.
The first values of the sequence are
1, 1, 2, 2, 3, 2, 3, 3, 3, 4, 2, 3, 3, 3, 4, 4, 4, 4, 2, 4, 5, ...
Input is a positive integer n
. Output is the first n
terms of the sequence.
The program should theoretically work (given enough time and memory) for any n
up to limitations caused by your default data type in any internal computations.
All functions allowed. Lowest number of bytes wins.