Or is it due to the algorithm for mining rewards where every 210k blocks the reward is halved? Something like:
50*210,000 + 25*210,000 + 12.5*210,000 + . . . ~= 21,000,000 BTC?
This exactly. The limit is actually slightly less than
BTC21,000,000 (
BTC20,999,999.9769 to be exact) as the block reward is rounded down to eight decimal places (eg, on the tenth halving the reward will decrease from
BTC0.09765625 to
BTC0.04882812, not
BTC0.048828125 as you might expect). The code for this is in the function GetBlockValue() in main.cpp:
int64 static GetBlockValue(int nHeight, int64 nFees)
{
int64 nSubsidy = 50 * COIN;
// Subsidy is cut in half every 210000 blocks, which will occur approximately every 4 years
nSubsidy >>= (nHeight / 210000);
return nSubsidy + nFees;
}
For more details, see
en.bitcoin.it/wiki/Controlled_supply.