Is this number a palindrome?

-6

Input

A single integer in your language's preferred format. This format is non-negotiable (no input as strings, arrays of digits, etc.), but standard I/O formats do apply if otherwise it would be impossible in your language.

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

Geza Kerecsenyi

Posted 2019-07-01T12:57:14.143

Reputation: 1 892

Question was closed 2019-07-01T18:28:53.293

2In your rules you state the input is less than 99999999, but the first test case is larger than this. – Kevin Cruijssen – 2019-07-01T13:22:58.027

4You state that the input is a 16-bit integer, but 4 of the 5 test cases don't fit on 16 bits. – Grimmy – 2019-07-01T13:26:19.137

1Indeed, the upper bound, 99999999, doesn't fit in 16 bits. – Adám – 2019-07-01T13:27:04.800

6Also: specifying any upper bound makes the problem trivially O(1). You should either change to fastest-code or remove the upper bound altogether. – Grimmy – 2019-07-01T13:33:08.090

OK, fixed all issues. – Geza Kerecsenyi – 2019-07-01T13:46:15.957

6@GezaKerecsenyi No you didn't fix the issue of an upper bound making O(1) solutions trivial. – Adám – 2019-07-01T13:50:04.860

in your language's preferred format — What if my language's integer type is bounded? (E.g. 8, 16, or 32 bits.) – Adám – 2019-07-02T06:50:38.200

@Adám I assume you're referring to C & co., with long and long long being separate? Do it for the maximum your language supports. If that means hardcoding everything up to 32 bits, sure, but that will likely not be the winning entry. The same with the upper bound: it means it's possible to hardcode everything, but that doesn't mean you should. Plus it makes the challenge pretty boring. – Geza Kerecsenyi – 2019-07-02T07:05:07.540

1If boring solutions are the optimal solutions then that's a reflection of the challenge, not the solutions. – Shaggy – 2019-07-02T13:30:20.910

Answers

6

APL (Dyalog Unicode), O(1)

⊃∘'111111111110… …10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000'

Anonymous tacit prefix function.

Try it online!

Simply picks the result from a pre-compiled list.

Adám

Posted 2019-07-01T12:57:14.143

Reputation: 37 779

2Haha, nice. Didn't think about that. Well done! :) – Kevin Cruijssen – 2019-07-01T13:37:19.817

1@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… – Adám – 2019-07-01T14:13:09.280

@Adám Ah, that was the issue here as well. It took about 1 minute (Chrome, W10) – Luis Mendo – 2019-07-01T14:16:53.187

3

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.

Kevin Cruijssen

Posted 2019-07-01T12:57:14.143

Reputation: 67 575

1

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.

dzaima

Posted 2019-07-01T12:57:14.143

Reputation: 19 048

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) – None – 2019-07-02T03:28:23.353

0

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!

Graham

Posted 2019-07-01T12:57:14.143

Reputation: 3 184

This isn't a [code-golf] challenge, it's [fastest-algorithm]. – Kevin Cruijssen – 2019-07-01T16:26:13.113

@Kevin Cruijssen OK so how do we decide which algorithm is fastest. If you compare our examples on TIO APL looks faster? – Graham – 2019-07-01T16:32:35.050

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.. – Kevin Cruijssen – 2019-07-01T16:59:16.793

@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. – Graham – 2019-07-01T17:06:18.937

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. – Adám – 2019-07-02T08:19:23.547