18
\$\begingroup\$

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!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Yes, I missed that. However, answers to this challenge could basically be reused by adding a result < n-2 check to them, I think. \$\endgroup\$ Aug 25, 2016 at 20:54

15 Answers 15

6
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ Have an upvote because I couldnt upvote the SILOS answer twice \$\endgroup\$ Aug 26, 2016 at 17:28
  • 3
    \$\begingroup\$ @RohanJhunjhunwala Best reason to upvote ever. \$\endgroup\$
    – Leaky Nun
    Aug 26, 2016 at 17:31
  • \$\begingroup\$ @LeakyNun But a bit of serial voting... \$\endgroup\$ Oct 20, 2016 at 18:19
5
\$\begingroup\$

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.

\$\endgroup\$
5
\$\begingroup\$

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.

\$\endgroup\$
9
  • \$\begingroup\$ Have two upvotes one for each answer, because I can't upvote this twice \$\endgroup\$ Aug 27, 2016 at 4:21
  • \$\begingroup\$ 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 \$\endgroup\$
    – user58988
    Aug 28, 2016 at 10:07
  • \$\begingroup\$ @RosLuP Am I using \r\n as \n now? \$\endgroup\$
    – Leaky Nun
    Aug 28, 2016 at 10:33
  • \$\begingroup\$ 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 \$\endgroup\$
    – user58988
    Aug 28, 2016 at 17:43
  • \$\begingroup\$ @RosLuP Your notepad automatically converted EOLs to \r\n. \$\endgroup\$
    – Leaky Nun
    Aug 28, 2016 at 17:52
4
\$\begingroup\$

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]
\$\endgroup\$
3
\$\begingroup\$

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.
\$\endgroup\$
0
2
\$\begingroup\$

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.

\$\endgroup\$
2
\$\begingroup\$

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
\$\endgroup\$
0
1
\$\begingroup\$

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>

\$\endgroup\$
0
1
\$\begingroup\$

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)}
\$\endgroup\$
2
  • \$\begingroup\$ 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 * \$\endgroup\$ Aug 25, 2016 at 21:54
  • \$\begingroup\$ 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 =. \$\endgroup\$ Aug 25, 2016 at 22:17
1
\$\begingroup\$

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.
\$\endgroup\$
1
\$\begingroup\$

Vyxal GRr, 58 bitsv2, 7.25 bytes

⇩Ḣƛ?τḂ⁼;a

Try it Online!

Bitstring:

1101010001101111001011011000000100110010001101101010011011
\$\endgroup\$
0
\$\begingroup\$

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
*/
\$\endgroup\$
1
  • 1
    \$\begingroup\$ Do not vandalize your posts. \$\endgroup\$
    – DJMcMayhem
    Oct 20, 2016 at 17:57
0
\$\begingroup\$

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;}
\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
1
  • \$\begingroup\$ If I see this right you can only solve n<39 \$\endgroup\$ Apr 3, 2017 at 0:17
0
\$\begingroup\$

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 
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.