# Numbers that are palindromes in N bases

Given an non-negative integer n >= 0, output forever the sequence of integers x_i >= 3 that are palindromes in exactly n different bases b, where the base can be 2 <= b <= x_i-2.

This is basically the inverse of OEIS A126071, where you output which indices in that sequence have the value n. It's a bit different, because I changed it so you ignore bases b = x_i-1, x_i, x_i+1, since the results for those bases are always the same (the values are always palindromes or always not). Also, the offset is different.

x_i is restricted to numbers >= 3 so that the first term of the result for each n is A037183.

Note that the output format is flexible, but the numbers should be delimited in a nice way.

Examples:

n   seq
0   3 4 6 11 19 47 53 79 103 137 139 149 163 167 ...
1   5 7 8 9 12 13 14 22 23 25 29 35 37 39 41 43 49 ...
2   10 15 16 17 18 20 27 30 31 32 33 34 38 44 ...
3   21 24 26 28 42 45 46 50 51 54 55 56 57 64 66 68 70 ...
4   36 40 48 52 63 65 85 88 90 92 98 121 128 132 136 138 ...
5   60 72 78 84 96 104 105 108 112 114 135 140 156 162 164 ...
10  252 400 420 432 510 546 600 648 784 800 810 816 819 828 858 882 910 912 1040 1056 ...


So for n=0, you get the output of this challenge (starting at 3), because you get numbers that are palindromes in n=0 bases.

For n=1, 5 is a palindrome in base 2, and that's the only base 2 <= b <= (5-2) that it's a palindrome in. 7 is a palindrome in base 2, and that's the only base 2 <= b <= (7-2) that it's a palindrome in. Etc.

Iff your language does not support infinite output, you may take another integer z as input and output the first z elements of the sequence, or all elements less than z. Whichever you prefer. Please state which you used in your answer if this is the case.

Related

• To be 100% clear, the numbers that are output must be palindromes in exactly n bases, not n or more bases? – Mike Bufardeci Mar 3 '17 at 20:47
• Yes. That is correct. So the union of all sequences for all values of n is the set of integers >=3. – mbomb007 Mar 3 '17 at 20:48

# Jelly, 18 bytes

Ṅ‘µ‘µbRŒḂ€S_2⁼³µ?ß


Try it online! - the online interpreter will timeout at 60 seconds then flush it's output (unless it has a cached copy), offline it will print each in turn.

### How?

Evaluates numbers from n up, printing them if they are in the sequence. Note that the first number in any output will be greater than n since otherwise the range of b is not great enough, so there is no need to seed the process with 3. Also note that the number of palindromes from base 2 to xi-2 inclusive is just two less than the number of palindromes from base 1 to x.

Ṅ‘µ‘µbRŒḂ€S_2⁼³µ?ß - Main link: n
AµBµC         µ?  - ternary if: if C then A else B
R            - range: [1,2,3...,n]
b             - n converted to those bases
ŒḂ€         - is palindromic? for €ach
S        - sum: counts the 1s
_2      - subtract 2: throw away the truthy unary and base n-1 effect
⁼³    - equals input: Does this equal the ORIGINAL n?
Ṅ                  - if so: print n
‘                 -        increment n
‘               - else: just increment n
ß - calls this link as a monad, with the incremented n.


# Mathematica, 80 71 bytes

Thanks to JungHwan Min for saving 9 bytes!

Do[#!=Length[i~IntegerReverse~Range[2,i-2]~Cases~i]||Echo@i,{i,3,∞}]&


(∞ is the three-byte character U+221E.) Pure function taking a nonnegative integer as input. i~IntegerReverse~Range[2,i-2] creates a list of the reversals of the number i in all the bases from 2 to i-2; then Length[...~Cases~i] counts how many of these reversals are equal to i again. #!=...||Echo@i halts silently if that count is not equal to the input, and echoes i if it is equal to the input. That procedure is embedded in a straightforward infinite loop.

• Clever use of short-circuit evaluation! The Echo@i term is not evaluated when the first argument is True. Can I add this to Tips for golfing in Mathematica? – JungHwan Min Mar 4 '17 at 16:35
• By the way, Do[...,{i,3,∞}] is shorter than (i=2;While[1>0,... ++i ...]), and Cases would work instead of Position. -9 bytes: Do[#!=Length[i~IntegerReverse~Range[2,i-2]~Cases~i]||Echo@i,{i,3,∞}]& – JungHwan Min Mar 4 '17 at 16:41
• Yes, please do add the tip!—I'm surprised it's not already in there, since I definitely learned it from this site somewhere.... – Greg Martin Mar 4 '17 at 19:44
• Cases works great in place of Position. But I tested the Do construction, and it doesn't work for me but I have no idea why not. For some reason, it doesn't plug in the i values—I get errors like "Range specification in Range[2,-2+i] does not have appropriate bounds.". (And inserting a Print[i]; verifies that i isn't being assigned values.) Any ideas? – Greg Martin Mar 4 '17 at 19:45
• Apparently, there were U+200B and U+200C in the code, between , and { (probably something to do with SE's system). That breaks the code because the characters are considered a variable. I hope this doesn't have it: Do[#!=Length[i~IntegerReverse~Range[2,i-2]~Cases~i]||Echo@i,{i,3,∞}]& EDIT: Still has it. Here's a pastebin link – JungHwan Min Mar 4 '17 at 23:05

# Pyth, 2119 18 bytes

.V3IqQlf_IjbTr2tbb


This should work in theory. It works properly if I substitute the infinite loop for any finite one (e.g. JQFbr3 50*bqJlf_IjbTr2tb for 3 up to 50, try here), but the Pyth interpreter doesn't know when or how to print literally infinite output.

Explanation:

.V3IqQlf_IjbTr2tbb
.V3                     increase b infinitely, starting from 3
r2Tb       range of integers including 2 to b - 2
lf                length of elements retained if:
jbT             b in that base
_I                is invariant under reversal
qQ                  check if the length is equivalent to the input
I             b      output b if so


# Perl 6, 90 bytes

->\n{->{$_=++($ //=2);$_ if n==grep {($/=[.polymod($^b xx*)])eq[$/.reverse]},2..$_-2}...*}  Try it -> \n { # sequence generator ->{ # using a code block with no parameter$_ =      # set the default scalar to the value being tested
++(     # preincrement
$# anonymous state variable //= 2 # initialized to 2 ); # the anonymous state var maintains its state # only for this current sequence # every time the outer block gets called it is created anew$_    # return the current value
if  # if the following is true

n == grep # find all that match the following code

{
# convert the current value to the base being tested
# store as an Array in $/ ($/ = [ .polymod($^b xx*) ] ) eq # is string equal to: (shorter than eqv) [$/.reverse ]
},

2 .. $_ - 2 # from the list of bases to test } ... # keep using that code block to produce values * # indefinitely }  # Bash + Unix utilities, 134 132 bytes for((x=3;;x++)){ c=$1
for((b=x-1;--b>1;)){
s=bc<<<"for(y=$x;y;y/=$b)y%$b" t=tac<<<"$s"
[ "${s#$t}" ]||((c--))
}
((c))||echo \$x
}


Try it online!

Input is passed as an argument. The output is on stdout.

If you run this normally, it will display one number at a time in the infinite sequence.

If you try this in TIO, it will display as much of the output as it has generated when it times out at 60 seconds.

# Python 2, 132 bytes

B=lambda n,b:[]if(n<1)else B(n/b,b)+[n%b]
n=input()
x=3
while 1:
if sum(B(x,b)==B(x,b)[::-1]for b in range(2,x-1))==n:print x
x+=1
`

Try it online

The TIO program has a footer added so you don't have to wait 1 minute for the program to time out before seeing the output.