Non-palindromic numbers

16

1

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!

Nathan Merrill

Posted 2016-08-25T20:47:42.363

Reputation: 13 591

2

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 – 2016-08-25T20:54:15.730

Answers

6

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).

Leaky Nun

Posted 2016-08-25T20:47:42.363

Reputation: 45 011

Have an upvote because I couldnt upvote the SILOS answer twice – Rohan Jhunjhunwala – 2016-08-26T17:28:36.353

3@RohanJhunjhunwala Best reason to upvote ever. – Leaky Nun – 2016-08-26T17:31:15.437

@LeakyNun But a bit of serial voting... – Erik the Outgolfer – 2016-10-20T18:19:41.150

5

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.

Dennis

Posted 2016-08-25T20:47:42.363

Reputation: 196 637

5

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

GOTO e
lbld
c - 1
GOTO c
lble
readIO 
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.

Leaky Nun

Posted 2016-08-25T20:47:42.363

Reputation: 45 011

Have two upvotes one for each answer, because I can't upvote this twice – Rohan Jhunjhunwala – 2016-08-27T04:21:14.127

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 – 2016-08-28T10:07:24.803

@RosLuP Am I using \r\n as \n now? – Leaky Nun – 2016-08-28T10:33:45.727

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 – 2016-08-28T17:43:54.787

@RosLuP Your notepad automatically converted EOLs to \r\n. – Leaky Nun – 2016-08-28T17:52:05.517

if not for gain one char for "\n", it is because i say it is good to use | as instruction separator (in the add of "\n") for readability reason... here in this place i can not indent that code nor use "\n" so i have no other thing to show or say. – RosLuP – 2016-08-30T17:26:57.480

@RosLuP The | is absolute. – Leaky Nun – 2016-08-30T17:48:18.127

abs is one function one use few time as the C or "|", but one separator is Always used every where so it is better choice one good instrution separator first than all other functions, operators, key words label etc. but this is just how i see the subject "instruction separators" – RosLuP – 2016-08-30T17:56:55.693

@RosLuP I am not understanding you. What is your native language? – Leaky Nun – 2016-08-30T18:03:37.633

4

Haskell, 75 68 bytes

(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]

Damien

Posted 2016-08-25T20:47:42.363

Reputation: 2 407

3

Jelly, 9 bytes

bRµ⁼"US<3

Try it online! or verify all test cases.

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.

Dennis

Posted 2016-08-25T20:47:42.363

Reputation: 196 637

+1 for the <3. – Leaky Nun – 2016-08-26T01:16:08.913

2

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

Steven H.

Posted 2016-08-25T20:47:42.363

Reputation: 2 841

2

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.

Greg Martin

Posted 2016-08-25T20:47:42.363

Reputation: 13 940

1

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.

Unrelated String

Posted 2016-08-25T20:47:42.363

Reputation: 5 300

1

Perl6, 110 72 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)}

bb94

Posted 2016-08-25T20:47:42.363

Reputation: 1 831

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 – 2016-08-25T21:54:34.373

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 – 2016-08-25T22:17:42.927

1

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>

Neil

Posted 2016-08-25T20:47:42.363

Reputation: 95 035

0

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.

Titus

Posted 2016-08-25T20:47:42.363

Reputation: 13 814

If I see this right you can only solve n<39 – Jörg Hülsermann – 2017-04-03T00:17:09.857

0

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 

RosLuP

Posted 2016-08-25T20:47:42.363

Reputation: 3 036

0

C, 129 bytes

f(n,b,k,j){int a[99];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;}

RosLuP

Posted 2016-08-25T20:47:42.363

Reputation: 3 036

0

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
*/

RosLuP

Posted 2016-08-25T20:47:42.363

Reputation: 3 036

1Do not vandalize your posts. – James – 2016-10-20T17:57:07.560