# Is this number a palindrome? [closed]

## Input

A single integer in your language's preferred format, e.g int or an array of digits are all acceptable.

## Output

A truthy or falsely value, depending on whether the given number is palindromic (same forwards and backwards)

## Rules

• Numbers will always be positive, at least two digits long.
• This is , so lowest O(n) time complexity wins.

# Test cases

• 123454321 - true
• 296296 - false
• 296692 - true
• 5499456 - false
• 0 - false/error/any value
• In your rules you state the input is less than 99999999, but the first test case is larger than this. Jul 1 '19 at 13:22
• You state that the input is a 16-bit integer, but 4 of the 5 test cases don't fit on 16 bits. Jul 1 '19 at 13:26
• Also: specifying any upper bound makes the problem trivially O(1). You should either change to fastest-code or remove the upper bound altogether. Jul 1 '19 at 13:33
• @GezaKerecsenyi No you didn't fix the issue of an upper bound making O(1) solutions trivial.
Jul 1 '19 at 13:50
• If boring solutions are the optimal solutions then that's a reflection of the challenge, not the solutions. Jul 2 '19 at 13:30

# APL (Dyalog Unicode), O(1)

⊃∘'111111111110… …10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

Anonymous tacit prefix function.

Try it online!

Simply picks the result from a pre-compiled list.

• Haha, nice. Didn't think about that. Well done! :) Jul 1 '19 at 13:37
• @LuisMendo It works for me in FFQ/W10 but it takes a very long time to render. Let's hope OP doesn't go to 64 bits…
Jul 1 '19 at 14:13
• @Adám Ah, that was the issue here as well. It took about 1 minute (Chrome, W10) Jul 1 '19 at 14:16
• The rules of the question have changed in a way that renders this answer invalid. (By no longer specifying a size of integer) Mar 22 '20 at 20:02
• @pppery Also, the challenge is closed.
Mar 22 '20 at 20:05

# Java, complexity: $$\O(\lfloor\log_{10}(n) + 1\rfloor)\$$

boolean f(int n){
int reverse = 0;
//int debugIterations = 0;
for(int palindrome = n; palindrome != 0; palindrome /= 10){
//debugIterations++;
int lastDigit = palindrome % 10;
reverse = reverse * 10 + lastDigit;
}
//System.out.println("Debug iterations: "+debugIterations);
return n == reverse;
}


Loops once for every digit in the input-number. Pretty straight-forward implementation tbh..

Try it online.

# Java, O(1)

boolean f(int n){
if (n <= 0 || n >= 16777216) throw new Error(n+" is not a valid input!");
int reverse = n/10000000%10*1 + n/1000000%10*10 + n/100000%10*100 + n/10000%10*1000 + n/1000%10*10000 + n/100%10*100000 + n/10%10*1000000 + n/1%10*10000000;
int len = (int) Math.log10(n);
reverse/= Math.pow(10, 7-len);
return reverse == n;
}


Try it online!

Kevin Cruijssen's answer, unrolled for 24-bit numbers.

• I can't post answers since this is on hold, so this is my answer: O(1) a=input();print(1 if a[::-1]==a and len(a)>1 else 0)
– user85052
Jul 2 '19 at 3:28
• The rules of the question have changed in a way that renders this answer invalid. (By no longer specifying a size of integer) Mar 22 '20 at 20:06
• @pppery It specifies "A single integer in your language's preferred format, e.g int", and I would consider my answer accepting an int to be as valid as it gets. It doesn't seem to disallow knowing the types size either. Mar 22 '20 at 20:22

# APL+WIN, O(1)

Thanks to Adam for suggested changes to my original code and his explanation - see the comments section.

Prompts for input of integer.

 17=+/(⌽17↑n)=¯17↑n←⍕⎕


Try it online!

• This isn't a [code-golf] challenge, it's [fastest-algorithm]. Jul 1 '19 at 16:26
• @Kevin Cruijssen OK so how do we decide which algorithm is fastest. If you compare our examples on TIO APL looks faster? Jul 1 '19 at 16:32
• If you compare the execution-speed it would be a [fastest-code] challenge. ;) I'm not very good at comparing complexity $O$ speeds tbh. But as the challenge is currently stated, the complexity is $O(1)$ anyway regardless of the implementation. The challenge is kinda pointless imho.. Jul 1 '19 at 16:59
• @Kevin Cruijssen OK if the op is not happy with my entry I will delete it. As APL is an interpreted language I have no idea what is going on under the hood so I do not know how it is handling the problem. I think given what you have said I agree it does seem pointless. Jul 1 '19 at 17:06
• You should be able to guarantee O(1) with 17=+/(⌽17↑n)=¯17↑n←⍕⎕ since it always has to allocate more memory (3 bytes) than initially assigned (1 or 2 bytes), always reverses a length-17 string, always compares 17 character pairs, and always sums 17 Booleans.