15
\$\begingroup\$

Write a program that reads from stdin two integers, each newline terminated, hereafter called "number" and "radix", and:

  1. Prints any fixed message you want if the number is a palindrome in that radix (e.g. true, t, 1)
  2. Prints any different fixed message you want if the number is not a palindrome in that radix (e.g. false, f, 0, etc.)
  3. These messages must be the same per each run, but there are no rules about what they must be (whatever's best for golfing).
  4. You may assume the input is valid, two positive integers. "number" will not exceed 2147483647, "radix" will not exceed 32767.
  5. You may not use external resources, but you may use any math function included by default in your language.

Note: a radix is just the base of the number.

Sample runs:

16
10
false
16
3
true
16
20
true
121
10
true
5
5
false
12346
12345
true
16781313
64
true
16781313
16
true
Giuseppe
29.2k3 gold badges33 silver badges106 bronze badges
asked Feb 28, 2014 at 21:28
\$\endgroup\$
5
  • \$\begingroup\$ Note: a radix is just the base of the number. \$\endgroup\$ Commented Feb 28, 2014 at 21:32
  • \$\begingroup\$ Looks good now. You may want to ban external resources though. \$\endgroup\$ Commented Feb 28, 2014 at 21:35
  • \$\begingroup\$ @user2509848 hmmm, for instance? \$\endgroup\$ Commented Feb 28, 2014 at 21:38
  • \$\begingroup\$ If a person can find a calculator on the web that converts numbers between bases, it will almost certainly be used. We have been having a rash of trolly answers lately. \$\endgroup\$ Commented Feb 28, 2014 at 21:40
  • \$\begingroup\$ Can one of the fixed messages be the empty string (assuming that the other is a non-empty string)? \$\endgroup\$ Commented Aug 8, 2018 at 9:36

24 Answers 24

8
\$\begingroup\$

GolfScript, 10 characters

~base.-1%=

That is an easy one for GolfScript if we do it the straightforward way. The output is 0/1 for false/true.

~ # Take input and evaluate it (stack: num rdx)
base # Fortunately the stack is in the correct order for
 # a base transformation (stack: b[])
. # Duplicate top of stack (stack: b[] b[])
-1% # Reverse array (stack: b[] brev[])
= # Compare the elements
answered Mar 1, 2014 at 6:14
\$\endgroup\$
5
\$\begingroup\$

J (23 char) and K (19) double feature

The two languages are very similar, both in general and in this specific golf. Here is the J:

(-:|.)#.^:_1~/".1!:1,~1
  • ,~1 - Append the number 1 to itself, making the array 1 1.
  • 1!:1 - Read in two strings from keyboard (1!:1 is to read, and 1 is the file handle/number for keyboard input).
  • ". - Convert each string to a number.
  • #.^:_1~/ - F~/ x,y means to find y F x. Our F is #.^:_1, which performs the base expansion.
  • (-:|.) - Does the argument match (-:) its reverse (|.)? 1 for yes, 0 for no.

And here is the K:

a~|a:_vs/|.:'0::'``
  • 0::'`` - Read in (0::) a string for each (') line from console (` is the file handle for this).
  • .:' - Convert (.:) each (') string to a number.
  • _vs/| - Reverse the pair of numbers, so that the radix is in front of the number, and then insert (/) the base expansion function _vs ("vector from scalar") between them.
  • a~|a: - Assign this resulting expansion to a, and then check if a matches (~) its reverse (|). Again, 1 for yes, 0 for no.
answered Mar 1, 2014 at 7:02
\$\endgroup\$
1
  • \$\begingroup\$ @ak82 I believe it's more interesting this ways \$\endgroup\$ Commented Mar 1, 2014 at 17:32
4
\$\begingroup\$

APL (20)

⎕{≡∘⌽⍨⍵⊤⍨⍺/⍨⌊1+⍺⍟⍵}⎕

Outputs 0 or 1, e.g:

 ⎕{≡∘⌽⍨⍵⊤⍨⍺/⍨⌊1+⍺⍟⍵}⎕
⎕:
 5
⎕:
 5
0
 ⎕{≡∘⌽⍨⍵⊤⍨⍺/⍨⌊1+⍺⍟⍵}⎕
⎕:
 16781313
⎕:
 64
1

Explanation:

  • ⎕{...}⎕: read two numbers, pass them to the function. is the first number and is the second number.
  • ⌊1+⍺⍟⍵: floor(1+⍺ log ⍵), number of digits necessary to represent in base .
  • ⍺/⍨: the base for each digit, so replicated by the number we just calculated.
  • ⍵⊤⍨: represent in the given base (using numbers, so it works for all values of ).
  • ≡∘⌽⍨: see if the result is equal to its reverse.
answered Feb 28, 2014 at 23:25
\$\endgroup\$
3
\$\begingroup\$

Perl, (削除) 82 (削除ここまで) (削除) 77 (削除ここまで) (削除) 73 (削除ここまで) 69 bytes

$==<>;$.=<>;push(@a,$=%$.),$=/=$.while$=;@b=reverse@a;print@a~~@b?1:0

The input numbers are expected as input lines of STDIN and the result is written as 1 or 0, the former meaning the first number is a palindrome in its representation of the given base.

Edit 1: Using $= saves some bytes, because of its internal conversion to int.

Edit 2: The smartmatch operator ~~ compares the array elements directly, thus the conversion to a string is not needed.

Edit 3: Optimization by removing of an unnecessary variable.

65 bytes: If the empty string is allowed as output for false, the last four bytes can be removed.

Ungolfed version

$= = <>;
$. = <>;
while ($=) {
 push(@a, $= % $.);
 $= /= $.; # implicit int conversion by $=
}
@b = reverse @a;
print (@a ~~ @b) ? 1 : 0

The algorithm stores the digits of the converted number in an array @a. Then the string representation of this array is compared with the array in reverse order. Spaces separates the digits.

answered Feb 28, 2014 at 22:50
\$\endgroup\$
4
  • \$\begingroup\$ Sorry, my answer is really same aproach, but using $= let you whipe int step... And question stand for anything you want so nothing could be what you want ;-) \$\endgroup\$ Commented Feb 28, 2014 at 23:10
  • \$\begingroup\$ @F.Hauri: Thanks, I will update. $= is also given as tip in this answer to the question "Tips for golfing in Perl". Returning 0 costs 6 extra bytes, but it was my impression that a fixed message is not intended to be empty. \$\endgroup\$ Commented Feb 28, 2014 at 23:27
  • \$\begingroup\$ Hem,, Return 0 cost 4 extra bytes, not 6. But I maintain: silence is anything! \$\endgroup\$ Commented Feb 28, 2014 at 23:34
  • \$\begingroup\$ @F.Hauri: Yes, 4 is correct, the extra two bytes were the parentheses of the ungolfed version. \$\endgroup\$ Commented Feb 28, 2014 at 23:44
2
\$\begingroup\$

Javascript 87

function f(n,b){for(a=[];n;n=(n-r)/b)a.push(r=n%b);return a.join()==a.reverse().join()}

n argument is the number, b argument is the radix.

answered Feb 28, 2014 at 22:33
\$\endgroup\$
2
\$\begingroup\$

Sage, 45

Runs in the interactive prompt

A=Integer(input()).digits(input())
A==A[::-1]

Prints True when it is a palindrome, prints False otherwise

answered Feb 28, 2014 at 22:12
\$\endgroup\$
0
2
\$\begingroup\$

Perl (削除) 54 56 (削除ここまで) 62

$==<>;$-=<>;while($=){$_.=$/.chr$=%$-+50;$=/=$-}say$_==reverse

To be tested:

for a in $'16\n3' $'16\n10' $'12346\n12345' $'12346\n12346' $'21\n11' $'170\n16';do
 perl -E <<<"$a" ' 
 $==<>;$-=<>;while($=){$_.=$/.chr$=%$-+50;$=/=$-}say$_==reverse
 '
 done

will give:

1
1
1

So this output 1 for true when a palindrome is found and nothing if else.

Ungolfing:

$==<>; # Stdin to `$=` (value)
$-=<>; # Stdin to `$-` (radix)
while ( $= ) {
 $_.= $/. chr ( $= % $- +50 ); # Add *separator*+ chr from next modulo to radix to `$_`
 $=/= $- # Divide value by radix
}
say $_ == reverse # Return test result

Nota:

  • $_ is the current line buffer and is empty at begin.
  • $= is a reserved variable, originaly used for line printing, this is a line counter. So this variable is an integer, any calcul on this would result in a truncated integer like if int() was used.
  • $- was used for fun, just to not use traditional letters... (some more obfuscation)...
answered Feb 28, 2014 at 23:06
\$\endgroup\$
5
  • \$\begingroup\$ to clarify, this says nothing when it's not a palindrome, and 1 when it is? \$\endgroup\$ Commented Feb 28, 2014 at 23:08
  • 1
    \$\begingroup\$ Nice tricks. However wrong positive: 21 with base 11. The digits need a separator in the string comparison. \$\endgroup\$ Commented Feb 28, 2014 at 23:15
  • \$\begingroup\$ Aaaarg +3! @HeikoOberdiek You're right f... \$\endgroup\$ Commented Feb 28, 2014 at 23:27
  • \$\begingroup\$ @F.Hauri: Also the digits get reversed. Thus 170 with base 16 is 0xAA, a palindrome, but the result is false. \$\endgroup\$ Commented Feb 28, 2014 at 23:53
  • \$\begingroup\$ Aaarg +6! converting to chars... \$\endgroup\$ Commented Mar 1, 2014 at 8:52
1
\$\begingroup\$

Mathematica (削除) 77 (削除ここまで) 43

IntegerDigits[n,b] represents n as a list of digits in base b. Each base-b digit is expressed decimally.

For example, 16781313 is not a palindrome in base 17:

IntegerDigits[16781313, 17]

{11, 13, 15, 11, 14, 1}

However, it is a palindrome in base 16:

IntegerDigits[16781313, 16]

{1, 0, 0, 1, 0, 0, 1}


If the ordered pairs in the above examples were entered,

(x=Input[]~IntegerDigits~Input[])==Reverse@x

would return

False (* (because {11, 13, 15, 11, 14, 1} != {1, 14, 11, 15, 13, 11} ) *)

True (* (because {1, 0, 0, 1, 0, 0, 1} is equal to {1, 0, 0, 1, 0, 0, 1} ) *)

answered Feb 28, 2014 at 22:29
\$\endgroup\$
6
  • \$\begingroup\$ Weird, not necessary for the answer but I'm curious, how does it render them? \$\endgroup\$ Commented Feb 28, 2014 at 23:20
  • \$\begingroup\$ I hate it when I lose because of a stupid typecast... \$\endgroup\$ Commented Feb 28, 2014 at 23:20
  • \$\begingroup\$ Kindly explain "typecast". \$\endgroup\$ Commented Feb 28, 2014 at 23:50
  • \$\begingroup\$ My sage solution is longer than yours by 2 characters because I have to cast the input to type Integer \$\endgroup\$ Commented Mar 1, 2014 at 0:44
  • \$\begingroup\$ Now I understand what you mean. Thanks. \$\endgroup\$ Commented Mar 1, 2014 at 10:26
1
\$\begingroup\$

Haskell (80 chars)

tb 0 _=[]
tb x b=(tb(div x b)b)++[mod x b]
pali n r=(\x->(x==reverse x))(tb n r)

Call it with pali $number $radix. True, when number is a palindrome, False if not.

answered Mar 2, 2014 at 13:03
\$\endgroup\$
1
\$\begingroup\$

C, (削除) 140 (削除ここまで) 132

int x,r,d[32],i=0,j=0,m=1;main(){scanf("%d %d",&x,&r);for(;x;i++)d[i]=x%r,x/=r;i--;for(j=i;j;j--)if(d[j]-d[i-j])m=0;printf("%d",m);}
  • radix 1 is not supported:)
answered Mar 2, 2014 at 11:06
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Just puts(m) would work right? \$\endgroup\$ Commented Mar 2, 2014 at 11:50
  • \$\begingroup\$ printf("%d",m); will be by 8 characters shorter. \$\endgroup\$ Commented Mar 2, 2014 at 13:03
1
\$\begingroup\$

Haskell - 59

Few changes to Max Ried's answer.

0%_=[]
x%b=x`mod`b:((x`div`b)%b)
p=((reverse>>=(==)).).(%)
answered Mar 2, 2014 at 13:44
\$\endgroup\$
1
\$\begingroup\$

Ruby - 76 chars

f=->(n,b){n==0?[]:[n%b,*f.(n/b,b)]};d=f.(gets.to_i,gets.to_i);p d==d.reverse
answered Mar 3, 2014 at 18:08
\$\endgroup\$
1
\$\begingroup\$

Pyth, 4 bytes

_IjF

Try it here or check out a test suite (Takes ~10-15 seconds).

answered Aug 7, 2018 at 14:36
\$\endgroup\$
1
\$\begingroup\$

dc, 39 bytes

The length is a palindrome, of course (3312).

[[t]pq]sgod[O~laO*+sad0<r]dsrx+la=g[f]p

The number and radix should be on the top of the stack (in the current number base); number must be at least 0, and radix must be at least 2. Output is t if it's a palindrome and f if not. As it's not specified in the challenge, I've assumed that numbers never have leading zeros (so any number ending in 0 cannot be a palindrome).

Explanation

As a full program:

#!/usr/bin/dc
# read input
??
# success message
[[t]pq]sg
# set output radix
o
# keep a copy unmodified
d
# reverse the digits into register a
[O~ laO*+sa d0<r]dsrx
# eat the zero left on stack, and compare stored copy to a
+ la=g
# failure message
[f]p
answered Aug 8, 2018 at 9:32
\$\endgroup\$
1
\$\begingroup\$

LaTeX, 165 bytes

Example at desmos.com

k, the radix, is an adjustable input

b=\floor \left(\log _k\left(\floor \left(x\right)\right)\right)
f\left(x\right)=k^bx-x-\left(k^2-1\right)\sum _{n=1}^bk^{\left(b-n\right)}\floor \left(xk^{-n}\right)

If f(x)=0, x is a palindrome in base k.

Toby Speight
6,9161 gold badge30 silver badges43 bronze badges
answered Mar 19, 2015 at 1:59
\$\endgroup\$
1
\$\begingroup\$

Perl 6, 34 bytes

-4 bytes thanks to PhilH

{@(polymod $^a: $^b xx*)~~[R,] $_}

Try it online!

answered Aug 8, 2018 at 9:51
\$\endgroup\$
5
  • \$\begingroup\$ You can use $_ instead of @r to save 2 \$\endgroup\$ Commented Aug 8, 2018 at 9:57
  • \$\begingroup\$ @PhilH Nope. (assigns a Seq, not a List) \$\endgroup\$ Commented Aug 8, 2018 at 9:59
  • \$\begingroup\$ Ahh, sorry didn't see the error \$\endgroup\$ Commented Aug 8, 2018 at 10:01
  • \$\begingroup\$ @PhilH Your second tip still helped save bytes though! \$\endgroup\$ Commented Aug 8, 2018 at 10:05
  • 1
    \$\begingroup\$ Always annoying that there's no shorter way to call reduce meta on $_ or @_. \$\endgroup\$ Commented Aug 8, 2018 at 10:18
1
\$\begingroup\$

Perl 6, 27 bytes (22 without stdin/out)

say (+get).base(get)~~.flip

Try it online!

 get() # pulls a line of stdin
 + # numerificate
( ).base(get()) # pull the radix and call base to 
 # convert to string with that base
 ~~ # alias LHS to $_ and smartmatch to
 .flip # reverse of the string in $_

Perl6, king of readable golfs (golves?) (and also some not so readable).

Perl 6 function (not stdin/stdout), 22 bytes

{$^a.base($^b)~~.flip}

Try it online!

answered Aug 8, 2018 at 9:51
\$\endgroup\$
2
  • \$\begingroup\$ The reason I didn't use base in my answer is that base only supports up to base 36, and the question asks to support radixes up to 32767 \$\endgroup\$ Commented Aug 9, 2018 at 5:09
  • \$\begingroup\$ Ooh, did not know that. Hmm. \$\endgroup\$ Commented Aug 9, 2018 at 8:36
1
\$\begingroup\$

05AB1E, (削除) 4 (削除ここまで) 3 bytes

вÂQ

Try it online or verify all test cases.

Explanation:

в # The first (implicit) input converted to base second (implicit) input
 # i.e. 12345 and 12346 → [1,1]
 # i.e. 10 and 16 → [1,6]
 Â # Bifurcate the value (short for duplicate & reverse)
 # i.e. [1,1] → [1,1] and [1,1]
 # i.e. [1,6] → [1,6] and [6,1]
 Q # Check if they are still the same, and thus a palindrome
 # i.e. [1,1] and [1,1] → 1
 # i.e. [1,6] and [6,1] → 0
answered Aug 7, 2018 at 15:16
\$\endgroup\$
1
\$\begingroup\$

C (gcc), 79 bytes

n,r,m;main(o){for(scanf("%d %d",&n,&r),o=n;n;n/=r)m=m*r+n%r;printf("%d",m==o);}

Try it online!

Rundown

n,r,m;main(o){
for(scanf("%d %d",&n,&r), Read the number and the radix.
o=n; ...and save the number in o
n; Loop while n is non-zero
n/=r) Divide by radix to remove right-most digit.
m=m*r+n%r; Multiply m by radix to make room for a digit
 and add the digit.
printf("%d",m==o);} Print whether we have a palindrome or not.

Based on the fact that for a palindrome, the reverse of the number must equal the number itself.

Suppose you have the three-digit number ABC in some base. Multiplying it by base will always result in ABC0, and dividing it by base in AB with C as remainder. So to reverse the number we pick off the right-most digit from the original number and insert it to the right on the reversed number. To make room for that digit, we multiply the reverse by base before-hand.

Basically:

n rev
ABC 0
AB C
AB C0
A CB
A CB0
0 CBA
answered Aug 9, 2018 at 4:56
\$\endgroup\$
2
  • \$\begingroup\$ This is cool, can u explain the math behind this? \$\endgroup\$ Commented Aug 9, 2018 at 7:44
  • \$\begingroup\$ @durron597 Sure! Added an explanation to the post. \$\endgroup\$ Commented Aug 9, 2018 at 9:07
1
\$\begingroup\$

Jelly, 3 bytes

bŒḂ

Try it online!

How it works

bŒḂ - Main link. Takes a number n on the left and a radix r on the right
b - Convert n to base r
 ŒḂ - Is palindrome?
answered Dec 27, 2020 at 22:01
\$\endgroup\$
1
\$\begingroup\$

Python 3.8 (pre-release), (削除) 167 (削除ここまで) (削除) 162 (削除ここまで) (削除) 65 (削除ここまで) 63 bytes

a,b=map(int,open(s:=0))
o=a
while a:s=s*b+a%b;a//=b
print(s==o)

Try it online!

Massive -99 thx to @Steffan

answered Jan 1, 2023 at 21:48
\$\endgroup\$
0
1
\$\begingroup\$

APL(NARS), 21 chars

{k≡⌽k←⍵⊤⍨⍺⍴⍨1+⌊⍺⍟1⌈⍵}

In the left [⍺] the base positive integer >1 in the right [⍵] one non negative integer. Return 0 for false 1 for true.

test:

 f←{k≡⌽k←⍵⊤⍨⍺⍴⍨1+⌊⍺⍟1⌈⍵}
 10 f 16
0
 3 f 16
1
 20 f 16
1
 10 f 121
1
 5 f 5
0
 12345 f 12346
1
 64 f 16781313
1
 16 f 16781313
1
answered Sep 23 at 9:41
\$\endgroup\$
0
\$\begingroup\$

dg - 97 bytes

Trying out dg:

n,r=map int$input!.split!
a=list!
while n=>
 a.append$chr(n%r)
 n//=r
print$a==(list$reversed a)

Explained:

n, r=map int $ input!.split! # convert to int the string from input
a = list! # ! calls a function without args
while n =>
 a.append $ chr (n % r) # append the modulus
 n //= r # integer division
print $ a == (list $ reversed a) # check for palindrome list
answered Mar 1, 2014 at 9:57
\$\endgroup\$
0
\$\begingroup\$

Dyalog APL, 12 bytes

Radix on the left, number on the right

(⊢≡⌽)⍤(⊥⍣ ̄1)­⁡​‎‎⁡⁠⁡‏⁠⁠⁠⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁣‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏‏​⁡⁠⁡‌⁢⁡​‎‎⁡⁠⁢⁢‏‏​⁡⁠⁡‌⁢⁢​‎⁠‎⁡⁠⁢⁣‏⁠‎⁡⁠⁢⁤‏⁠‎⁡⁠⁣⁡‏⁠‎⁡⁠⁣⁢‏⁠‎⁡⁠⁣⁣‏⁠‎⁡⁠⁣⁤‏‏​⁡⁠⁡‌­
( ) # ‎⁡Train of
 ⊢ # ‎⁢ does the argument
 ≡ # ‎⁣ match
 ⌽ # ‎⁤ its reverse
 ⍤ # ‎⁢⁡Run atop
 (⊥⍣ ̄1) # ‎⁢⁢Inverse decode (which automatically makes enough digits to wholly decode the number)
💎

Created with the help of Luminespire.

answered Sep 22 at 20:25
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.