Number of logic gates for counter with each type of flip flop
D, JK or T flip flops can all be configured such that they divide by 2.  SR flip-flops, by themselves, can not be wired to toggle that way.  So they are not applicable.

There are really only two types of counters you can build; a ripple counter, which divides by 2\$^N\$ using N flip-flops, and a ring counter, which divides by N using N flip-flops.  The ripple counter's output can be short-circuited to divided by a number less than 2\$^N\$, for example a decade counter using four flip-flops.  So we just need ripple counters, the ring counter will always use more flip-flops and can be discarded.

So to minimize the number of flip-flops, you need to break the counter value into prime factors, e.g. 21 breaks up into 3 x 7, and 30 breaks up into 2 x 3 x 5.

In the first case (21), since it doesn't divide by two you can't use a single flip-flop to divide the input by half.  But you can cascade two ripple counters, one counting to 3 and the other counting to 7 (the output of the first fed into the input of the second).  The total number of flip-flops needed would be 2 + 3 = 5.

In the second case (30), you can divide the input by two using a single flip-flop, and once again cascade two ripple counte3rs, one counting to 3 and the other counting to 5.  The total number of flip-flops needed would be 1 + 2 + 3 = 6. (Or, you could use one ripple counter counting to 5, and another counting to 6, using the same number of flip-flops.)