Skip to content

Commit

Permalink
updated example file'
Browse files Browse the repository at this point in the history
vrudhgarg committed Jan 25, 2025
1 parent c0354c3 commit daa1301
Showing 2 changed files with 210 additions and 176 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -4,4 +4,5 @@ tests/__pycache__/
.coverage
.ipynb_checkpoints/
tests/.ipynb_checkpoints/

build/
jupyter_execute/
383 changes: 208 additions & 175 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
@@ -4,14 +4,24 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example usage\n",
"# Number Theory Package Documentation\n",
"\n",
"To use `num_theory` in a project:"
"Welcome to the documentation for the `num_theory` package! This package provides essential number theory functions designed for educational and analytical purposes. From prime factorization to generating arithmetic progressions, the `num_theory` package is a versatile tool for students, researchers, and enthusiasts alike.\n",
"\n",
"## Installation\n",
"\n",
"To begin using the `num_theory` package, you can install it via pip:\n",
"\n",
"```bash\n",
"pip install num_theory\n",
"```\n",
"\n",
"Once installed, you can import the package in your Python project:\n"
]
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -32,70 +42,128 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Prime Factorization Package Documentation\n",
"## Prime Factorization\n",
"\n",
"The `prime_factorization` function breaks down a number into its prime components, which is especially useful for understanding number properties, simplifying fractions, and studying cryptographic algorithms.\n",
"\n",
"### How It Works\n",
"\n",
"The function determines prime factors using the following steps:\n",
"- Start dividing the number by 2 (the smallest prime) and record how many times it divides evenly.\n",
"- Proceed to odd numbers (3, 5, etc.) and repeat the division process, recording the results.\n",
"- Stop when the divisor squared exceeds the number. If a remainder greater than 1 remains, it is also a prime factor.\n",
"\n",
"This notebook provides documentation for the `prime_factorization` function. The function computes the prime factorization of a given positive integer greater than 1."
"---\n",
"\n",
"### Example Usage\n",
"\n",
"Here are some examples to demonstrate the `prime_factorization` function:\n",
"\n",
"```python\n",
"from num_theory.prime_factorization import prime_factorization\n",
"\n",
"# Factorizing 28\n",
"print(prime_factorization(28)) \n",
"# Output: [(2, 2), (7, 1)] which is equal to (2^2 + 7^1 = 28)\n",
"\n",
"# Factorizing 100\n",
"print(prime_factorization(100)) \n",
"# Output: [(2, 2), (5, 2)] which is equal to (2^2 + 5^2 = 100)\n",
"```\n",
"\n",
"---\n",
"\n",
"### Real-Life Applications\n",
"\n",
"#### Application 1: Simplifying Fractions\n",
"A student learning fractions wants to simplify the fraction `28/35`. Using the `prime_factorization` function, they can find the prime factors of both the numerator and denominator, then divide by their greatest common divisor (GCD):\n"
]
},
{
"cell_type": "markdown",
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(4, 5)\n"
]
}
],
"source": [
"## Installation\n",
"\n",
"To use the `prime_factorization` function, include it in your Python project by copying the function into your script or importing it from your package/module.\n",
"from num_theory.prime_factorization import prime_factorization\n",
"\n",
"If this function is part of a package, install the package as follows:\n",
"def simplify_fraction(numerator, denominator):\n",
" num_factors = prime_factorization(numerator)\n",
" den_factors = prime_factorization(denominator)\n",
" gcd = 1\n",
" for factor, _ in num_factors:\n",
" if factor in dict(den_factors):\n",
" gcd *= factor\n",
" return numerator // gcd, denominator // gcd\n",
"\n",
"```bash\n",
"pip install num_theory\n",
"```"
"# Simplify 28/35\n",
"print(simplify_fraction(28, 35)) # Output: (4, 5)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!-- ## Function Overview\n",
"\n",
"### `prime_factorization(n)`\n",
"This function computes the prime factorization of a given integer `n` and returns the result as a list of tuples. Each tuple represents a prime factor and its corresponding power.\n",
"\n",
"### Parameters\n",
"- **`n`**: An integer to factorize. Must be a positive integer greater than 1.\n",
"\n",
"### Returns\n",
"- **List of Tuples**: Each tuple contains a prime factor and its power.\n",
"\n",
"### Raises\n",
"- **`ValueError`**: If the input `n` is not a positive integer greater than 1.\n",
"\n",
"### Notes\n",
"- Uses trial division for factorization.\n",
"- For large `n`, optimized algorithms like Pollard's Rho or the Quadratic Sieve are recommended. -->"
"#### Application 2: Finding Multiples in a Classroom Activity\n",
"A teacher wants to create a fun activity where students identify the prime factors of numbers between 2 and 10. The `prime_factorization` function can generate the factors for any number:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"2: [(2, 1)]\n",
"3: [(3, 1)]\n",
"4: [(2, 2)]\n",
"5: [(5, 1)]\n",
"6: [(2, 1), (3, 1)]\n",
"7: [(7, 1)]\n",
"8: [(2, 3)]\n",
"9: [(3, 2)]\n",
"10: [(2, 1), (5, 1)]\n"
]
}
],
"source": [
"# Prime factors for classroom activity\n",
"for num in range(2, 11):\n",
" print(f\"{num}: {prime_factorization(num)}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Real World Application: Analyzing Prime Factor Distributions in Cryptography\n",
"This activity helps students visually understand how numbers break down into prime components.\n",
"\n",
"A cybersecurity researcher is investigating the factorization of large integers as part of a study on cryptographic algorithms, specifically RSA encryption. The security of RSA relies on the difficulty of factoring the product of two large prime numbers. To demonstrate this concept, the researcher uses the prime_factorization function to: \n",
" 1.\tFactorize smaller integers to explain the concept of prime factorization to a team of non-technical stakeholders. \n",
" 2.\tTest and validate a set of randomly generated composite numbers to confirm their factorization as part of the cryptographic key analysis process. \n",
" 3.\tAnalyze the distribution of prime factors in datasets of composite numbers to study patterns or anomalies that may reveal vulnerabilities in certain key-generation techniques. \n",
"#### Application 3: Cryptographic Key Analysis\n",
"A cybersecurity researcher is investigating the factorization of large integers as part of a study on cryptographic algorithms, specifically RSA encryption. The security of RSA relies on the difficulty of factoring the product of two large prime numbers. To demonstrate this concept, the researcher uses the `prime_factorization` function to:\n",
"\n",
"This function serves as an educational tool and a basic analysis tool for smaller numbers, helping the researcher communicate and validate fundamental cryptographic concepts. \n",
"1. Factorize smaller integers to explain the concept of prime factorization to a team of non-technical stakeholders.\n",
"2. Test and validate a set of randomly generated composite numbers to confirm their factorization as part of the cryptographic key analysis process.\n",
"3. Analyze the distribution of prime factors in datasets of composite numbers to study patterns or anomalies that may reveal vulnerabilities in certain key-generation techniques.\n",
"\n",
"## Examples\n",
"This function serves as an educational tool and a basic analysis tool for smaller numbers, helping the researcher communicate and validate fundamental cryptographic concepts.\n",
"\n",
"Below are examples demonstrating the use of the `prime_factorization` function."
"### Example Usage"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -121,13 +189,6 @@
"print(prime_factorization(13195)) # Output: [(5, 1), (7, 1), (13, 1), (29, 1)]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Implementation"
]
},
{
"cell_type": "markdown",
"metadata": {},
@@ -149,241 +210,213 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial: get_primes()\n",
"## Get Primes\n",
"\n",
"The \"get_primes()\" retrieves a list of all prime numbers under a given number. This list of prime numbers can aid you in solving Project Euler problems, creating hashing methods, as well as many other number theory and cryptography applications."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 1: Project Euler's prime numbers sum.\n",
"Project Euler's 10th problem states: \"Find the sum of all the primes below two million\". Using our \"get_primes()\" function trivializes this solution, as we can simply use Python's built-in \"sum()\" function with our function while passing 2,000,000 as the argument. Our function will retrieve the list of all primes under 2,000,000 and \"sum()\" will calculate thier sum to reach the answer."
"The `get_primes` function generates a list of all prime numbers below a given integer. This is ideal for applications like solving Project Euler problems, creating hashing methods, and studying prime number distributions.\n",
"\n",
"### Real-World Applications\n",
"\n",
"#### Application 1: Generating Prime-Based Security Codes\n",
"A small business wants to generate unique security codes using prime numbers for encrypting customer IDs. Using the `get_primes` function, they generate primes up to a certain number and select them as secure keys:\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"142913828922"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"[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]\n"
]
}
],
"source": [
"from num_theory.get_primes import get_primes \n",
"from num_theory.get_primes import get_primes\n",
"\n",
"sum(get_primes(2000000))"
"# Generate prime-based security codes\n",
"security_codes = get_primes(100)\n",
"print(security_codes) "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Example 2: Hashing\n",
"Let's say you want to create a simple hash function that hashes a given number into a number within the range of 0 and 255. You can use the modulo \"%\" operation alongside a sum of our \"get_primes()\" function's output to create a simple algorithm that achieves this. please note that this hashing function is too primitive for most practical uses and is only used here to provide an easy to understand example."
"### Application 2: Project Euler's prime numbers sum.\n",
"\n",
"Project Euler's 10th problem states: \"Find the sum of all the primes below two million.\" Using our `get_primes()` function simplifies this solution, as we can simply use Python's built-in `sum()` function along with our function, passing 2,000,000 as the argument. Our function will retrieve the list of all primes under 2,000,000, and `sum()` will calculate their sum to obtain the answer."
]
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"201\n",
"58\n",
"100\n"
"142913828922\n"
]
}
],
"source": [
"def simple_hash(num: int) -> int:\n",
" return sum(get_primes(num)) % 256\n",
"from num_theory.get_primes import get_primes\n",
"\n",
"print(simple_hash(2025))\n",
"print(simple_hash(1999))\n",
"print(simple_hash(23))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Additional notes\n",
"The get_primes() function uses a custom Sieve of Eratosthenes algorithm to retrieve the list of prime numbers. It does this by initializing a list of all number under n, then it iterates through all of them from the bottom to the top and assigns their multiples as non-primes. Once done, it leaves you with a list containing only prime numbers under n. This algorithm has a time complexity of O(n log(log(n))) and a space complexity of O(n)."
"# Sum of all primes below 2,000,000\n",
"print(sum(get_primes(2000000)))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Tutorial: arithmetic_progression()\n",
"### Application 3: Hashing with Primes\n",
"\n",
"Here we will demonstrate how to use the `arithmetic_progression` function to generate an arithmetic progression, compute the nth term, or calculate the sum of the first n terms."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"# Imports\n",
"from num_theory.arithmetic_progression import arithmetic_progression"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example 1: Saving for a Vacation\n",
"You start saving $100 in the first month, increasing the amount by $10 every month. We'll calculate:\n",
"- Total savings after 12 months\n",
"- Amount saved in the 12th month\n",
"- The savings progression"
"Suppose you want to create a simple hash function that converts a given number into a value within the range of 0 to 255. You can achieve this by using the modulo (`%`) operation along with the sum of the output from our `get_primes()` function to create a straightforward algorithm. However, please note that this hashing function is extremely basic and not suitable for most practical applications. It is used here solely as an easy-to-understand example."
]
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1860.0\n"
"201\n",
"58\n"
]
}
],
"source": [
"# Total amount saved in 12 months\n",
"total_savings = arithmetic_progression(a=100, d=10, n=12, compute_sum=True)\n",
"print(total_savings) # Output: 1860.0"
"def simple_hash(num: int) -> int:\n",
" from num_theory.get_primes import get_primes\n",
" return sum(get_primes(num)) % 256\n",
"\n",
"print(simple_hash(2025))\n",
"print(simple_hash(1999))"
]
},
{
"cell_type": "code",
"execution_count": 14,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"210\n"
]
}
],
"source": [
"# Deposit in the 12th month\n",
"twelfth_deposit = arithmetic_progression(a=100, d=10, n=12, nth_term=True)\n",
"print(twelfth_deposit) # Output: 210"
"## Additional notes\n",
"\n",
"The get_primes() function uses a custom Sieve of Eratosthenes algorithm to retrieve the list of prime numbers. It does this by initializing a list of all number under n, then it iterates through all of them from the bottom to the top and assigns their multiples as non-primes. Once done, it leaves you with a list containing only prime numbers under n. This algorithm has a time complexity of O(n log(log(n))) and a space complexity of O(n)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Arithmetic Progression\n",
"\n",
"The `arithmetic_progression` function calculates terms, sums, or sequences of arithmetic progressions. This can model savings plans, salary increments, and more.\n",
"\n",
"### Example 1: Saving for a Vacation\n",
"\n",
"You start saving $100 in the first month, increasing by $10 each month. Let’s calculate:\n",
"\n",
"1. Total savings after 12 months.\n",
"2. Amount saved in the 12th month.\n",
"3. The savings progression."
]
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Total savings after 12 months: $1,860.0\n",
"Amount saved in the 12th month: $210\n",
"[100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]\n"
]
}
],
"source": [
"# Sequence of monthly deposits\n",
"deposits = arithmetic_progression(a=100, d=10, n=12)\n",
"print(deposits) # Output: [100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]\n"
"from num_theory.arithmetic_progression import arithmetic_progression\n",
"\n",
"# Total savings after 12 months\n",
"print(f\"Total savings after 12 months: ${arithmetic_progression(a=100, d=10, n=12, compute_sum=True):,}\") \n",
"\n",
"# Amount saved in the 12th month\n",
"print(f\"Amount saved in the 12th month: ${arithmetic_progression(a=100, d=10, n=12, nth_term=True):,}\") \n",
"\n",
"# Savings progression\n",
"print(arithmetic_progression(a=100, d=10, n=12))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Example 2: Salary Increment\n",
"Imagine your starting salary is $50,000, and you get an annual increment of $3,000. You want to calculate:\n",
"### Example 2: Salary Increment\n",
"\n",
"1. Your salary in the 5th year.\n",
"2. The total earnings over 5 years.\n",
"3. Your salary progression over the 5 years."
"Your starting salary is $50,000, with an annual increment of $3,000. Let’s calculate:\n",
"\n",
"1. Salary in the 5th year.\n",
"2. Total earnings over 5 years.\n",
"3. Salary progression over 5 years."
]
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"62000\n"
"Salary in the 5th year: $62,000\n",
"Total earnings over 5 years: $280,000.0\n",
"[50000, 53000, 56000, 59000, 62000]\n"
]
}
],
"source": [
"# Salary in the 5th year\n",
"fifth_year_salary = arithmetic_progression(a=50000, d=3000, n=5, nth_term=True)\n",
"print(fifth_year_salary) # Output: 62000"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"280000.0\n"
]
}
],
"source": [
"print(f\"Salary in the 5th year: ${arithmetic_progression(a=50000, d=3000, n=5, nth_term=True):,}\") \n",
"\n",
"# Total earnings over 5 years\n",
"total_earnings = arithmetic_progression(a=50000, d=3000, n=5, compute_sum=True)\n",
"print(total_earnings) # Output: 275000.0"
"print(f\"Total earnings over 5 years: ${arithmetic_progression(a=50000, d=3000, n=5, compute_sum=True):,}\")\n",
"\n",
"# Salary progression over 5 years\n",
"print(arithmetic_progression(a=50000, d=3000, n=5)) # Output: [50000, 53000, ..., 62000]"
]
},
{
"cell_type": "code",
"execution_count": 18,
"cell_type": "markdown",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[50000, 53000, 56000, 59000, 62000]\n"
]
}
],
"source": [
"# Salary progression over 5 years\n",
"salary_progression = arithmetic_progression(a=50000, d=3000, n=5)\n",
"print(salary_progression) # Output: [50000, 53000, 56000, 59000, 62000]"
"---\n",
"\n",
"## Summary\n",
"\n",
"The `num_theory` package offers practical and educational tools for number theory applications. From analyzing cryptographic keys to modeling real-world scenarios like savings plans, this package provides:\n",
"\n",
"- **Prime factorization** for understanding number properties.\n",
"- **Prime generation** for problem-solving and hashing.\n",
"- **Prime checking** for quick validation of numbers.\n",
"- **Arithmetic progressions** for financial and mathematical modeling.\n",
"\n",
"Explore the power of number theory with `num_theory`!"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "test_env",
"language": "python",
"name": "python3"
},
@@ -397,7 +430,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
"version": "3.13.1"
}
},
"nbformat": 4,

0 comments on commit daa1301

Please sign in to comment.