-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Thamerd
committed
Jan 22, 2025
1 parent
7a9e7b7
commit 75d0f1b
Showing
1 changed file
with
109 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [ | ||
"# Tutorial: 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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"data": { | ||
"text/plain": [ | ||
"142913828922" | ||
] | ||
}, | ||
"execution_count": 26, | ||
"metadata": {}, | ||
"output_type": "execute_result" | ||
} | ||
], | ||
"source": [ | ||
"from num_theory.get_primes import get_primes \n", | ||
"\n", | ||
"sum(get_primes(2000000))" | ||
] | ||
}, | ||
{ | ||
"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." | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 29, | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"201\n", | ||
"58\n", | ||
"100\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"def simple_hash(num: int) -> int:\n", | ||
" return sum(get_primes(num)) % 256\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)." | ||
] | ||
}, | ||
{ | ||
"cell_type": "markdown", | ||
"metadata": {}, | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "524", | ||
"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.1" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 2 | ||
} |