What's the next number in this sequence? 5, 10, 20, 30, 36 … ? And the next in this? 640, 231, 100, 91 … ?

If you know your Mozart then you'll identify 43 as the number that comes after 36 in the first sequence. These are the opening lines of The Marriage of Figaro sung by Figaro as he measures out the room that he will share with Susanna once they are married. It's a curious selection of numbers that when added together comes to 144, or 12 squared: perhaps a coincidence or maybe a numerical representation of the impending union of Figaro and his bride Susanna.

The second sequence continues with 1,003, the number of Don Giovanni's female conquests in Spain. The other numbers are part of the famous Catalogue aria sung by Leporello, Don Giovanni's servant, which include his other conquests: 640 in Italy, 231 in Germany, 100 in France, 91 in Turkey.

Mozart loved numbers. Johann Andreas Schachtner, court trumpeter and friend of the Mozart family, wrote about the young Wolfgang: "When he was doing sums, the table, the chair, the walls and even the floor would be covered with chalked numbers."

As an adult Mozart's obsession with numbers didn't wane. He would scatter numbers throughout his letters to family and friends. His family used a secret code to keep politically sensitive comments from the eyes of the censors. But he also used numbers in more intimate exchanges. His kisses would invariably be issued in units of 1,000, although sometimes he would choose a more interesting selection of numbers to shower his correspondent with.

The curious string of numbers 1095060437082 appears in a letter to his wife Constanze. One decoding that has been offered of this sequence suggests we add 10+9+50+60+43+70+82 to get 324, which is 18 squared, again like the opening of Figaro, expressing the bond of love between Mozart and Constanze. He signed himself in another letter as "Friend of the House of Numbers"; while Constanze told a biographer after Mozart's death about "his love of arithmetic and algebra".

Mozart isn't the only composer to be fascinated by music, numbers and codes. Bach's name spells out a series of musical notes that Bach employs often in pieces as if he is musically signing his work. Other composers during the baroque used a cabalistic code that changes letters into numbers which could then be used in musical composition to hide words. For example, by replacing each letter with its number in the alphabet, Bach's name translates into 2+1+3+8=14. Some commentators have tried to identify an obsession with the number 14 running throughout Bach's work and life. Apparently when he was asked to join Mizler's society of Musical Sciences he delayed until 1747 just to ensure that he could be the 14th member to join.

Schumann too was an inveterate user of musical cryptograms. More recently, Alban Berg regarded 23 as his signature number, rather like a footballer being identified by his shirt number. For example, the Lyric Suite is made up of a sequence of 23-bar phrases. Embedded in the piece is a musical representation of a love affair that Berg was having at the time. His lover was denoted by a 10-bar sequence which he entwined with his own signature, 23, using the combination of mathematics and music to serenade his affair.

But it's probably Mozart's final opera, The Magic Flute, that is the work most laden with symbolism and numerical imagery. The opera is full of masonic symbols, which in turn are underpinned by mathematics – Mozart had been admitted to Beneficence lodge in Vienna seven years earlier. The number three, for example, is very significant in masonic practice. The three knocks at the lodge door that are part of the initiation ceremony for a new mason are heard again and again throughout the opera. As Goethe, a fellow mason, declared: "The crowd should find pleasure in seeing the spectacle: at the same time, its high significance will not escape the initiates."

Beyond the three-note rhythm sequence the number three is threaded through the opera in numerous ways. Much of Mozart's masonic music is written in E flat major, a key with three flats, although this may have more to do with the key being best suited for the wind instruments that Mozart employed. Many of the characters come in threes: the three ladies who serve the Queen of the Night, the three boys. Three-part harmony abounds.

The opera is also full of pairs. Day and night, fire and water, Osiris and Isis, gold and silver, sun and moon. The number five plays a part, another important number for the masons given their choice of the symbol of the pentagram or five-pointed star. Trios give way to quintets, not quartets. And ultimately Sarastro's power is bound up in the mystical seal of the seven circles of the sun.

For Mozart The Magic Flute is also a statement of his belief in a changing order, not just politically but also musically. The work premiered in Vienna in 1791, two years after the revolution that swept the streets of Paris. The masonic order had suffered repression because the authorities feared the enlightened ideas this secret society was promoting. This transition from ancient regime to enlightenment is captured in the music. The ornate music of the Queen of the Night gives way to a new sound that Mozart hoped would be his legacy.

• This article was amended on 5 April 2013. The original gave 1,001 as the number of Don Giovanni's female conquests in Spain. That should have been 1,003 and has been corrected.