Skip to main content
1 of 5
Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55

First tip to start it off.

When doing comparisons in your code, most of the times, it is better to try not to use brackets { } in your code, because they always require a \left and a \right to go with them, which increases byte count unnecessarily. Instead, we can utilize the sign function.

Consider a naive implementation that returns 0 if a=b, and returns 1 otherwise:
(20 bytes)

\left{a=b:0,1}\right

Instead of doing this, we can save 9 bytes by doing a little math instead:
(11 bytes)

sign(a-b)^2

This works because sign(x) returns -1 if x is negative, 0 if x=0, and 1 otherwise. a-b is 0 only when a=b, so sign(a-b) would be 0 only when a=b. If a does not equal b, it returns either -1 or 1. The ^2 is just to convert the -1 to a 1.

Even if we wanted to return 1 if a=b and 0 otherwise, we can still save 7 bytes by doing 1-sign(a-b)^2 instead of \left{a=b:1,0\right}.

Aiden Chow
  • 14.2k
  • 1
  • 17
  • 55