1186 lines
35 KiB
Plaintext
1186 lines
35 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"LCM(82, 21) = 1722, Here, GCD(82, 21) = 1.\n",
|
|
"None\n",
|
|
"LCM(42, 123) = 1722, Here, GCD(42, 123) = 3.\n",
|
|
"None\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from tools import *\n",
|
|
"from sympy import Poly, symbols, Piecewise, sqrt, symbols, Eq, solve, factorint\n",
|
|
"import numpy as np\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Extended Euclidean Algorithm\n",
|
|
"Thanks to Jesper TA for making this <3\n",
|
|
"Prints a table with all values, as well as the linear combination. "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"LCM(190, 124) = 11780, Here, GCD(190, 124) = 2.\n",
|
|
"28\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"lcm(190,124)\n",
|
|
"\n",
|
|
"print(190*100-124*153)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 117,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"i \t r_i \t r_i+1 \t q_i+1 \t r_i+2 \t s_i \t t_i\n",
|
|
"0 \t 189 \t 232 \t 0 \t 189 \t 1 \t 0\n",
|
|
"1 \t 232 \t 189 \t 1 \t 43 \t 0 \t 1\n",
|
|
"2 \t 189 \t 43 \t 4 \t 17 \t 1 \t 0\n",
|
|
"3 \t 43 \t 17 \t 2 \t 9 \t -1 \t 1\n",
|
|
"4 \t 17 \t 9 \t 1 \t 8 \t 5 \t -4\n",
|
|
"5 \t 9 \t 8 \t 1 \t 1 \t -11 \t 9\n",
|
|
"6 \t 8 \t 1 \t 8 \t 0 \t 16 \t -13\n",
|
|
"\t\t\t\t\t -27 \t 22\n",
|
|
"GCD(189, 232) = 1, with coefficients -27 and 22 such that 1 = (-27)*189 + (22)*232\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"gcd(189,232)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Least common multiple (LCM)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 118,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"LCM(189, 232) = 43848, Here, GCD(189, 232) = 1.\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"lcm(189,232)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# System of congruences\n",
|
|
"Thanks to Alexander for making this <3\n",
|
|
"#vi ønsker at løse systemet: x ≡ 2 mod 6 og x ≡ 3 mod 7\n",
|
|
"syssolver([[2,6],[3,7]])\n",
|
|
"\n",
|
|
"Works by:\n",
|
|
"$$\n",
|
|
"x \\equiv a_1 \\mod{n_1}\\\\\n",
|
|
"x \\equiv a_2 \\mod{n_2}\\\\\n",
|
|
"x \\equiv a_3 \\mod{n_3}\n",
|
|
"$$\n",
|
|
"Call the function like this: `congruences_system_solver([[a_1, n_1], [a_2, n_2], [a_3,n_3]])`\n",
|
|
"First output returned is the smallest pos int solution to the system of congruences.\n",
|
|
"\n",
|
|
"\n",
|
|
"Want to solve\n",
|
|
"$$\n",
|
|
"x \\equiv 2 \\mod{6}\\\\\n",
|
|
"x \\equiv 3 \\mod{7}\n",
|
|
"$$\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 119,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"all solutions are 2 + 91k\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(2, 91, [1], [1])"
|
|
]
|
|
},
|
|
"execution_count": 119,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"congruences_system_solver([[2,91]])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"So the answer we get is `(38, 42, [7, 6], [1, -1])` where $38$ is the smallest non-negative integer solution to the system of congruences. $42$ is the least common multiple of the moduli if the moduli are pairwise coprime.\n",
|
|
"This means the full solution can be written as $\\{38 + 42k \\mid k \\in \\mathbb{Z}\\}$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 120,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"all solutions are 68 + 105k\n"
|
|
]
|
|
},
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(68, 105, [35, 21, 15], [-1, 1, 1])"
|
|
]
|
|
},
|
|
"execution_count": 120,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"congruences_system_solver([[2,3],[3,5],[5,7]])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"So all solutions can be written as $\\{68+105k \\mid k \\in \\mathbb{Z} \\}$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Congruences with coefficients BETTER?\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 25,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"All solutions are 59 + 70k\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"system = [\n",
|
|
" (1, 1, 2), \n",
|
|
" (1, 4, 5), \n",
|
|
" (1, 3, 7), \n",
|
|
"]\n",
|
|
"\n",
|
|
"# Solve the system\n",
|
|
"solution, modulus, M_list, y_list = some_congruences_system_solver(system)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"All solutions are 59 + 70k\n",
|
|
"Solution: x ≡ 59 (mod 70)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#a * x ≡ c (mod m)\n",
|
|
"# (a, c, m)\n",
|
|
"system = [\n",
|
|
" (1, 1, 2), # x ≡ 1 (mod 2)\n",
|
|
" (1, 4, 5), # x ≡ 4 (mod 5)\n",
|
|
" (1, 3, 7), # x ≡ 3 (mod 7)\n",
|
|
"]\n",
|
|
"\n",
|
|
"# Solve the system\n",
|
|
"solution, modulus, M_list, y_list = some_congruences_system_solver(system)\n",
|
|
"\n",
|
|
"# Output the solution\n",
|
|
"print(f\"Solution: x ≡ {solution} (mod {modulus})\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 123,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"All solutions are 7 + 42k\n",
|
|
"Solution: x ≡ 7 (mod 42)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"system = [\n",
|
|
" (2, 2, 6),\n",
|
|
" (1, 7, 14),\n",
|
|
"]\n",
|
|
"solution, modulus, M_list, y_list = some_congruences_system_solver(system)\n",
|
|
"print(f\"Solution: x ≡ {solution} (mod {modulus})\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"Only a solution when c is a multiple of 7"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Multiplicative inverse \n",
|
|
"Input a value for $n$ and it will determine the multiplicative inverse of $n \\mod{m}$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 124,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"6\n",
|
|
"Does not exist\n",
|
|
"5\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#print(multiplicative_inverse(n,m))\n",
|
|
"print(multiplicative_inverse(3,17))\n",
|
|
"print(multiplicative_inverse(4,8))\n",
|
|
"print(multiplicative_inverse(5,8))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# factor a number to a product of primes"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{3: 1, 7: 2}"
|
|
]
|
|
},
|
|
"execution_count": 152,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"factorint(147)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Check if is prime number\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 125,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"True"
|
|
]
|
|
},
|
|
"execution_count": 125,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"is_prime(2)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Get all primes below n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 126,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'Number of primes below 100 is 25 and the list is [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]'"
|
|
]
|
|
},
|
|
"execution_count": 126,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"primes_below(100)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Whole number division\n",
|
|
"Divides a,b and returns the rest and factor."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 127,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"'The quotient of 11 divided by 20 is 0, and the remainder is 11.'"
|
|
]
|
|
},
|
|
"execution_count": 127,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"divide_with_remainder(11, 20)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Permutations \n",
|
|
"Permutations: Arrange all n objects from the set.\n",
|
|
"\n",
|
|
"Can also be used for bitstrings. Returns all the permutations. Then you can afterwards create a program, which can count the pattern you are looking for."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 130,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['abcde', 'abced', 'abdce', 'abdec', 'abecd', 'abedc', 'acbde', 'acbed', 'acdbe', 'acdeb', 'acebd', 'acedb', 'adbce', 'adbec', 'adcbe', 'adceb', 'adebc', 'adecb', 'aebcd', 'aebdc', 'aecbd', 'aecdb', 'aedbc', 'aedcb', 'bacde', 'baced', 'badce', 'badec', 'baecd', 'baedc', 'bcade', 'bcaed', 'bcdae', 'bcdea', 'bcead', 'bceda', 'bdace', 'bdaec', 'bdcae', 'bdcea', 'bdeac', 'bdeca', 'beacd', 'beadc', 'becad', 'becda', 'bedac', 'bedca', 'cabde', 'cabed', 'cadbe', 'cadeb', 'caebd', 'caedb', 'cbade', 'cbaed', 'cbdae', 'cbdea', 'cbead', 'cbeda', 'cdabe', 'cdaeb', 'cdbae', 'cdbea', 'cdeab', 'cdeba', 'ceabd', 'ceadb', 'cebad', 'cebda', 'cedab', 'cedba', 'dabce', 'dabec', 'dacbe', 'daceb', 'daebc', 'daecb', 'dbace', 'dbaec', 'dbcae', 'dbcea', 'dbeac', 'dbeca', 'dcabe', 'dcaeb', 'dcbae', 'dcbea', 'dceab', 'dceba', 'deabc', 'deacb', 'debac', 'debca', 'decab', 'decba', 'eabcd', 'eabdc', 'eacbd', 'eacdb', 'eadbc', 'eadcb', 'ebacd', 'ebadc', 'ebcad', 'ebcda', 'ebdac', 'ebdca', 'ecabd', 'ecadb', 'ecbad', 'ecbda', 'ecdab', 'ecdba', 'edabc', 'edacb', 'edbac', 'edbca', 'edcab', 'edcba']\n",
|
|
"114 do not contain abc\n",
|
|
"42 contain ab or bc\n",
|
|
"36 contain ab or bc but not abc\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"perms = get_permutations('abcde')\n",
|
|
"print(perms)\n",
|
|
"\n",
|
|
"# task could be to figure out how many DO NOT contain abc\n",
|
|
"count = 0\n",
|
|
"pattern = 'abc'\n",
|
|
"for perm in perms:\n",
|
|
" if pattern not in perm:\n",
|
|
" count +=1\n",
|
|
"print(f'{count} do not contain {pattern}')\n",
|
|
"\n",
|
|
"# contain ab or ac\n",
|
|
"count = 0\n",
|
|
"pattern1 = 'ab'\n",
|
|
"pattern2 = 'bc'\n",
|
|
"for perm in perms:\n",
|
|
" if pattern1 in perm or pattern2 in perm:\n",
|
|
" count +=1\n",
|
|
"print(f'{count} contain {pattern1} or {pattern2}')\n",
|
|
"\n",
|
|
"#How many contain ab or bc but not abc?\n",
|
|
"pattern1 = 'ab'\n",
|
|
"pattern2 = 'bc'\n",
|
|
"pattern3 = 'abc'\n",
|
|
"\n",
|
|
"count = 0\n",
|
|
"for perm in perms:\n",
|
|
" if pattern1 in perm or pattern2 in perm:\n",
|
|
" if pattern3 not in perm:\n",
|
|
" count +=1\n",
|
|
"print(f'{count} contain ab or bc but not abc')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# All bit strings of length n\n",
|
|
"Return all bit strings of a specific length"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 131,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"[[0, 0, 0, 0, 0], [0, 0, 0, 0, 1], [0, 0, 0, 1, 0], [0, 0, 0, 1, 1], [0, 0, 1, 0, 0], [0, 0, 1, 0, 1], [0, 0, 1, 1, 0], [0, 0, 1, 1, 1], [0, 1, 0, 0, 0], [0, 1, 0, 0, 1], [0, 1, 0, 1, 0], [0, 1, 0, 1, 1], [0, 1, 1, 0, 0], [0, 1, 1, 0, 1], [0, 1, 1, 1, 0], [0, 1, 1, 1, 1], [1, 0, 0, 0, 0], [1, 0, 0, 0, 1], [1, 0, 0, 1, 0], [1, 0, 0, 1, 1], [1, 0, 1, 0, 0], [1, 0, 1, 0, 1], [1, 0, 1, 1, 0], [1, 0, 1, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 0, 1], [1, 1, 0, 1, 0], [1, 1, 0, 1, 1], [1, 1, 1, 0, 0], [1, 1, 1, 0, 1], [1, 1, 1, 1, 0], [1, 1, 1, 1, 1]]\n",
|
|
"['00000', '00001', '00010', '00011', '00100', '00101', '00110', '00111', '01000', '01001', '01010', '01011', '01100', '01101', '01110', '01111', '10000', '10001', '10010', '10011', '10100', '10101', '10110', '10111', '11000', '11001', '11010', '11011', '11100', '11101', '11110', '11111']\n",
|
|
"number of bit strings of length 5 that end with \"11\" are 8\n",
|
|
"number of bit strings of length 5 that starts with \"11\" are 8\n",
|
|
"number of bit strings of length 5 that contains \"111\" are 8\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"bit_strings = get_binary_strings(5)\n",
|
|
"bit_strings_string = get_binary_strings(5, as_strings=True)\n",
|
|
"print(bit_strings)\n",
|
|
"print(bit_strings_string)\n",
|
|
"\n",
|
|
"# how many of them end with 11\n",
|
|
"count = 0\n",
|
|
"for element in bit_strings_string:\n",
|
|
" if element.endswith('11'):\n",
|
|
" count+=1\n",
|
|
"print(f'number of bit strings of length 5 that end with \"11\" are {count}')\n",
|
|
"\n",
|
|
"count = 0\n",
|
|
"for element in bit_strings_string:\n",
|
|
" if element.startswith('11'):\n",
|
|
" count+=1\n",
|
|
"print(f'number of bit strings of length 5 that starts with \"11\" are {count}')\n",
|
|
"\n",
|
|
"# contains 111\n",
|
|
"count = 0\n",
|
|
"for element in bit_strings_string:\n",
|
|
" if '111' in element:\n",
|
|
" count+=1\n",
|
|
"print(f'number of bit strings of length 5 that contains \"111\" are {count}')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# r-permutations\n",
|
|
"r-permutations: Arrange only r objects, chosen from n."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 132,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"20\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"r_permutations = get_r_permutations(\"abcde\", 2)\n",
|
|
"print(len(r_permutations))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Combinations\n",
|
|
"Returns all possible combinations without ordering. So \"AB\" == \"BA\"."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 133,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"31\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"all_combinations = get_combinations(\"abcde\")\n",
|
|
"print(len(all_combinations))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# R-Combinations\n",
|
|
"All combinations of size r."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 134,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"10\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"all_r_combinations = get_r_combinations(\"abcde\", 2)\n",
|
|
"print(len(all_r_combinations))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Inclusion exclusion"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 135,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1\n",
|
|
"8\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"A = {1,2,3,4}\n",
|
|
"B = {3,4,5,6}\n",
|
|
"C = {1,3,8,9}\n",
|
|
"\n",
|
|
"# intersection\n",
|
|
"intersection = A & B &C\n",
|
|
"print(len(intersection))\n",
|
|
"\n",
|
|
"# union\n",
|
|
"union = A | B | C\n",
|
|
"print(len(union))\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Guide to Finding Coefficients in Polynomial Expansions\n",
|
|
"\n",
|
|
"This guide explains how to compute the coefficient of a specific term in the expansion of a polynomial like \\((2x + 3y)^9\\) using Python and the SymPy library. Say we want to find the coefficient of $x^7y^2$.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Step 1: Understand the Binomial Formula\n",
|
|
"For a binomial expression \\((ax + by)^n\\), the coefficient of \\(x^k y^m\\) is given by:\n",
|
|
"\n",
|
|
"$$\n",
|
|
"\\text{Coefficient} = \\binom{n}{k} \\cdot a^k \\cdot b^m\n",
|
|
"$$\n",
|
|
"\n",
|
|
"where:\n",
|
|
"- \\(n\\) is the power of the binomial.\n",
|
|
"- \\(k\\) is the power of \\(x\\).\n",
|
|
"- \\(m = n - k\\) is the power of \\(y\\).\n",
|
|
"- \\(a\\) and \\(b\\) are the coefficients of \\(x\\) and \\(y\\), respectively.\n",
|
|
"\n",
|
|
"---\n",
|
|
"\n",
|
|
"## Step 2: Python Code\n",
|
|
"\n",
|
|
"Use the following Python code to calculate the coefficient:\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 136,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The coefficient of x^7y^2 in (2x + 3y)^9 is: 41472\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import symbols, binomial\n",
|
|
"\n",
|
|
"# (2x + 3y)^9 want to find the coefficient of x^7*y^2\n",
|
|
"\n",
|
|
"# Define variables\n",
|
|
"x, y = symbols('x y')\n",
|
|
"\n",
|
|
"# Define parameters\n",
|
|
"n = 9 # Power of the binomial\n",
|
|
"k = 7 # Power of x\n",
|
|
"m = 2 # Power of y\n",
|
|
"a = 2 # Coefficient of x\n",
|
|
"b = 3 # Coefficient of y\n",
|
|
"\n",
|
|
"# Calculate the coefficient\n",
|
|
"coefficient = binomial(n, k) * (a**k) * (b**m)\n",
|
|
"\n",
|
|
"# Output the result\n",
|
|
"print(f\"The coefficient of x^{k}y^{m} in (2x + 3y)^{n} is: {coefficient}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 137,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The coefficient of x^5y^3 is: 60025000\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import symbols, binomial\n",
|
|
"\n",
|
|
"# (5x + 7y)^8 want to find the coefficient of x^5*y^3\n",
|
|
"\n",
|
|
"# Define variables\n",
|
|
"x, y = symbols('x y')\n",
|
|
"\n",
|
|
"# Define parameters\n",
|
|
"n = 8 # Power of the binomial\n",
|
|
"k = 5 # Power of x in x^5*y^3\n",
|
|
"m = 3 # Power of y in x^5*y^3\n",
|
|
"a = 5 # Coefficient of x in (5x + 7y)^8\n",
|
|
"b = 7 # Coefficient of y in (5x + 7y)^8\n",
|
|
"\n",
|
|
"# Calculate the coefficient\n",
|
|
"coefficient = binomial(n, k) * (a**k) * (b**m)\n",
|
|
"\n",
|
|
"# Output the result\n",
|
|
"print(f\"The coefficient of x^{k}y^{m} is: {coefficient}\")\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 138,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The coefficient of x^12y^13 is: 5200300\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#What is the coefficient of x^12y^13 in the expansion of (x +y)^25?\n",
|
|
"\n",
|
|
"#x^k * y^m in the expansion of (ax+by)^n\n",
|
|
"\n",
|
|
"n = 25\n",
|
|
"k = 12\n",
|
|
"m = 13\n",
|
|
"a = 1\n",
|
|
"b = 1\n",
|
|
"\n",
|
|
"coefficient = binomial(n, k) * (a**k) * (b**m)\n",
|
|
"\n",
|
|
"# Output the result\n",
|
|
"print(f\"The coefficient of x^{k}y^{m} is: {coefficient}\")"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Binomial stuff Figure out correct naming\n",
|
|
"\n",
|
|
"Lets say we want to figure out if the following is true:\n",
|
|
"$$\n",
|
|
"n \\cdot 2^{n-1} = \\sum_{k=1}^n \\binom{n}{k} \\cdot k\n",
|
|
"$$"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 150,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/latex": [
|
|
"$\\displaystyle 2^{n - 1} n$"
|
|
],
|
|
"text/plain": [
|
|
"2**(n - 1)*n"
|
|
]
|
|
},
|
|
"execution_count": 150,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"from sympy import symbols, binomial, summation, Eq, simplify\n",
|
|
"\n",
|
|
"# Define the variables\n",
|
|
"# positive true does such that n and k are assumed to be greater than 0.\n",
|
|
"n, k = symbols('n k', integer=True, positive=True)\n",
|
|
"\n",
|
|
"\n",
|
|
"# summation(expression, (variable, lower_limit, upper_limit)) computes the sum of the expression over the specified range.\n",
|
|
"\n",
|
|
"# Options\n",
|
|
"option_1 = summation(binomial(n, k) * k, (k, 1, n))\n",
|
|
"# option_2 = summation(binomial(n, k) * binomial(n, k+1), (k, 0, n-1))\n",
|
|
"# option_3 = summation(2**k, (k, 1, n))\n",
|
|
"# option_4 = summation(2**k, (k, 0, n-1))\n",
|
|
"\n",
|
|
"\n",
|
|
"#option2 = summation(binomial(n,k)*binomial(n,k+1), (k,0,n-1))\n",
|
|
"\n",
|
|
"simplify(option_1)\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Number of derangements"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The number D_6 is 265\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"number_of_elements = 7\n",
|
|
"number_of_derangements = calculate_number_of_derangements(number_of_elements)\n",
|
|
"print(f'The number D_{number_of_elements} is {number_of_derangements}')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Get all derangements"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 22,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"all derangements of abcdef is 1854 and 108 ends with abc\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"all_derangements = get_derangements('1234567')\n",
|
|
"len(all_derangements)\n",
|
|
"\n",
|
|
"# all derangements ending with abc\n",
|
|
"count = 0\n",
|
|
"for derangment in all_derangements:\n",
|
|
" if derangment.endswith('123') or derangment.endswith('132') or derangment.endswith('213') or derangment.endswith('231') or derangment.endswith('321') or derangment.endswith('312'):\n",
|
|
" count +=1\n",
|
|
"print(f'all derangements of abcdef is {len(all_derangements)} and {count} ends with abc')\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Minimum selections for sum\n",
|
|
"Figure out how many numbers are needed from a set in order to gurantee that at least one pair adds to n? Pigeonhole principle"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 142,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"you need to select 5 numbers from {1, 3, 5, 7, 9, 11, 13, 15} in order to gurantee at least one pair gets you to 16\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"numbers_set = {1,3, 5,7,9,11,13,15}\n",
|
|
"target_sum = 16\n",
|
|
"\n",
|
|
"min = minimum_selections_for_sum(numbers_set, target_sum)\n",
|
|
"print(f'you need to select {min} numbers from {numbers_set} in order to gurantee at least one pair gets you to {target_sum}')"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Relations\n",
|
|
"check for the different properties."
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 143,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"Transitive? False\n",
|
|
"Symmetric? False\n",
|
|
"Reflexive? False\n",
|
|
"Antisymmetric? False\n",
|
|
"Equivalence relation? False\n",
|
|
"Partial ordering? False\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"a = 0\n",
|
|
"b = 1\n",
|
|
"c = 2\n",
|
|
"d = 3\n",
|
|
"\n",
|
|
"S = {a, b, c, d}\n",
|
|
"R = {(a,b),(b,a),(b,c),(c,b),(c,d),(d,a),(a,d)}\n",
|
|
"\n",
|
|
"print(\"Transitive?\", is_transitive(R))\n",
|
|
"print(\"Symmetric?\", is_symmetric(R))\n",
|
|
"print(\"Reflexive?\", is_reflexive(S, R))\n",
|
|
"print(\"Antisymmetric?\", is_antisymmetric(R))\n",
|
|
"print(\"Equivalence relation?\", is_equivalence_relation(S, R))\n",
|
|
"print(\"Partial ordering?\", is_partial_order(S, R))"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Number of vertices and edges in a n-dimensional cube"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"(16, 32)"
|
|
]
|
|
},
|
|
"execution_count": 151,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"n = 4\n",
|
|
"\n",
|
|
"vertices = 2**n\n",
|
|
"edges = n * 2**(n-1)\n",
|
|
"\n",
|
|
"vertices, edges\n",
|
|
"# output (vertices, edges)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Check if function is well defined"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 144,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"The function is well-defined over the given domain.\n",
|
|
"Result: True\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"def my_func(x):\n",
|
|
" return x**2\n",
|
|
"\n",
|
|
"domain = [1, 2, 3, -1, 0]\n",
|
|
"codomain = lambda y: isinstance(y, int)\n",
|
|
"\n",
|
|
"result = check_func(my_func, domain, codomain)\n",
|
|
"print(\"Result:\", result)\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Round seating example"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 145,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"['abcdef', 'abcdfe', 'abcedf', 'abcefd', 'abcfde', 'abcfed', 'abdcef', 'abdcfe', 'abdecf', 'abdefc', 'abdfce', 'abdfec', 'abecdf', 'abecfd', 'abedcf', 'abedfc', 'abefcd', 'abefdc', 'abfcde', 'abfced', 'abfdce', 'abfdec', 'abfecd', 'abfedc', 'acbdef', 'acbdfe', 'acbedf', 'acbefd', 'acbfde', 'acbfed', 'acdbef', 'acdbfe', 'acdebf', 'acdefb', 'acdfbe', 'acdfeb', 'acebdf', 'acebfd', 'acedbf', 'acedfb', 'acefbd', 'acefdb', 'acfbde', 'acfbed', 'acfdbe', 'acfdeb', 'acfebd', 'acfedb', 'adbcef', 'adbcfe', 'adbecf', 'adbefc', 'adbfce', 'adbfec', 'adcbef', 'adcbfe', 'adcebf', 'adcefb', 'adcfbe', 'adcfeb', 'adebcf', 'adebfc', 'adecbf', 'adecfb', 'adefbc', 'adefcb', 'adfbce', 'adfbec', 'adfcbe', 'adfceb', 'adfebc', 'adfecb', 'aebcdf', 'aebcfd', 'aebdcf', 'aebdfc', 'aebfcd', 'aebfdc', 'aecbdf', 'aecbfd', 'aecdbf', 'aecdfb', 'aecfbd', 'aecfdb', 'aedbcf', 'aedbfc', 'aedcbf', 'aedcfb', 'aedfbc', 'aedfcb', 'aefbcd', 'aefbdc', 'aefcbd', 'aefcdb', 'aefdbc', 'aefdcb', 'afbcde', 'afbced', 'afbdce', 'afbdec', 'afbecd', 'afbedc', 'afcbde', 'afcbed', 'afcdbe', 'afcdeb', 'afcebd', 'afcedb', 'afdbce', 'afdbec', 'afdcbe', 'afdceb', 'afdebc', 'afdecb', 'afebcd', 'afebdc', 'afecbd', 'afecdb', 'afedbc', 'afedcb', 'bacdef', 'bacdfe', 'bacedf', 'bacefd', 'bacfde', 'bacfed', 'badcef', 'badcfe', 'badecf', 'badefc', 'badfce', 'badfec', 'baecdf', 'baecfd', 'baedcf', 'baedfc', 'baefcd', 'baefdc', 'bafcde', 'bafced', 'bafdce', 'bafdec', 'bafecd', 'bafedc', 'bcadef', 'bcadfe', 'bcaedf', 'bcaefd', 'bcafde', 'bcafed', 'bcdaef', 'bcdafe', 'bcdeaf', 'bcdefa', 'bcdfae', 'bcdfea', 'bceadf', 'bceafd', 'bcedaf', 'bcedfa', 'bcefad', 'bcefda', 'bcfade', 'bcfaed', 'bcfdae', 'bcfdea', 'bcfead', 'bcfeda', 'bdacef', 'bdacfe', 'bdaecf', 'bdaefc', 'bdafce', 'bdafec', 'bdcaef', 'bdcafe', 'bdceaf', 'bdcefa', 'bdcfae', 'bdcfea', 'bdeacf', 'bdeafc', 'bdecaf', 'bdecfa', 'bdefac', 'bdefca', 'bdface', 'bdfaec', 'bdfcae', 'bdfcea', 'bdfeac', 'bdfeca', 'beacdf', 'beacfd', 'beadcf', 'beadfc', 'beafcd', 'beafdc', 'becadf', 'becafd', 'becdaf', 'becdfa', 'becfad', 'becfda', 'bedacf', 'bedafc', 'bedcaf', 'bedcfa', 'bedfac', 'bedfca', 'befacd', 'befadc', 'befcad', 'befcda', 'befdac', 'befdca', 'bfacde', 'bfaced', 'bfadce', 'bfadec', 'bfaecd', 'bfaedc', 'bfcade', 'bfcaed', 'bfcdae', 'bfcdea', 'bfcead', 'bfceda', 'bfdace', 'bfdaec', 'bfdcae', 'bfdcea', 'bfdeac', 'bfdeca', 'bfeacd', 'bfeadc', 'bfecad', 'bfecda', 'bfedac', 'bfedca', 'cabdef', 'cabdfe', 'cabedf', 'cabefd', 'cabfde', 'cabfed', 'cadbef', 'cadbfe', 'cadebf', 'cadefb', 'cadfbe', 'cadfeb', 'caebdf', 'caebfd', 'caedbf', 'caedfb', 'caefbd', 'caefdb', 'cafbde', 'cafbed', 'cafdbe', 'cafdeb', 'cafebd', 'cafedb', 'cbadef', 'cbadfe', 'cbaedf', 'cbaefd', 'cbafde', 'cbafed', 'cbdaef', 'cbdafe', 'cbdeaf', 'cbdefa', 'cbdfae', 'cbdfea', 'cbeadf', 'cbeafd', 'cbedaf', 'cbedfa', 'cbefad', 'cbefda', 'cbfade', 'cbfaed', 'cbfdae', 'cbfdea', 'cbfead', 'cbfeda', 'cdabef', 'cdabfe', 'cdaebf', 'cdaefb', 'cdafbe', 'cdafeb', 'cdbaef', 'cdbafe', 'cdbeaf', 'cdbefa', 'cdbfae', 'cdbfea', 'cdeabf', 'cdeafb', 'cdebaf', 'cdebfa', 'cdefab', 'cdefba', 'cdfabe', 'cdfaeb', 'cdfbae', 'cdfbea', 'cdfeab', 'cdfeba', 'ceabdf', 'ceabfd', 'ceadbf', 'ceadfb', 'ceafbd', 'ceafdb', 'cebadf', 'cebafd', 'cebdaf', 'cebdfa', 'cebfad', 'cebfda', 'cedabf', 'cedafb', 'cedbaf', 'cedbfa', 'cedfab', 'cedfba', 'cefabd', 'cefadb', 'cefbad', 'cefbda', 'cefdab', 'cefdba', 'cfabde', 'cfabed', 'cfadbe', 'cfadeb', 'cfaebd', 'cfaedb', 'cfbade', 'cfbaed', 'cfbdae', 'cfbdea', 'cfbead', 'cfbeda', 'cfdabe', 'cfdaeb', 'cfdbae', 'cfdbea', 'cfdeab', 'cfdeba', 'cfeabd', 'cfeadb', 'cfebad', 'cfebda', 'cfedab', 'cfedba', 'dabcef', 'dabcfe', 'dabecf', 'dabefc', 'dabfce', 'dabfec', 'dacbef', 'dacbfe', 'dacebf', 'dacefb', 'dacfbe', 'dacfeb', 'daebcf', 'daebfc', 'daecbf', 'daecfb', 'daefbc', 'daefcb', 'dafbce', 'dafbec', 'dafcbe', 'dafceb', 'dafebc', 'dafecb', 'dbacef', 'dbacfe', 'dbaecf', 'dbaefc', 'dbafce', 'dbafec', 'dbcaef', 'dbcafe', 'dbceaf', 'dbcefa', 'dbcfae', 'dbcfea', 'dbeacf', 'dbeafc', 'dbecaf', 'dbecfa', 'dbefac', 'dbefca', 'dbface', 'dbfaec', 'dbfcae', 'dbfcea', 'dbfeac', 'dbfeca', 'dcabef', 'dcabfe', 'dcaebf', 'dcaefb', 'dcafbe', 'dcafeb', 'dcbaef', 'dcbafe', 'dcbeaf', 'dcbefa', 'dcbfae', 'dcbfea', 'dceabf', 'dceafb', 'dcebaf', 'dcebfa', 'dcefab', 'dcefba', 'dcfabe', 'dcfaeb', 'dcfbae', 'dcfbea', 'dcfeab', 'dcfeba', 'deabcf', 'deabfc', 'deacbf', 'deacfb', 'deafbc', 'deafcb', 'debacf', 'debafc', 'debcaf', 'debcfa', 'debfac', 'debfca', 'decabf', 'decafb', 'decbaf', 'decbfa', 'decfab', 'decfba', 'defabc', 'defacb', 'defbac', 'defbca', 'defcab', 'defcba', 'dfabce', 'dfabec', 'dfacbe', 'dfaceb', 'dfaebc', 'dfaecb', 'dfbace', 'dfbaec', 'dfbcae', 'dfbcea', 'dfbeac', 'dfbeca', 'dfcabe', 'dfcaeb', 'dfcbae', 'dfcbea', 'dfceab', 'dfceba', 'dfeabc', 'dfeacb', 'dfebac', 'dfebca', 'dfecab', 'dfecba', 'eabcdf', 'eabcfd', 'eabdcf', 'eabdfc', 'eabfcd', 'eabfdc', 'eacbdf', 'eacbfd', 'eacdbf', 'eacdfb', 'eacfbd', 'eacfdb', 'eadbcf', 'eadbfc', 'eadcbf', 'eadcfb', 'eadfbc', 'eadfcb', 'eafbcd', 'eafbdc', 'eafcbd', 'eafcdb', 'eafdbc', 'eafdcb', 'ebacdf', 'ebacfd', 'ebadcf', 'ebadfc', 'ebafcd', 'ebafdc', 'ebcadf', 'ebcafd', 'ebcdaf', 'ebcdfa', 'ebcfad', 'ebcfda', 'ebdacf', 'ebdafc', 'ebdcaf', 'ebdcfa', 'ebdfac', 'ebdfca', 'ebfacd', 'ebfadc', 'ebfcad', 'ebfcda', 'ebfdac', 'ebfdca', 'ecabdf', 'ecabfd', 'ecadbf', 'ecadfb', 'ecafbd', 'ecafdb', 'ecbadf', 'ecbafd', 'ecbdaf', 'ecbdfa', 'ecbfad', 'ecbfda', 'ecdabf', 'ecdafb', 'ecdbaf', 'ecdbfa', 'ecdfab', 'ecdfba', 'ecfabd', 'ecfadb', 'ecfbad', 'ecfbda', 'ecfdab', 'ecfdba', 'edabcf', 'edabfc', 'edacbf', 'edacfb', 'edafbc', 'edafcb', 'edbacf', 'edbafc', 'edbcaf', 'edbcfa', 'edbfac', 'edbfca', 'edcabf', 'edcafb', 'edcbaf', 'edcbfa', 'edcfab', 'edcfba', 'edfabc', 'edfacb', 'edfbac', 'edfbca', 'edfcab', 'edfcba', 'efabcd', 'efabdc', 'efacbd', 'efacdb', 'efadbc', 'efadcb', 'efbacd', 'efbadc', 'efbcad', 'efbcda', 'efbdac', 'efbdca', 'efcabd', 'efcadb', 'efcbad', 'efcbda', 'efcdab', 'efcdba', 'efdabc', 'efdacb', 'efdbac', 'efdbca', 'efdcab', 'efdcba', 'fabcde', 'fabced', 'fabdce', 'fabdec', 'fabecd', 'fabedc', 'facbde', 'facbed', 'facdbe', 'facdeb', 'facebd', 'facedb', 'fadbce', 'fadbec', 'fadcbe', 'fadceb', 'fadebc', 'fadecb', 'faebcd', 'faebdc', 'faecbd', 'faecdb', 'faedbc', 'faedcb', 'fbacde', 'fbaced', 'fbadce', 'fbadec', 'fbaecd', 'fbaedc', 'fbcade', 'fbcaed', 'fbcdae', 'fbcdea', 'fbcead', 'fbceda', 'fbdace', 'fbdaec', 'fbdcae', 'fbdcea', 'fbdeac', 'fbdeca', 'fbeacd', 'fbeadc', 'fbecad', 'fbecda', 'fbedac', 'fbedca', 'fcabde', 'fcabed', 'fcadbe', 'fcadeb', 'fcaebd', 'fcaedb', 'fcbade', 'fcbaed', 'fcbdae', 'fcbdea', 'fcbead', 'fcbeda', 'fcdabe', 'fcdaeb', 'fcdbae', 'fcdbea', 'fcdeab', 'fcdeba', 'fceabd', 'fceadb', 'fcebad', 'fcebda', 'fcedab', 'fcedba', 'fdabce', 'fdabec', 'fdacbe', 'fdaceb', 'fdaebc', 'fdaecb', 'fdbace', 'fdbaec', 'fdbcae', 'fdbcea', 'fdbeac', 'fdbeca', 'fdcabe', 'fdcaeb', 'fdcbae', 'fdcbea', 'fdceab', 'fdceba', 'fdeabc', 'fdeacb', 'fdebac', 'fdebca', 'fdecab', 'fdecba', 'feabcd', 'feabdc', 'feacbd', 'feacdb', 'feadbc', 'feadcb', 'febacd', 'febadc', 'febcad', 'febcda', 'febdac', 'febdca', 'fecabd', 'fecadb', 'fecbad', 'fecbda', 'fecdab', 'fecdba', 'fedabc', 'fedacb', 'fedbac', 'fedbca', 'fedcab', 'fedcba']\n",
|
|
"576\n",
|
|
"432\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"tables = get_r_permutations('abcdef', 6)\n",
|
|
"print(tables)\n",
|
|
"\n",
|
|
"count =0\n",
|
|
"\n",
|
|
"# Rotaing the entire seating counts as different arrangement.\n",
|
|
"\n",
|
|
"# how many ways can they be seated such that A and B are not seated opposite to each other\n",
|
|
"for table in tables:\n",
|
|
" a_idx = table.find('a')\n",
|
|
" b_idx = table.find('b')\n",
|
|
" if table[(a_idx + 3) % len(table)] == 'b':\n",
|
|
" count+=1\n",
|
|
"print(len(tables)-count)\n",
|
|
"# how many ways can they be seased such that a and b are NOT seated next to each other.\n",
|
|
"count=0\n",
|
|
"for table in tables:\n",
|
|
" a_idx = table.find('a')\n",
|
|
" b_idx = table.find('b')\n",
|
|
" if table[(a_idx + 1) % len(table)] == 'b' or table[(a_idx -1) % len(table)] == 'b':\n",
|
|
" count+=1\n",
|
|
"print(len(tables)-count)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "markdown",
|
|
"metadata": {},
|
|
"source": [
|
|
"# Alice beslutter sig for at holde en fest hver 1000. dag startende med i dag, der er mandag. Hvor mange dag går der, før Alices fest falder på en torsdag?"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 146,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"4000\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"days = [\"monday\", \"tuesday\", \"wednesday\",\"thursday\", \"friday\", \"saturday\", \"sunday\"]\n",
|
|
"day = \"monday\"\n",
|
|
"count = 0\n",
|
|
"while day != \"thursday\":\n",
|
|
" d = days.index(day)\n",
|
|
" day = days[(d+1000)%len(days)]\n",
|
|
" count += 1000\n",
|
|
"print(count)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 147,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"1\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"#How many positive integers are between 50 and 100 are divisble by 7 and 11? and which are they?\n",
|
|
"count = 0\n",
|
|
"for i in range(50, 101):\n",
|
|
" if i % 7 == 0 and i % 11 == 0:\n",
|
|
" count+=1\n",
|
|
"print(count)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"data": {
|
|
"text/plain": [
|
|
"{2: 1, 5: 1}"
|
|
]
|
|
},
|
|
"execution_count": 148,
|
|
"metadata": {},
|
|
"output_type": "execute_result"
|
|
}
|
|
],
|
|
"source": [
|
|
"factorint(10)\n",
|
|
"\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": null,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": []
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"language_info": {
|
|
"codemirror_mode": {
|
|
"name": "ipython",
|
|
"version": 3
|
|
},
|
|
"file_extension": ".py",
|
|
"mimetype": "text/x-python",
|
|
"name": "python",
|
|
"nbconvert_exporter": "python",
|
|
"pygments_lexer": "ipython3",
|
|
"version": "3.13.7"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|