The idea of (num - 1) / 26 is:

suppose you have excel title 52 = AZ = A * 26 + Z * 1, where A = 1, Z = 26.

Now you shift each digit down i.e. A' = 0, and Z' = 25. (We would like to use 26 while dividing, and A' start as the "0st" character )

Then 52 = AZ = (A' + 1) * 26 + (Z' + 1) * 1.

So now you need to find A' and Z'. Z' = (52 - 1) % 26 = 25,

which is (num - 1) % 26 in the code above.

Now you need to get A' + 1 from 26 * (A' + 1) + (Z' + 1)

If you simply do num /= 26, Z' + 1 will give additional 1. So you will get n = 2 instead of n = 1.

To avoid this you do num = (num - 1) / 26