@hpaulj Yeah, why does it use these numbers? Very interesting. Why is the first sequence big (major) and the second - small (minor)?
– user4035Sep 3 '15 at 2:16

Don't think of 'major/minor' as 'big/small'. Don't read too much into the names; they are more historical convensions than anything else. Look at en.wikipedia.org/wiki/Mode_(music)#Modern to see how they fit in a bigger picture of modes.
– hpauljSep 3 '15 at 5:06

4 Answers
4

Yes, there is a pattern. The initial starting point is the following two facts:

Traditionally, the western musical scale was based on a 7-note scale, named A-G.

Acoustically, the most basic harmony, aside from unisons and octaves, is the perfect fifth (P5), which can be very closely approximated by 7/12ths of an octave (where each 12th of an octave is called a half-step, H).

In order to maximize the occurrence of P5's, the scale is constructed so that each of the seven notes in the scale is a P5 away from another note. Since pitches "wrap around" at the octave, we use so-called "modular arithmetic" (think of adding clock times, where 11:00 + 2 hours = 1:00), denoted below by the notation "mod12". If we start from F (which happens to be 5 in your numbering scheme), and add a P5 (7 half steps) each time, this gives us the following sequence of notes:

F = 5

C = F+P5 = 5+7 mod12 = 0

G = C+P5 = 0+7 mod12 = 7

D = G+P5 = 7+7 mod12 = 2

A = D+P5 = 2+7 mod12 = 9

E = A+P5 = 9+7 mod12 = 4

B = E+P5 = 4+7 mod12 = 11

This then, gives us the patterns of the non-sharp and non-flat notes. You'll notice, however, that from B back to F is a distance of 6 not 7, which corresponds to a dissonant interval called the tritone, instead of a P5. In order to address this, you have two choices: You can either replace the B with a note a P5 below F:

B&flat; = F-P5 = 5-7 mod12 = 10 = B-1

Or you can replace the F with a note a P5 above B:

F&sharp; = B+P5 = 11+7 mod12 = 6 = F+1

Note that these new notes replace the original, and are either a half step below or above the note that they replace. Also note that this pattern can then be continued on indefinitely, by adding or subtracting 7 (mod12) to get the next note in the sequence.

Update:
If you extrapolate and generalize the above sequence, you'll notice that any pitch can be represented by the formula:

(5 + n*7) mod 12

In this formula, the value of n tells you two important things about how this pitch is named.

If you divide n by 7, the integer part of the division (technically, the floor) tells you how many sharps (positive) or flats (negative) the note has. For example, if n is in the range 0..6, floor(n/7) = 0 and you get the plain note names listed above. If n is in the range 7..13 (floor(n/7) = 1), you get names with single sharps. In the range 14..20 (floor(n/7) = 2), you get double sharps. In the range -7..-1 (floor(n/7) = -1), you get flats.

The remainder of dividing n/7 gives you a number from 0-6, which gives you the letter name in the order (F, C, G, D, A, E, B).

As you point out in the comments, this sequence will eventually repeat, since it is modular arithmetic. Indeed, this is true, and it reflects a very important fact about our musical system: no note has a single unique name, but rather can be expressed using any number of different names (note names to pitches are not a one-to-one function). For example, all of the following pitch names map to the same pitch class:

F = 5

E&sharp; = (5 + 12*7) mod12 = 5

D&sharp;&sharp;&sharp; = (5 + 24*7) mod12 = 5

C&sharp;&sharp;&sharp;&sharp;&sharp; = (5 + 36*7) mod12 = 5

G&flat;&flat; = (5 - 12*7) mod12 = 5

A&flat;&flat;&flat;&flat; = (5 - 24*7) mod12 = 5

Thus, as you can see, all pitches can technically be described as a sharp or a flat. However, there will also exist a non-sharp and non-flat note name only in the case where the pitch number can be expressed with an n such that floor(n/7) == 0 (in other words, n is in the range 0..6).

"this pattern can then be continued on indefinitely, by adding or subtracting 7 (mod12) to get the next note in the sequence." - yeah, but it will repeat itself as it's a modular arithmetics.
– user4035Sep 2 '15 at 8:54

Can we use another numbering not to start from 5? I want to get a formula for sharp and non-sharp notes.
– user4035Sep 2 '15 at 8:54

It's mildy curious that the circle of fifths has to start at F to get the 7 natural notes, even though C is the root of the corresponding Major scale. In a sense the circle fits the Lydian (F) mode better than the Ionian (C).
– hpauljSep 3 '15 at 22:18

A somewhat simpler answer, for us mere mortals. Write the note names around in a circle, as in the numbers on a clock face, in the same order that you did earlier. C can go anywhere - I put it at 12 o'clock. Start at C (no # or b), and count clockwise 7. You get to G. 1#. Go another 7, you get to D. 2#. And so on. Now back to C, this time count anti-clockwise 7. You get F. 1b. On another 7, you get Bb. 2b. Obviously (?) the Bb isn't going to be called A#, because we're now in flat territory. How you make this into an equation is up to you, the mathematician!

Here's a picture: circle-of-fifths.net/images/circle-of-fifths.gif. There's a key at 6:00 that has two possible names, G flat or F#. But except for that one, there is a clear convention for whether to write the key signature with sharps or with flats so that you don't end up with more than 7 sharps or flats in the key signature.
– aparente001Sep 2 '15 at 13:18

Here's a 'formula' for finding the natural and sharp notes, expressed as Python/numpy calculations (MATLAB would do just as well). It's not a refined calculation, just an easy way to generate the numbers and group them (mixing arrays, sets and sorted lists).

The previous answers are good, but I think it's also musically significant that since sharps are added through the cycle of fifths means that what you're calling the "sharp keys", taken together form a pentatonic scale. Specifically C#, D#, F#, G#, and A# form the F# major pentatonic scale.