diff --git a/exercise8/main.tex b/exercise8/main.tex new file mode 100644 index 0000000..2e8c25c --- /dev/null +++ b/exercise8/main.tex @@ -0,0 +1,272 @@ +\documentclass[12pt]{article} +\usepackage{ntnu} +\usepackage{ntnu-math} +\usepackage{ntnu-code} + +\author{Øystein Tveit} +\title{MA0301 Exercise 8} + +\usepackage{amsthm} +\usepackage{mathabx} + +\begin{document} + \ntnuTitle{} + \break{} + + \begin{excs} + + \exc{} + \begin{subexcs} + \subexc{} + Since there is only one president, the possibilities is the sum of candidates + \[ 5 + 8 = 13 \] + + \subexc{} + For every candidate from one party there is all the candidates of the other party to be compared to. Therefore the amount of possibilities is + \[ 5 \cdot 8 = 13 \] + + \end{subexcs} + + \exc{} + \begin{subexcs} + \subexc{} + To end up with the amount of possibilities, we have to multiply the amounts of components together + \[ 4 \cdot 12 \cdot 3 \cdot 2 = 288 \] + + \subexc{} + This reduces the amount of colors from 4 to 1. Therefore the amount of possibilities is + \[ 1 \cdot 4 \cdot 3 \cot 2 = 24 \] + + \end{subexcs} + + \exc{} + \begin{subexcs} + \subexc{} + Let one bakery item be either pastry or muffins. + \[ (8 + 6) \cdot (4 + 6 + 1 + 1) = 14 \cdot 12 = 168 \] + + \subexc{} + \[ 14 \cdot 4 \cdot 6 \cdot 6 = 2016 \] + + \subexc{} + \[ 8 \cdot 6 \cdot 6 \cdot 1 \cdot 2(14 \cdot 4) = 32256 \] + + \end{subexcs} + + \exc{} + + \[ \nPr{8!}{8!} = \frac{8!}{(8-8)!} = 8! = 40320 \] + + \exc{} + \begin{subexcs} + \subexc{} + \[ \nPr{7}{2} = \frac{7!}{(7-2)!} = \frac{7!}{5!} = 7 \cdot 6 = 42 \] + + \subexc{} + \[ \nPr{8}{4} = \frac{8!}{(8-4)!} = 8 \cdot 7 \cdot 6 \cdot 5 = 1680 \] + + \subexc{} + \[ \nPr{10}{7} = \frac{10!}{(10-7)!} = 10 \cdot 9 \cdot \ldots \cdot 4 = 604800 \] + + \subexc{} + \[ \nPr{12}{3} = \frac{12!}{(12-3)!} = 12 \cdot 11 \cdot 10 = 1320 \] + + \end{subexcs} + + \exc{} + \begin{subexcs} + \subexc{} + \[ \nCr{10}{4} = \frac{10!}{4!(10-4)!} = \frac{10!}{4!6!} = \frac{10 \cdot \cancelto{3}{9} \cdot \cancelto{1}{8} \cdot 7}{1 \cdot \cancel{2} \cdot \cancel{3} \cdot \cancel{4}} = 10 \cdot 3 \cdot 7 = 210 \] + + \subexc{} + \[ \nCr{12}{7} = \frac{12!}{7!5!} = \frac{\cancelto{1}{12} \cdot 11 \cdot \cancelto{1}{10} \cdot 9 \cdot 8}{1 \cdot \cancel{2} \cdot \cancel{3} \cdot \cancel{4} \cdot \cancel{5}} = 11 \cdot 9 \cdot 8 = 792 \] + + \subexc{} + \[ \nCr{14}{12} = \frac{14!}{12!2!} = \frac{14 \cdot 13}{2} = 91 \] + + \subexc{} + \[ \nCr{15}{10} = \frac{15!}{10!5!} = \frac{\cancelto{1}{15} \cdot \cancelto{7}{14} \cdot 13 \cdot \cancelto{3}{12} \cdot 11}{1 \cdot \cancel{2} \cdot \cancel{3} \cdot \cancel{4} \cdot \cancel{5}} = 7 \cdot 13 \cdot 3 \cdot 11 = 3003 \] + + \end{subexcs} + + \exc{} + \begin{subexcs} + \subexc{} + + If there are no restrictions, the amount of arrangements will be the number of permutations of the books + + \[ \nPr{7}{7} = 7! \] + + In order for the languages to alternate, the 3 books has to be inbetween each of the 4 books + + There are $\nPr{3}{3}$ permutations of the C++ books. For each of those permutations, there are $\nPr{4}{4}$ permutations of the Java books. + + Hence the amount of ways to arrange the books will be + + \subexc{} + \[ \nPr{3,3} \cdot \nPr{4}{4} = 3! \cdot 4! = 144 \] + + Because all the C++ books has to be together, we can think of them like one "block" in the permutations of 5 blocks where four of the blocks are Java books. + + For each of those blocks, there is $\nPr{3}{3}$ arrangements of the C++ books. + + Hence the amount of ways to arrange the books will be + + \subexc{} + \[ \nPr{3}{3} \cdot \nPr{5}{5} = 3! \cdot 5! = 720 \] + + Here, there are just two blocks. Therefore there is only $\nPr{2}{2}$ permutations of the blocks + + For each of those permutations, there are $\nPr{3}{3}$ ways to arrange the C++ books. + + And for each way to arrange the C++ books, there are $\nPr{4}{4}$ ways to arrange the java books. + + Hence the amount of ways to arrange the books will be + + \subexc{} + \[ \nPr{2}{2} \cdot \nPr{3}{3} \cdot \nPr{4}{4} = 288 \] + + \end{subexcs} + + \exc{} + \begin{subexcs} + \subexc{} + Because we don't care if there's a different order that the people were selected, we have to use combinations. + + No restrictions means every combination of 12 in 20 people. + + \[ \nCr{20}{12} = 125970 \] + + \subexc{} + For every combination of six women, we have every combination of six men. + \[ \nCr{10}{6} \cdot \nCr{10}{6} = 44100 \] + + \subexc{} + Here we sum together all the combinations where there is an even number of women for every corresponding combination of men. + \[ \sum^5_{i=0} \nCr{10}{2i} \cdot \nCr{10}{2(6-i)} = 63090 \] + + \subexc{} + In order for the selection to contain more women than men, the amount of women has to be 7 so that the amount of men is 5. + + Therefore, we sum together all combination products from 7 to 10. + \[ \sum^{10}_{i=7} \nCr{10}{i} \cdot \nCr{10}{12-i} = 40935 \] + + \subexc{} + Sum of all combination products from 8 to 10. + \[ \sum^{10}_{i=8} \nCr{10}{i} \cdot \nCr{10}{12-i} = 10695 \] + + \end{subexcs} + + \exc{} + + In order to solve this task, we will sum together separate cases \\ + + Case i) The number only contains one distinct digit from $\{1, 3, 7, 8\}$ + + This would be all the permutations of the digits, that is + + \[ \nPr{4}{4} \] \\ + + Case ii) The number contains 2 of the digit $3$ and two distinct digits from $\{1, 7, 8\}$ + + Here, we start with all the ways we can form a four digit number including two of the digit $3$. + + Since we don't care what order the $3$s are in, we want the combinations (for example, $x{\color{red}3}{\color{ForestGreen}3}x$ and $x{\color{ForestGreen}3}{\color{red}3}x$ are the same) + + Therefore the amount of ways we can write a four digit number including two of the digit $3$ would be + + \[\nCr{4}{2}\] + + For each of those ways to write the number, there are all the permutations of the remaining digits ways to construct a number (here order does matter since the digits are distinct) + + Hence, the amount of ways we can write this number would be + + \[\nCr{4}{2} \cdot \nPr{3}{3}\] \\ + + Case iii) The number contains 2 of the digit $7$ and two distinct digits from $\{1, 3, 8\}$ + + This is the same as Case ii, just with $7$s instead of $3s$ \\ + + Case iv) The number contains 2 of the digit $3$ and 2 of the digit $7$ + + In this case, the amount of combinations would be the same as the amount of the ways we can write a four digit number with two fixed numbers. + + Hence, the amount of ways we can write this number would be + + \[\nCr{4}{2}\] + + In conclusion, the total amount of distinct four digit integers we can make with the digits $1$, $3$, $3$, $7$, $7$, $8$ would be + + \[ \nPr{4}{4} + \nCr{4}{2} \cdot \nPr{3}{3} \cdot 2 + \nCr{4}{2} = 102 \] + + \exc{} + + Here i wrote a program to calculate a modified pascal triangle and print the coefficients of a specific row + + \codeFile{scripts/pascal.py}{python} + + Here are the results: + + \begin{verbatim} + a. + 1 x^12 y^0 + 12 x^11 y^1 + 66 x^10 y^2 + 220 x^9 y^3 + 495 x^8 y^4 + 792 x^7 y^5 + 924 x^6 y^6 + 792 x^5 y^7 + 495 x^4 y^8 + 220 x^3 y^9 + 66 x^2 y^10 + 12 x^1 y^11 + 1 x^0 y^12 + + b. + 1 x^12 y^0 + 24 x^11 y^1 + 264 x^10 y^2 + 1760 x^9 y^3 + 7920 x^8 y^4 + 25344 x^7 y^5 + 59136 x^6 y^6 + 101376 x^5 y^7 + 126720 x^4 y^8 + 112640 x^3 y^9 + 67584 x^2 y^10 + 24576 x^1 y^11 + 4096 x^0 y^12 + + c. + 4096 x^12 y^0 + -73728 x^11 y^1 + 608256 x^10 y^2 + -3041280 x^9 y^3 + 10264320 x^8 y^4 + -24634368 x^7 y^5 + 43110144 x^6 y^6 + -55427328 x^5 y^7 + 51963120 x^4 y^8 + -34642080 x^3 y^9 + 15588936 x^2 y^10 + -4251528 x^1 y^11 + 531441 x^0 y^12 + \end{verbatim} + + Which means that the coefficient for $x^9y^3$ for each of the subexercises would be + + \begin{subexcs} + \subexc{} + \[ 220 \] + + \subexc{} + \[ 17601 \] + + \subexc{} + \[ -3041280 \] + + \end{subexcs} + + \end{excs} +\end{document} \ No newline at end of file diff --git a/exercise8/scripts/combination-bruteforce.py b/exercise8/scripts/combination-bruteforce.py new file mode 100644 index 0000000..5d656cd --- /dev/null +++ b/exercise8/scripts/combination-bruteforce.py @@ -0,0 +1,25 @@ +from itertools import permutations, combinations + +def amountOfBooksWhere(fun): + return len([bookOrder for bookOrder in permutations('jjjjccc') if fun(''.join(bookOrder))]) + +# a) +def no_restriction(books): + return True + +# b) +def alternating_books(books): + return all(books.find(l+l) == -1 for l in set(books)) + +# c) +def all_cs_are_together(books): + return not books.find('ccc') == -1 + +# d) +def all_cs_js_are_together(books): + return not (books.find('ccc') == -1 or books.find('jjjj') == -1) + +print(amountOfBooksWhere(no_restriction)) +print(amountOfBooksWhere(alternating_books)) +print(amountOfBooksWhere(all_cs_are_together)) +print(amountOfBooksWhere(all_cs_js_are_together)) diff --git a/exercise8/scripts/pascal.py b/exercise8/scripts/pascal.py new file mode 100644 index 0000000..36c554f --- /dev/null +++ b/exercise8/scripts/pascal.py @@ -0,0 +1,38 @@ +from math import comb + +def pascal(n, left, right): + i = 1 + result = [[left, right]] + + while i < n: + prev_row = result[-1] + result.append( + [left ** (i+1)] + + [comb(i+1, j+1) * (left ** (len(prev_row)-j-1)) * (right ** (j+1)) for j in range(len(prev_row)-1)] + + [right ** (i+1)]) + + i += 1 + + return result + +def pretty_print_pascal(triangle): + for i, row in enumerate(triangle): + print(f'{i+1}.\t{row}') + +def pretty_print_row(row): + for i, coefficient in enumerate(row): + print(f'{coefficient}\tx^{len(row)-i-1}\ty^{i}') + + +if __name__ == "__main__": + + print('a. ') + pretty_print_row(pascal(12, 1, 1)[-1]) + print() + + print('b. ') + pretty_print_row(pascal(12, 1, 2)[-1]) + print() + + print('c. ') + pretty_print_row(pascal(12, 2, -3)[-1])