# Non-palindromic numbers

A strictly non-palindromic number N is a number that isn't a palindrome in any base (in bases 2 to N-2). These numbers are listed on OEIS

For example, the number 19 in base 2,3,4,5,6,...17 is: 10011,201,103,34,31,...12. None of these representations is palindromic, so the number is strictly non-palindromic.

For this challenge, you need to return a truthy value if the number is non-palindromic, otherwise a falsy value.

• You may assume the number passed to you is greater than or equal to 0.
• Your program should work for values up to your languages' integer size.

# Test cases:

Truthy:

0
1
2
3
4
6
11
19
47
53
79
103
389
997
1459


Falsy:

5
7
8
9
10
13
16
43
48
61
62
101
113
211
1361


This is a , so make your answers as short as possible!

• Yes, I missed that. However, answers to this challenge could basically be reused by adding a result < n-2 check to them, I think. – FryAmTheEggman Aug 25 '16 at 20:54

# C, 82 bytes

p(n,a,b,c,r){c=0;for(b=1;++b<n-2;c+=r==n)for(a=n,r=0;a>0;a/=b)r=r*b+a%b;return!c;}


Ideone it!

## Explanation

This code reverses n in base b and stores in r:

for(a=n,r=0;a>0;a/=b)r=r*b+a%b;


The outer loop counts the number of bases from 2 to n-1 in which n is a palindrome.

If n is non-palindromic, the count would be 1 (n must be a palindrome in base n-1).

• Have an upvote because I couldnt upvote the SILOS answer twice – Rohan Jhunjhunwala Aug 26 '16 at 17:28
• @RohanJhunjhunwala Best reason to upvote ever. – Leaky Nun Aug 26 '16 at 17:31
• @LeakyNun But a bit of serial voting... – Erik the Outgolfer Oct 20 '16 at 18:19

# Python 2, 71 bytes

n=input();b=1
while b<n-2:
m=n;r=0;b+=1
while m/(r!=n):r=r*b+m%b;m/=b


Output is via exit code, where 0 is truthy and 1 is falsy. Test it on Ideone.

# S.I.L.O.S, 206 bytes

GOTO e
lbld
c - 1
GOTO c
lble
n = i
i - 3
b = i
b + 1
GOTO f
lbla
a = n
r = 0
lblb
m = a
m % b
r * b
r + m
a / b
if a b
r - n
r |
if r d
lblc
c + 1
i - 1
b - 1
lblf
if i a
c / c
c - 1
c |
printInt c


Try it online!

Port of my answer in C.

• Have two upvotes one for each answer, because I can't upvote this twice – Rohan Jhunjhunwala Aug 27 '16 at 4:21
• pheraps if you can write the code using one separation statement as "|" you can make advantage in write 1 char instead of 2 char of \13 \10 as \n as separation statement – RosLuP Aug 28 '16 at 10:07
• @RosLuP Am I using \r\n as \n now? – Leaky Nun Aug 28 '16 at 10:33
• i don't know in your sys, but i copy above program in a notepad, than save it: the lenght of that file is 241 not 206. so here it seems to me that \n is 2 chars not 1 – RosLuP Aug 28 '16 at 17:43
• @RosLuP Your notepad automatically converted EOLs to \r\n. – Leaky Nun Aug 28 '16 at 17:52

(a!c)b|a<1=c|x<-c*b+mod a b=div a b!x$b f n=all((/=n).(n!0))[2..n-2]  # Jelly, 9 bytes bRµ⁼"US<3  ### How it works bRµ⁼"US<3 Main link. Argument: n R Range; yield [1, ..., n]. b Convert n to all bases between 1 and n, yielding a 2D array A> µ Begin a new, monadic chain. Argument: A U Upend; reverse the 1D arrays in A. ⁼" Zipwith equal; yield 1 for each array that matches its inverse. S Sum; add the resulting Booleans. If n > 1, the sum will be 2 if n is strictly non-palindromic (it is only a palindrome in bases 1 and n - 1), and greater than 2 otherwise. For 0 and 1, the sum will be 0 (sum of the empty array) and 1 (only a palindrome in base 1); both are less than 2. <3 Compare the sum with 3, yielding the desired Boolean.  • +1 for the <3. – Leaky Nun Aug 26 '16 at 1:16 # Mathematica, 58 43 bytes !Or@@Table[#==#~IntegerReverse~i,{i,2,#-2}]&  TIL that #~IntegerReverse~i reverses the digits of the input when written in base i. # Pyth, 12 10 bytes Saved two bytes with Dennis' trick. >3sm_IjQdS  Try it online! Explanation:  S (Q) Get all the bases we need by building a list from 1 to Q m For all bases d in the bases list: jQd cast Q to base d as a list _I and check to see if the list is palindromic (invariant on reversal) Compile all the results back into a list s Sum the results (a shorter form of any), gives 3 or more for palindromics (2 is the usual because of bases 1 and Q-1) >3 And verify that the sum is greater than three to get non-palindromics  ## JavaScript (ES6), 83 bytes f=(n,i=n-2,g=n=>n<i?[n]:[...g(n/i|0),n%i])=>i<2||${a=g(n)}!=a.reverse()&&f(n,i-1)
<input type=number oninput=o.textContent=f(this.value);><pre id=o>

## Perl6, 11072 65

my &f={?all(map {{.reverse ne$_}(@(.polymod:$^a xx*))},2..$_-2)}  Couldn't use base since that's broken for any base above 36. ### Previous attempts my &a={$^a??flat($a%$^b,a($a div$b,$b))!!()};my &f=->$n {?all(map {.reverse ne$_ given @(a($n,$_))},2..$n-2)}
my &f=->\n {?all(map {.reverse ne$_ given @(n.polymod:$_ xx*)},2..n-2)}

• I managed to get it down to 59 bytes with my first try. Hint use .polymod with an infinite list of divisors. 1362.polymod: 226 xx * – Brad Gilbert b2gills Aug 25 '16 at 21:54
• Make that 53, and another hint {...} and -> $_ {...} are almost exactly the same. Also you don't have to store the lambda anywhere so you can remove the my &f =. – Brad Gilbert b2gills Aug 25 '16 at 22:17 # Brachylog, 14 bytes ¬{⟦₆bk∋;?ḃ₍.↔}  Try it online! Outputs through predicate success or failure, which prints true. or false. if run as a program. ¬{ } It cannot be shown that ? the input ; ḃ₍ in a base ∋ which is an element of ⟦₆ the range from 1 to the input - 1 b without its first element k or its last element . can be unified with both the output variable ↔ and its reverse.  C, 77 bytes h(n,b,k,z){for(z=0,k=n;z+=k%b,k/=b;z*=b);return b+3>n?1:z==n?0:h(n,++b,0,0);}  recursive exercise...i change (b+2>=n) with (b+3>n) whithout debugging... main() {int v[]={0,1,2,3, 4, 6,11,19,47,53,79,103,389,997,1459}, n[]={5,7,8,9,10,13,16,43,48,61,62,101,113,211,1361}, m; // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 for(m=0; m<15; ++m) printf("%u=%u\n", v[m], h(v[m],2,0,0)); for(m=0; m<15; ++m) printf("%u=%u\n", n[m], h(n[m],2,0,0)); } /* 77 0=1 1=1 2=1 3=1 4=1 6=1 11=1 19=1 47=1 53=1 79=1 103=1 389=1 997=1 1459=1 5=0 7=0 8=0 9=0 10=0 13=0 16=0 43=0 48=0 61=0 62=0 101=0 113=0 211=0 1361=0 */  • Do not vandalize your posts. – DJMcMayhem Oct 20 '16 at 17:57 # C, 129 bytes f(n,b,k,j){int a;for(b=2;b+2<n;++b){for(j=0,k=n;a[j]=k%b,k/=b;++j);for(;k<j&&a[k]==a[j];++k,--j);if(k>=j)return 0;}return 1;}  # PHP, 68 bytes for($b=$argn;--$b;)strrev($c=base_convert($argn,10,$b))!=$c?:die(1);


takes input from STDIN, exits with 1 for falsy, 0 for truthy. Run with -R.

• If I see this right you can only solve n<39 – Jörg Hülsermann Apr 3 '17 at 0:17

# APL(NARS), chars 47, bytes 94

{⍵≤4:1⋄∼∨/{⍵≡⌽⍵}¨{⍵{(⍺⍴⍨⌊1+⍺⍟⍵)⊤⍵}w}¨2..¯2+w←⍵}


where {(⍺⍴⍨⌊1+⍺⍟⍵)⊤⍵} would be the function conversion one positive omega in digits number base alpha, and {⍵≡⌽⍵} would be the function check palindrome... test:

  f←{⍵≤4:1⋄∼∨/{⍵≡⌽⍵}¨{⍵{(⍺⍴⍨⌊1+⍺⍟⍵)⊤⍵}w}¨2..¯2+w←⍵}
f¨0 1 2 3 4 6 11 19 47 53 79 103 389 997 1459
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
f¨5 7 8 9 10 13 16 43 48 61 62 101 113 211 1361
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0