Compounding more frequently seems to decrease total amount when using APYs? Interest rates are often given in terms of annual growth, even when compounding happens more often than once a year. To account for this, I read that we can use the following transformation to get the periodic compounding rate, $r$.
$$ r = (1 + \text{apy})^{1/n} $$
Where apy is the percentage annual growth, and n is the number of compounding periods.
Though approximations exist, my understanding is that this is justified by the fact that, for a balance $B$, one year of growth produces
\begin{align}
B \cdot \underbrace{r \cdot r \cdot \ldots \cdot r}_{n \text{ times}}
& = B (1 + \text{apy})^{1/n} (1 + \text{apy})^{1/n} \ldots (1 + \text{apy})^{1/n} \\ 
& = B(1 + \text{apy})
= B + B(\text{apy})
\end{align}
Which is the annual rate applied once, as we should expect.
But problems arise when we consider the fact that people periodically contribute to their accounts. When I do the math, assuming contributions are made at the start of each compounding period, compounding more frequently decreases the overall growth. This, surely, cannot be right.
My thinking can be expressed two ways. First, mathematically, then as an equivalent computer program.


*

*Let $a$ be the contribution amount.

*Let $t$ be the number of contributions per year.

*Let $P$ be the initial balance.

*Let $n$ be the number of compounding periods. Assume $n$ divides $t$ for simplicity.

*Let $y$ be the number of years.


$$ P ( 1 + r)^y + a {t \over n} {(1+r)^{1/n} \over {(1+r)^{1/n}-1}} ((1+r)^y - 1)$$
Alternatively, as a C program, we see the same results.
int main() {

  double apy = 7.2;       // annual growth rate
  double start = 5000;    // starting amount
  double add   = 2000;    // amount to add each contribution
  int compounds = 4;      // number of times to compound
  int additions = 12;     // number of times to contribute
  int years = 20;         // number of years to grow

  double balance = start;
  double rate = pow(1 + apy/100.0, 1.0/compounds) - 1;

  for (int i = 0; i < years; i++) {

    for (int c = 0; c < compounds; c++) {

      for (int m = 0; m < additions/compounds; m++)
        balance += add;

      balance *= 1 + rate;
    }
  }

  printf("Balance: %.2lf\n", balance);
}

In either case, we can tabulate the following when $P = 5000$, $a = 2000$, $\text{apy} = 7.2$, $t = 12$ and $y = 20$,

The balance is $1,098,139.14$ when $n = 1$.
The balance is $1,070,593.39$ when $n = 4$.
The balance is $1,064,536.20$ when $n = 12$.


The reason is that with less compounding the money is in the account longer.  Let us take a one year term and compare annual vs. semiannual compounding at $10\%$.  If you deposit $1$ at the start of the year, annual compounding gets you $1.1$ while semiannual gets you $1.1025$ as you would expect.  But if you contribute $1$ split over the periods, for semiannual interest the second $0.5$ is only in the account for half the year, so it only draws half a year's interest.  Again, $1$ in the annual account gives $1.1$, but in the semiannual account it only gives $1.07625$
