Monday, March 23, 2015

Algorithm to find the Chinese horoscope sign of any year

After learning about modulo function, I finally have an elegant algorithm to find out the Chinese horoscope sign based on the year of input. It came from an innocent enough question from facebook - what's the value of m+n, such that 2012^m and 2012^n has the same last 3 digits, if m and n are integers are m is greater than m?

I did a bit of exploring and found out the answer by doing it through excel. Then I discovered the modulo function, and then I also discovered the theorem that if a = b (mod m), then a^c = b^c (mod m). It's vastly different from learning about mathematical theorem through books or teachers, and learning about it through discovery. The joy of solving the puzzle is immensely satisfying.

But first of all, what's the modulo function?

Modulo function has two inputs i.e. MOD (a, b). Basically the function just returns you the number that is the remainder if a is divided by b. For example, MOD (12, 3) = 0 because 12 divided by 3 gives no remainder. MOD (12, 5) = 2 because 12 divided by 5 gives a remainder of 2.

My algorithm to find out the Chinese astrological sign is to take the input year, subtract 4 to it, modulo it to 12, then add 1 to the result. You can use it on excel.

i.e. Let x be the input year

y = MOD(x-4, 12) + 1

The output y will give you a number ranging from 1 to 12, so

1 is rat,
2 is ox,
3 is tiger,
4 is rabbit,
5 is dragon,
6 is snake,
7 is horse,
8 is goat,
9 is monkey,
10 is rooster,
11 is dog and
12 is pig.

To illustrate this, let's try out the year 3194:

So in the year 3194, y = MOD(3194-4, 12) +1 = 11, so it’s the year of the dog.


Richard Ng said...

LP : hmmmmm... I think you take this mathematic/scientific stuff a bit too far for the commoner liao... hahahaha... but it shows that you a natural born practical mathematician...

la papillion said...

Hi Richard,

Haha, I'm an engineer at heart :) I see problems and I try to solve them. This problem of trying to backcount chinese horoscope sign had been a personal problem for many years. I had a method of doing it but it's at best clumsy and not very easily done because it requires a lot of mental memory.

This method solves my problem with relative ease. You see, we can do something we've never done before and that's an achievement by itself. AND we can also do something more efficiently than the last attempt. That's what I'm doing - improving my solution.

You can see this as a mathematical/programming problem. But this post resonates with my philosophy in life - always improve, always upgrade, always learn new things. If at the end of the year when I look back at what I've done, I should resolutely say that this,

"This year is better than the last"

Rolf Suey said...

Hi LP,

I test you.

What happen to a plant in a math class?


la papillion said...

Hi Rolf,

Oh, that will depend on the type of plants - plastic or the living ones.

For the living plants, it'll grow square roots. For the plastic artificial ones, the roots will be kind of complex because they can't really grow roots, so the roots will be kind of imaginary.

However, if you place a pot of artificial roots, so they it will grow into artificial plants and thus acting like a conjugate of the artificial plants with imaginary roots. When both of these plants are placed side by side, the two plants become a real!

Now how do you rationalise this interesting situation?

la papillion said...

Let me just tidy that a bit regarding the artificial plants to clarify it.

If we cross fertilize an artificial plant with imaginary roots with another imaginary plant with artificial roots (because these plants are conjugate of each other), the product will be a fruit with seeds that will give rise to a real plant. It’s not very rational, isn’t it? ;)