This isn't really a formula as it is more a matter of fractions and thinking it through.
You have 2 kinds of socks--one black and one white. That's a 50% chance that you'll get either a black or white sock, or 1 out of 2 tries.
So, if you do 3 tries, you're guaranteed to get a pair of matching socks, if you've not already gotten it by your 2nd try. This is because by the third try there is no other option but to pull a black or white sock, thus giving you a matching set.
Another way to put it is that if the number of tries outweigh the number of options, a repeat of one of the options is guaranteed.
Hope that helps.