Skip to content

24 ways to impress your friends

Vote down?

Chris van Hasselt

I like your simple use of the modulus operator. The modulus operator is very useful in many situations. As you noted, mod 2 is an easy way to determine if something is odd or even, useful for row coloring. Let’s say you have an arbitrary list of names and you want them divided into 4 columns. The names should all have an index number (1st name, 2nd name, etc) associated with them. Use mod 4 to assign the names to the correct columns – 0, 1, 2, 3. The nice thing is, if you need 5 columns, the same logic works, just change to mod 5, and re-flow the list.
It’s an easy way to put things into a discrete number of buckets.