# Week 5: Homework Solutions

## 3.

```def factorial(n):
"""Calculate n factorial."""
product = 1
for i in range(1, n + 1):
product = product * i
return product

def binomial(n, k):
"""Calculate n choose k."""
return factorial(n) // (factorial(n-k) * factorial(k))

rows = int(input("How many rows? "))
for n in range(rows):
# print the right number of tabs at the start of the row
print("\t"*(rows-n-1), end="")
for k in range(n):
print(binomial(n, k), end="\t\t")
print(binomial(n, n), end="\n")
```

To do the more adventurous bit, we keep the functions the same and change the main part of the program to be as follows.

```rows = 32
for n in range(rows + 1):
print(" "*(top-n-1), end="")
for k in range(n + 1):
if binomial(n, k) % 2 == 1:
print("*", end=" ")
else:
print(" ", end=" ")
print("")
```

The output is below.

```                               *
* *
*   *
* * * *
*       *
* *     * *
*   *   *   *
* * * * * * * *
*               *
* *             * *
*   *           *   *
* * * *         * * * *
*       *       *       *
* *     * *     * *     * *
*   *   *   *   *   *   *   *
* * * * * * * * * * * * * * * *
*                               *
* *                             * *
*   *                           *   *
* * * *                         * * * *
*       *                       *       *
* *     * *                     * *     * *
*   *   *   *                   *   *   *   *
* * * * * * * *                 * * * * * * * *
*               *               *               *
* *             * *             * *             * *
*   *           *   *           *   *           *   *
* * * *         * * * *         * * * *         * * * *
*       *       *       *       *       *       *       *
* *     * *     * *     * *     * *     * *     * *     * *
*   *   *   *   *   *   *   *   *   *   *   *   *   *   *   *
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
```