If we keep the constraint that each letter has to be used exactly once when naming the notes of a major scale, but drop the constraint that the tonic has to be named using the same letter as the scale name (e.g., we can write G major starting at F##) then that pattern of sharps and flats generalizes nicely.

Number the 12 tones of 12-TET starting with C=0, C#/Db=1, ..., B=11. Then if you write a major scale starting at note N, the sum of all the accidentals counting sharps as +1 and flats as -1 will be equal to 7N mod 12.

For example G is note 7. G major then should have an accidental sum of 7 x 7 = 1 mod 12. We get that writing it G A B C D E F#. But it could also be written with a sum of 13, as F## G## A## B# C## D## E##, or with a sum of -11 as Abb Bbb Cb Dbb Ebb Fb Gb.

Note that because 7 x 7 = 1 mod 12, if we have to answer the question what scale N would have an accidental sum of K mod 12, we can solve 7N = K mod 12 by multiplying both sides by 7, giving N = 7K mod 12.

E.g., what major scale as 3 flats? 7 x -3 = -21 = 3 mod 12, which gives us the major scale starting at Eb.

Personally I find this approach a lot easier than memorizing the circle of 5ths to find key signatures given the key or to find the key given the signature.

A couple of questions naturally arise at this point. Why 7N? Why mod 12. The 12 part is easy to guess--it is because we are picking our major scale out of an underlying 12 tone scale. The major scale has 7 notes out of those underlying 12 notes, so a reasonable guess is that is where the 7 comes from.

But if you think about starting with C major (all white keys) and going up half a step, because the white keys are 0 2 4 5 7 9 11 12 (I've included the octave at 12 to make things clearer), and two of those (4 and 11) are white keys that do not have a black key immediately to the right, it might seem that how many accidentals get added or removed each time you go up in key half a step is going to vary a lot. Going from C to C#, every position goes black except those two. Those two will go black when you go C# to D, and all the ones on black will go to white.

The way the white and black keys are distributed gives you some different regions of the keyboard, each of which has a distinct pattern of adding and removing accidentals as you step through, and the overall pattern of accidentals is a result of those different patterns interacting. So maybe the 7 depends on those regions, and would be different if you had a 7 tone major scale chosen from 12 underlying tones but did not have the same pattern of white/black that we have.

I spent a while trying to show that the patterns would interact in such a way as to make 7N mod 12 work, but utterly failed.

To check that out we can try imagining alien music. Maybe some aliens who also use a 12-TET underlying scale and also have a 7 tone major scale have picked 0 2 3 4 7 9 10 as their major scale. Quite a different pattern. However, it turns out that 7N mod 12 works for that too. It also works even with alien music whose major scale is 0 1 2 3 4 5 6. You can have to use a crazy number of sharps or flats in that system!

What the pattern of white/black keys affects is which notes get accidentals when, not the total number of accidentals. By having the white and black keys spread out about as evenly as you can for a 7 white/5 black system we can write every key using the "right" starting note without needing any note to have more than one sharp or flat. Less even distributions of the black keys make it so you need multiple sharps and flats on some notes, but don't change the total number of accidentals mod 12.

Once you realize it really doesn't have anything to do with the pattern of black/white but only on the number of white keys, it is then not too hard to prove that it does indeed only depend on the number.

This can be further generalized. If aliens used a 5 note major scale, then the accidental sum of key N would be 5N mod 12. Since 5x5 = 1 mod 12, they could also go the other way and find the key from the accidental count K via 5K mod 12.

In general for a M note major scale from a T tone underlying scale, transposing that scale to note N uses NM mod T accidentals.