mirror of
https://github.com/rzmk/ladderz.git
synced 2025-12-19 06:59:25 +00:00
feat: add dsa, update get_multiples_in_range, add rust notebook
This commit is contained in:
parent
f135bad4ad
commit
6a3f4cc7e4
10 changed files with 287 additions and 98 deletions
|
|
@ -1,392 +0,0 @@
|
|||
{
|
||||
"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": [
|
||||
"## 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": 44,
|
||||
"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": 45,
|
||||
"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": 46,
|
||||
"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": 47,
|
||||
"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": 48,
|
||||
"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}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## Prime and composite numbers\n",
|
||||
"\n",
|
||||
"> [Link to lesson](https://www.khanacademy.org/math/pre-algebra/pre-algebra-factors-multiples/pre-algebra-prime-numbers/v/prime-numbers)."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that determines whether a positive integer `n` is prime.**\n",
|
||||
"\n",
|
||||
"For example for `n = 7`, the output of `is_prime(7)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"True\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 49,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_prime(n: int) -> bool:\n",
|
||||
" if n == 1: return False\n",
|
||||
" for num in range(2, n):\n",
|
||||
" if n % num == 0:\n",
|
||||
" return False\n",
|
||||
" return True\n",
|
||||
"\n",
|
||||
"assert is_prime(1) == False\n",
|
||||
"assert is_prime(2) == True\n",
|
||||
"assert is_prime(3) == True\n",
|
||||
"assert is_prime(4) == False\n",
|
||||
"assert is_prime(7) == True\n",
|
||||
"assert is_prime(24) == False\n",
|
||||
"assert is_prime(59) == True\n",
|
||||
"assert is_prime(72) == False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that determines whether a positive integer `n` is composite.**\n",
|
||||
"\n",
|
||||
"For example for `n = 7`, the output of `is_composite(7)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"False\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 50,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def is_composite(n: int) -> bool:\n",
|
||||
" for num in range(1, n):\n",
|
||||
" if n % num == 0 and num != 1 and num != n:\n",
|
||||
" return True\n",
|
||||
" return False\n",
|
||||
"\n",
|
||||
"assert is_composite(1) == False\n",
|
||||
"assert is_composite(2) == False\n",
|
||||
"assert is_composite(3) == False\n",
|
||||
"assert is_composite(4) == True\n",
|
||||
"assert is_composite(7) == False\n",
|
||||
"assert is_composite(24) == True\n",
|
||||
"assert is_composite(59) == False\n",
|
||||
"assert is_composite(72) == True"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that finds all prime numbers in the range `[start, end]` within the natural numbers.**\n",
|
||||
"\n",
|
||||
"- Assume that `start` and `end` are positive integers greater than or equal to 1.\n",
|
||||
"\n",
|
||||
"For example for `start = 1` and `end = 20`, the output of `get_primes_in_range(1, 20)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"{2, 3, 5, 7, 11, 13, 17, 19}\n",
|
||||
"```"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 51,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_primes_in_range(start: int, end: int) -> set:\n",
|
||||
" primes: set = set()\n",
|
||||
" for num in range(start, end + 1):\n",
|
||||
" if is_prime(num):\n",
|
||||
" primes.add(num)\n",
|
||||
" return primes\n",
|
||||
"\n",
|
||||
"assert get_primes_in_range(1, 1) == set()\n",
|
||||
"assert get_primes_in_range(1, 2) == {2}\n",
|
||||
"assert get_primes_in_range(1, 20) == {2, 3, 5, 7, 11, 13, 17, 19}\n",
|
||||
"assert get_primes_in_range(3, 20) == {3, 5, 7, 11, 13, 17, 19}\n",
|
||||
"assert get_primes_in_range(1, 50) == {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47}\n",
|
||||
"assert get_primes_in_range(32, 85) == {37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83}"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**Write a program that determines the prime factorization of a positive integer `n`.**\n",
|
||||
"\n",
|
||||
"For example for `n = 12`, the output of `get_prime_factorization(12)` may be:\n",
|
||||
"\n",
|
||||
"```\n",
|
||||
"{2: 2, 3: 1}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"This means the prime factorization of 12 is $2^2 \\times 3^1$, where the keys are the prime factors and the values are the exponents."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 52,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def get_prime_factorization(n: int) -> dict:\n",
|
||||
" if n == 1: return {}\n",
|
||||
" prime_factors: dict = {}\n",
|
||||
" primes_of_n: set = get_primes_in_range(1, n)\n",
|
||||
" current_num: int = n\n",
|
||||
" for prime in primes_of_n:\n",
|
||||
" while current_num % prime == 0:\n",
|
||||
" current_num //= prime\n",
|
||||
" if prime not in prime_factors:\n",
|
||||
" prime_factors[prime] = 1\n",
|
||||
" else:\n",
|
||||
" prime_factors[prime] += 1\n",
|
||||
" if current_num in primes_of_n:\n",
|
||||
" if current_num not in prime_factors:\n",
|
||||
" prime_factors[current_num] = 1\n",
|
||||
" else:\n",
|
||||
" prime_factors[current_num] += 1\n",
|
||||
" break\n",
|
||||
" elif current_num == 1:\n",
|
||||
" break\n",
|
||||
" return prime_factors\n",
|
||||
"\n",
|
||||
"assert get_prime_factorization(1) == {}\n",
|
||||
"assert get_prime_factorization(2) == {2: 1}\n",
|
||||
"assert get_prime_factorization(3) == {3: 1}\n",
|
||||
"assert get_prime_factorization(4) == {2: 2}\n",
|
||||
"assert get_prime_factorization(5) == {5: 1}\n",
|
||||
"assert get_prime_factorization(6) == {2: 1, 3: 1}\n",
|
||||
"assert get_prime_factorization(10) == {2: 1, 5: 1}\n",
|
||||
"assert get_prime_factorization(12) == {2: 2, 3: 1}\n",
|
||||
"assert get_prime_factorization(13) == {13: 1}\n",
|
||||
"assert get_prime_factorization(20) == {2: 2, 5: 1}\n",
|
||||
"assert get_prime_factorization(75) == {3: 1, 5: 2}\n",
|
||||
"assert get_prime_factorization(750) == {2: 1, 3: 1, 5: 3}"
|
||||
]
|
||||
}
|
||||
],
|
||||
"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
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue