Calculate total number of iterations

160 Views Asked by At

I am writing a program using iterations, but I have next to none math skills. My explanation will be lacking keywords and definitions so please excuse that, but my question is as follows:

I am generating iterations and using them, it's a mix of entries, let's say "$A$" and "$B$", Where $AB != BA$ - with repetition $2$ the complete list looks like this: $AA$, $AB$, $BA$, $BB$.

Now, I would like to know how long my iteration will be in total if I still had "$A$" and "$B$", but $5$ repetitions with an output looking like this $AAABA$ eg.

How do I go about calculating $X$ entries and $Y$ repetitions?

I dunno if I should just have paid more attention in high school and this is a stupidly easy question, but if you take the time to help it would help a hobby programmer greatly!

(sry for the tags if they are all wrong, I don't know what any of them mean)

3

There are 3 best solutions below

4
On BEST ANSWER

Fast answer:

$$\text{number of items}^{\text{number of spaces}}$$

for example, in 5 repetitions and entry $A,B$, the number of spaces is 5 and number of items is 2

Also, if your program has a part like:

    for variable1 in lista1:
        for variable2 in lista2:
            print(variable1+variable2)

Then you can do:

    count = 0
    for variable1 in lista1:
        for variable2 in lista2:
            count += 1
            print(variable1+variable2)
    print(count)   

The math behind is like this:

You have $X$ items, and you have $Y$ slots, then in the first of slot how many options do you have to put one item? Of course, you have $X$ options.

Then for the second slot, you ask how many items can be in that slot? Of course, the answer is again $X$.

Since you have $y$ slots, and in everyone of them you can put $X$ items, you will have a total of combinatios of :

$$X\times X\times X\times \cdots X$$

How many times? The number of slots that you have, it is $Y$

So the number of combinatios are $$\underbrace{X\times X\times X\times \cdots X}_{Y \text{times}} = X^Y$$

0
On

I think you are asking for the number of $n$ letter "words" using the two letter alphabet $\{A,B\}$. There are $2^n$ of them. For $n=2$ they are the $4$ you wrote down. For $n=5$ there will be $32$. If you start to write them down in alphabetical order you should see why this is the case.

If the alphabet has $k$ letters there will be $k^n$ words of length $n$.

3
On

All the above answers are correct. I just want to add that if you happen to be using Python, from itertools import product allows you to really easily do this by typing product(["A", "B"], repeat=5) (do note that this returns an itertools object, so you can convert it into a list by wrapping it in list().