ladderz/notebooks/pre-algebra/unit1.ipynb

226 lines
6.6 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<a target=\"_blank\" href=\"https://colab.research.google.com/github/rzmk/ladderz/blob/main/notebooks/pre-algebra/unit1.ipynb\">\n",
" <img src=\"https://colab.research.google.com/assets/colab-badge.svg\" alt=\"Open In Colab\"/>\n",
"</a>"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Unit 1: Factors and multiples"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Understanding Factor Pairs\n",
"\n",
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/understanding-factor-pairs).\n",
"\n",
"**Write a program that finds all the factor pairs for a number $n$.**\n",
"\n",
"- Do not repeat any pairs (e.g., consider `(2, 8)` and `(8, 2)` as the same).\n",
"- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n",
"\n",
"For example for $n = 16$, the output of `get_factor_pairs(16)` may be:\n",
"\n",
"```\n",
"{(1, 16), (2, 8), (4, 4)}\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def get_factor_pairs(n: int) -> set:\n",
" factor_pairs: set = set()\n",
" for i in range(1, n + 1):\n",
" dividend: int = n\n",
" divisor: int = i\n",
" quotient: int = int(dividend / divisor)\n",
" remainder: int = dividend % divisor\n",
" if remainder == 0 and (quotient, divisor) not in factor_pairs:\n",
" factor_pairs.add((divisor, quotient))\n",
" return factor_pairs\n",
"\n",
"assert get_factor_pairs(1) == {(1, 1)}\n",
"assert get_factor_pairs(15) == {(1, 15), (3, 5)}\n",
"assert get_factor_pairs(16) == {(1, 16), (2, 8), (4, 4)}\n",
"assert get_factor_pairs(21) == {(1, 21), (3, 7)}\n",
"assert get_factor_pairs(26) == {(1, 26), (2, 13)}\n",
"assert get_factor_pairs(30) == {(1, 30), (2, 15), (3, 10), (5, 6)}\n",
"assert get_factor_pairs(40) == {(1, 40), (2, 20), (4, 10), (5, 8)}\n",
"assert get_factor_pairs(42) == {(1, 42), (2, 21), (3, 14), (6, 7)}\n",
"assert get_factor_pairs(66) == {(1, 66), (2, 33), (3, 22), (6, 11)}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding factors of a number\n",
"\n",
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-of-a-number).\n",
"\n",
"**Write a program that finds all the factors of a number $n$.**\n",
"\n",
"- Assume that $n$ is a positive integer greater than or equal to 1 ($n \\in \\mathbb{Z}^+$).\n",
"\n",
"For example for $n = 16$, the output of `get_factors(16)` may be:\n",
"\n",
"```\n",
"{1, 2, 4, 8, 16}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"def get_factors(n: int) -> set:\n",
" factors: set = set()\n",
" for i in range(1, n + 1):\n",
" dividend: int = n\n",
" divisor: int = i\n",
" quotient: int = int(n / i)\n",
" remainder: int = n % i\n",
" if remainder == 0:\n",
" factors.add(quotient)\n",
" return factors\n",
"\n",
"assert get_factors(16) == {1, 2, 4, 8, 16}\n",
"assert get_factors(120) == {1, 2, 3, 4, 5, 6, 8, 10, 12, 15, 20, 24, 30, 40, 60, 120}"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Finding factors and multiples\n",
"\n",
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-factors-mult/v/finding-factors-and-multiples)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Write a program that checks if a number $x$ is a factor of $y$.**\n",
"\n",
"- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n",
"\n",
"For example, for $x = 2$ and $y = 10$, the output of `is_factor(2, 10)` may be:\n",
"\n",
"```\n",
"True\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def is_factor(x: int, y: int) -> bool:\n",
" return y % x == 0\n",
"\n",
"assert is_factor(2, 10) == True\n",
"assert is_factor(3, 10) == False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Write a program that checks if a number $x$ is a multiple of $y$.**\n",
"\n",
"- Assume that $x$ and $y$ are positive integers greater than or equal to 1 ($x, y \\in \\mathbb{Z}^+$).\n",
"\n",
"For example, for $x = 20$ and $y = 3$, the output of `is_multiple(20, 3)` may be:\n",
"\n",
"```\n",
"True\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"def is_multiple(x: int, y: int) -> bool:\n",
" return x % y == 0\n",
"\n",
"assert is_multiple(10, 2) == True\n",
"assert is_multiple(10, 3) == False"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Write a program that finds all multiples of a positive integer `n` in the range `[n, end]`.**\n",
"\n",
"- Assume that `n` and `end` are positive integers greater than or equal to 1.\n",
"\n",
"For example for `n = 3` and `end = 20`, the output of `get_multiples_in_range(3, 20)` may be:\n",
"\n",
"```\n",
"{3, 6, 9, 12, 15, 18}\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"def get_multiples_in_range(n: int, end: int) -> set:\n",
" multiples = set()\n",
" for num in range(n, end + 1, n):\n",
" multiples.add(num)\n",
" return multiples\n",
"\n",
"assert get_multiples_in_range(3, 20) == {3, 6, 9, 12, 15, 18}\n",
"assert get_multiples_in_range(5, 50) == {5, 10, 15, 20, 25, 30, 35, 40, 45, 50}"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "venv",
"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.10.6"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}