If your are not sure what a bit string is here are some examples for this question: 000100000000000
111000000000000
011000000000000
101000000000000
001000000000000
110000000000000
010000000000000
100000000000000
000000000000000
Basically, it's $15$ characters long and each string is a permutation of the set $\{1, 0\}$.
How many bit strings of length $15$ have:
question 1: exactly five 0s?
I came up with this:
$\frac{15 \cdot 14 \cdot 13 \cdot 12 \cdot 11}{3} = 120,120$
My reasoning behind this is if you were to create a $15$ length bit string from scratch, at first you have $15$ places to place a 0. Then since you have one 0 in the string, for your next 0 you have $14$ places to place another 0 and so on. Then you divide by the number of times you can fit $5$ 0's in the string.
I wrote a program to calculate all possible permutations and count the occurrences of how many bit strings have five o's and it came up with:
3003
so I am not sure if my math \ logic is wrong or my program is wrong.
Edit: Based on the comments by Lord Shark the Unknown, I came up with $$\frac{15 \cdot 14 \cdot 13 \cdot 12 \cdot 11}{5!} = 3003$$
Question 2: at least ten 1's?
Program came up with: 4944
Question 3 more 1's than 0's?
program came up with: 16384
import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;
public class MyClass {
static ArrayList<String> arrr = new ArrayList<String>();
static void convert_To_Len_th_base(int n, int arr[],
int len, int L)
{
String hold = "";
// Sequence is of length L
for (int i = 0; i < L; i++)
{
// Print the ith element
// of sequence
hold += arr[n % len] +"";
n /= len;
}
//System.out.println(hold);
arrr.add(hold);
}
static void print(int arr[], int len, int L)
{
// There can be (len)^l
// permutations
for (int i = 0;
i < (int)Math.pow(len, L); i++)
{
// Convert i to len th base
convert_To_Len_th_base(i, arr, len, L);
}
}
// Driver Code
public static void main(String[] args)
{
int arr[] = {1, 0};
int len = arr.length;
int L = 15;
// function call
print(arr, len, L);
int counter1 = 0;
int counter2 = 0;
int counter3 = 0;
for (int i = 0; i < arrr.size(); i++) {
if(arrr.get(i).length() - arrr.get(i).replaceAll("0", "").length() == 5) {
counter1++;
}
if(arrr.get(i).length() - arrr.get(i).replaceAll("1", "").length() >= 10) {
counter2++;
}
if(arrr.get(i).length() - arrr.get(i).replaceAll("1", "").length() < arrr.get(i).length() - arrr.get(i).replaceAll("0", "").length()) {
counter3++;
}
}
System.out.println("answer 1: " + counter1);
System.out.println("answer 2: " + counter2);
System.out.println("answer 3: " + counter3);
}
}
paste this into: https://www.jdoodle.com/online-java-compiler/
Any insight will be much appreciated.
Isn't this just binomial coefficient stuff? Consider an length-$n$ bit string, and suppose it starts out all zeroes, and you want to put exactly $k\le n$ ones in it. Then there are $n$ positions in which you can put the first one, $n-1$ remaining positions for the second one, etc. So in the end there are $n\times(n-1)\times\ldots\times(n-k+1)=\frac{n!}{(n-k)!}$ ways. But that includes "duplicates" when you just change the order. That is, e.g., place the first $1$ in position $5$ and the second $1$ in position $7$, or vice versa. And that's just the number of permutations of $k$ things, or $k!$. So divide by that to get the ususal binomial coefficient $\frac{n!}{k!(n-k)!}=\left(n\atop k\right)$.
So that would be for >>exactly<< $k$ ones in a length-$n$ bit string (or conversely $k$ zeroes, by similar reasoning). For, say $k$-or-less, just add up all the corresponding binomial coefficients $\displaystyle\sum^k_{i=0}\left(n\atop i\right)$. And note that $\displaystyle\sum^n_{i=0}\left(n\atop i\right)=2^n$, exactly the number of different binary strings of length $n$, as expected.