The answer here is really more about math than about Python, but hopefully the explanation below is helpful.
First, to clarify some Python basics:
print("YNEOS"[0::2]) # "YES"
print("YNEOS"[1::2]) # "NO"
This setup is a fairly common trick in code golf, where the goal is to make the code as short (and unreadable) as possible. From this you can see that the code you've posted simply chooses either 0 or 1 as the start index for the "YNEOS"
string, depending on the input. Then ::2
means get every second letter.
Second, you probably already know this, but input()
returns a string, so we have to use int(input())
to convert it to an integer.
Now on to the problem, which is to try to split the input number w
into 2 pieces, both of which are even numbers. The sum of two even numbers is always itself an even number, which means for w
to be a valid solution, it must be even. An easy way to check for even numbers in programming is to check if a number modulo 2 is equal to 0. However, in our case the number must also be greater than 2, because 2 cannot be split into two even-sized halves. So we need to check for:
w % 2 == 0 and w > 2
With a little more polishing, in the real world this would be the solution to the problem. We don't need a brute force method because the problem is not asking you which specific combinations are possible, only if any combination is possible. Now the only reason we need anything more is to make the code shorter. How can we do that?
Digging in
The hard part is incorporating the w > 2
into one statement with the even-odd check. It's tempting to try to add 2 to w
, or multiply it by 2, or something similar, but that won't work because it only shifts the even-odd pattern of input numbers. We need something measurable about the pattern (using just one operation) to change after w > 2
.
It turns out that using powers of 2 will give us our answer. If you calculate 2 to the power of w
for each number from 1 to 100, you get 2, 4, 8, 16, 32, 64, ...
. Nothing special-looking. But think back to the modulo operation - how can we use that to get a different answer in this sequence after the first 2, or 3, or 4 numbers?
Notice that every number in the sequence after (and including) an arbitrary n
is a multiple of n
: 2, 4, 8, 16
are multiples of 2. 4, 8, 16, 32
are multiples of 4. 8, 16, 32, 64
are multiples of 8, and so on. So for our purposes, we can run:
2**int(input()) % 8
for every input (w
) 1-100, and we will get 2, 4, 0, 0, 0, 0, 0, ...
. Progress!
But now since every number in our sequence is a power of 2, they are all even. How can we check if w
was even, based only on 2**w
? It turns out we can check 2**w % 3
. If that quantity is equal to 1, w
is even. Otherwise, w
is odd. (This is kind of an interesting result. Why is every power of 2 with an even exponent exactly 1 more than a multiple of 3?)
Home stretch
And now we are almost there. We could combine our results by checking ((2**w % 8) + (2**w % 3)) == 1
, but this is terribly inefficient and definitely doesn't shorten our code. We can instead multiply the two moduli (8 and 3) by each other to get 24 and use that as the new modulus. (2**w % 24)
gives the following sequence, starting with w
= 1:
2, 4, 8, 16, 8, 16, 8, 16, 8, 16, ...
We are so close! We can see that the result for even values of w
greater than 2 is always 16. So if 2**w % 24 == 16
, w
is a solution to the problem, and we should return "YES." Otherwise, we should return "NO". We could instead write the opposite: if 2**w % 24 != 16
, return "NO", otherwise "YES". And from here we can save a few characters by using 2**w % 24 < 9
, which is equivalent because the only values in the sequence not equal to 16 are less than 9. This final option is what your code does, and keep it in mind because it is somewhat confusing: if 2**w % 24 < 9
is True
, we want to return "NO."
The final piece is understanding that Python internally treats True
as equal to 1 and False
as equal to 0. So if you run "YNEOS"[True]
, for example, you will get N
, the character at index 1. And that means "YNEOS"[2**int(input()) % 24 < 9::2]
will evaluate to "YNEOS"[1::2] == "NO"
if the inequality is True
, just like we wanted. Otherwise it will evaluate to "YNEOS"[0::2] == "YES"
.
So there you have it. Now, if I've explained this well enough, you can understand what the heck print("YNEOS"[2**int(input())%24<9::2])
does.
PARTING WARNING
This is kind of a cute solution, but it is not efficient. Python happens to be very good at dealing with huge numbers like 2**100
, but in other languages a number that big would most likely blow up a regular integer variable, or even a C++ 64-bit long long
variable. Plus, even if it doesn't blow up, finding that huge number modulo 24 is computationally expensive. Anywhere outside of these "code golf" competitions where you are trying to make the code as short as possible, you would not want to use code like this.