Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add is_prime package level documentation #55

Merged
merged 1 commit into from
Jan 26, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
141 changes: 132 additions & 9 deletions docs/example.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 3,
"metadata": {},
"outputs": [
{
Expand All @@ -48,7 +48,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -107,7 +107,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 5,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -144,7 +144,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 6,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -189,7 +189,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 7,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -270,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 8,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -300,7 +300,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -417,7 +417,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 11,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -458,7 +458,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 12,
"metadata": {},
"outputs": [
{
Expand Down Expand Up @@ -500,6 +500,129 @@
" - Additional options to calculate exponential growth and other non linear progressions (e.g., geometric, harmonic, etc.)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## **Is Prime**\n",
"\n",
"The `is_prime` package provides a simple, efficient, and reliable way to determine whether a given integer is prime. This lightweight utility is perfect for educational purposes, mathematical explorations, and real-world applications requiring prime number checks.\n",
"\n",
"---\n",
"### Parameters\n",
"\n",
"**n : int** \n",
" The number to check for primality. Must be a positive integer greater than 1. \n",
"\n",
"### Returns\n",
"\n",
"**bool** \n",
" `True` if the number is prime, otherwise `False`.\n",
"\n",
"### Raises\n",
"\n",
"**ValueError** \n",
" If the input is not a positive integer greater than 1.\n",
"\n",
"---\n",
"\n",
"### Example Usage\n",
"```python\n",
"from num_theory.is_prime import is_prime\n",
"\n",
"print(is_prime(7)) # True\n",
"print(is_prime(10)) # False\n",
"```\n",
"\n",
"---\n",
"### Application 1: Prime Pattern Finder\n",
"\n",
"By filtering prime numbers that satisfy the pattern $p = 6k \\pm 1$, this demonstrates how mathematical properties can be utilized to further refine prime numbers."
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Special primes (6k ± 1) between 2 and 200: [5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199]\n"
]
}
],
"source": [
"from num_theory.is_prime import is_prime\n",
"\n",
"# Find primes matching the pattern\n",
"def find_special_primes(start, end):\n",
" primes = [n for n in range(start, end + 1) if is_prime(n)]\n",
" special_primes = [p for p in primes if (p - 1) % 6 == 0 or (p + 1) % 6 == 0]\n",
" return special_primes\n",
"\n",
"# Find special primes in the range\n",
"special_primes = find_special_primes(2, 200)\n",
"print(f\"Special primes (6k ± 1) between 2 and 200: {special_primes}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Application 2: Generate RSA Keys\n",
"\n",
"In encryption applications, generating an RSA key pair (public and private keys) requires two large prime numbers and modulus calculations. This code uses `is_prime` to verify the generated prime numbers and constructs the RSA public and private keys, showcasing a practical application in a real-world encryption system."
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"RSA Keys:\n",
"Public Key: (52931, 126169)\n",
"Private Key: (44011, 126169)\n"
]
}
],
"source": [
"import random\n",
"from math import gcd\n",
"\n",
"# Generate large primes\n",
"def generate_large_prime(start, end):\n",
" while True:\n",
" n = random.randint(start, end)\n",
" if is_prime(n):\n",
" return n\n",
"\n",
"# Generate RSA keys\n",
"def generate_rsa_keys():\n",
" p = generate_large_prime(100, 999)\n",
" q = generate_large_prime(100, 999)\n",
" n = p * q\n",
" phi = (p - 1) * (q - 1)\n",
"\n",
" # Choose e such that 1 < e < phi and gcd(e, phi) = 1\n",
" e = random.choice([x for x in range(2, phi) if gcd(x, phi) == 1])\n",
"\n",
" # Calculate d such that (d * e) % phi = 1\n",
" d = pow(e, -1, phi)\n",
" return {\"public_key\": (e, n), \"private_key\": (d, n)}\n",
"\n",
"# Generate and display RSA keys\n",
"keys = generate_rsa_keys()\n",
"print(\"RSA Keys:\")\n",
"print(f\"Public Key: {keys['public_key']}\")\n",
"print(f\"Private Key: {keys['private_key']}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down
Loading