What is the formula for knowing the number of binary combinations, where n is the total number of digits and k is the maximum number of digits 1. Only those ending in 1 will be counted.
Example: n = 2, k = 3, it has 2 combinations 01 and 11;
Example: n = 3. k = 3, it has 4 combinations 111, 101, 111 and 001;
Example: n = 5, k = 3. it has 11 combinations;
For context: It is a challenge from the teacher to the class, he passed this question on to come up with a formula and ask us to make a code that generates the result. I’m days trying to get to the formula but I always make the mistake
It is clear that if the last digit "1" is in last position, we have to select at most $k-1$ "unit" digits before that (the rest of the digits being zeros). Therefore if we intend to select $p$ digits "one" ($o \le p \le k-1$), meaning selecting $p$ "places" among $n-1$ places:
$$\begin{cases}\text{if} & p=0:& \ \binom{0}{n-1} \ \text{possibilities}\\ \text{if} & p=1:& \ \binom{1}{n-1}{1} \ \text{possibilities}\\ ... & ... & ...\\ \text{if} & p=k-2:& \ \binom{n-1}{k-2} \ \text{possibilities}\\ \text{if} & p=k-1:& \ \binom{n-1}{k-1} \ \text{possibilities} \end{cases}$$
Threfore the result is:
$$\sum_{p=0}^{p=k-1}\binom{n-1}{p}$$
BUT there is no closed form formula for this sum as you can see here