{"id":26287243,"url":"https://github.com/optimized-brain/aks-algorithm-optimized-with-fast-fourier-transform","last_synced_at":"2025-09-22T08:51:28.081Z","repository":{"id":274417258,"uuid":"912796338","full_name":"Optimized-Brain/AKS-Algorithm-Optimized-with-Fast-Fourier-Transform","owner":"Optimized-Brain","description":"Optimized AKS Primality Test with Fast Fourier Transform (FFT) for enhanced performance in large number primality checking. This implementation improves the efficiency of the AKS algorithm using FFT for polynomial multiplication, offering significant speedup.","archived":false,"fork":false,"pushed_at":"2025-03-05T11:21:13.000Z","size":76,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-05T12:26:02.374Z","etag":null,"topics":["aks-algorithm","algorithm-complexity","fast-fourier-transform","polynomial-multiplication","primality-testing"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Optimized-Brain.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-01-06T12:13:44.000Z","updated_at":"2025-03-05T11:21:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"6ef9c60a-0acd-49d2-8f93-dfbb797ea02e","html_url":"https://github.com/Optimized-Brain/AKS-Algorithm-Optimized-with-Fast-Fourier-Transform","commit_stats":null,"previous_names":["optimized-brain/aks-algorithm-optimized-with-fast-fourier-transform"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Optimized-Brain%2FAKS-Algorithm-Optimized-with-Fast-Fourier-Transform","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Optimized-Brain%2FAKS-Algorithm-Optimized-with-Fast-Fourier-Transform/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Optimized-Brain%2FAKS-Algorithm-Optimized-with-Fast-Fourier-Transform/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Optimized-Brain%2FAKS-Algorithm-Optimized-with-Fast-Fourier-Transform/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Optimized-Brain","download_url":"https://codeload.github.com/Optimized-Brain/AKS-Algorithm-Optimized-with-Fast-Fourier-Transform/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243646698,"owners_count":20324587,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aks-algorithm","algorithm-complexity","fast-fourier-transform","polynomial-multiplication","primality-testing"],"created_at":"2025-03-14T21:19:15.654Z","updated_at":"2025-09-22T08:51:23.047Z","avatar_url":"https://github.com/Optimized-Brain.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# AKS Algorithm Optimized with Fast Fourier Transform (FFT)\n\nThis repository implements the **AKS Primality Test** optimized using **Fast Fourier Transform (FFT)** to improve polynomial multiplication performance. The AKS Primality Test is a deterministic primality test that determines whether a given number is prime. This implementation leverages FFT to speed up the polynomial multiplication step, making the algorithm more efficient for large numbers.\n\n## Table of Contents\n- [Introduction](#introduction)\n- [Features](#features)\n- [Installation](#installation)\n- [Usage](#usage)\n- [Detailed Explanation](#detailed-explanation)\n  - [What is the AKS Primality Test?](#what-is-the-aks-primality-test)\n  - [Optimization with FFT](#optimization-with-fft)\n\n## Introduction\n\nThe **AKS Primality Test** is a famous primality test developed by Agrawal, Kayal, and Saxena in 2002. It is notable for being a deterministic polynomial-time algorithm to determine if a number is prime. This implementation uses the **Fast Fourier Transform (FFT)** to optimize polynomial multiplication, which is a crucial step in the AKS algorithm.\n\nBy using FFT for polynomial multiplication, we reduce the time complexity involved in this operation from $\\( O(n^2) \\)$ to $\\( O(n \\log n) \\)$, which helps the AKS test scale to larger numbers more efficiently.\n\n\n## Features\n\n- **Primality Testing**: Checks if a given number is prime using the AKS algorithm.\n- **FFT Optimization**: Polynomial multiplication is optimized using FFT for faster computation.\n- **Benchmarking**: Includes a benchmarking tool to compare the performance of the normal AKS algorithm versus the FFT-optimized version.\n- **Python Implementation**: Written in Python for easy understanding and modification.\n\n## Installation\n\nTo run this project on your local machine, follow these steps:\n\n\n1. **Clone the Repository**  \n   Clone the repository using Git and navigate to the project directory:  \n   ```bash\n   git clone https://github.com/Optimized-Brain/AKS-Algorithm-Optimized-with-Fast-Fourier-Transform.git\n   cd AKS-Algorithm-Optimized-with-Fast-Fourier-Transform\n2. **Create a Virtual Environment** (optional but recommended)\n\nCreate and activate a virtual environment to manage dependencies:\n```bash\npython -m venv venv\nsource venv/bin/activate     # On Linux/Mac\nvenv\\Scripts\\activate        # On Windows\n```\n```\n3. **Verify Installation**\nRun the project to ensure it is set up correctly:\n```bash\npython main.py\n```\n\nThis will execute the benchmarking script and display a comparison of the normal AKS algorithm and the FFT-optimized version.\n\n\n### Prerequisites\nMake sure you have **Python 3.x** installed on your machine. You can download Python from [here](https://www.python.org/downloads/).\n\n### Install Required Libraries\n\nClone the repository:\n\n```\ngit clone https://github.com/Optimized-Brain/AKS-Algorithm-Optimized-with-Fast-Fourier-Transform.git\ncd AKS-Algorithm-Optimized-with-Fast-Fourier-Transform\n\n```\n\n\n## Usage\n\nThis project provides an implementation of the AKS (Agrawal-Kayal-Saxena) Primality Test, both in its original form and optimized using Fast Fourier Transform (FFT). Below are the steps to use the project, including running benchmarks, testing primality of numbers, and customizing the input for performance evaluation.\n\n1. ### Running the Benchmark\n\nThe benchmarking script compares the performance of the normal AKS algorithm and the FFT-optimized version across various input sizes.\n\nTo run the benchmark, simply execute the `main.py` file:\n\n   ```\n   python main.py\n```\n\nThe script will display a table comparing the execution time of both implementations for different input sizes. Here's an example of the output:\n\n\n      Input |  Normal AKS Time (s) |     FFT AKS Time (s) |      Improvement (%)\n----------------------------------------------------------------------------\n        100 |             0.012345 |             0.005678 |              54.05%\n        200 |             0.045678 |             0.015678 |              65.69%\n\nThis output allows you to see the time improvement achieved by using FFT over the traditional AKS algorithm.\n\n2. **Customizing the Input Sizes for Benchmarking**\n\nThe benchmarking input sizes are specified in the `benchmark_aks()` function, located in the `src/benchmarks.py` file. By default, it tests against numbers like 1000, 2000, 5000, 10000 and 20000.\n\nTo modify the input sizes, you can change the values in the ns list. For example:\n\n```python\nns = [1000, 2000, 5000, 10000, 20000]  # Adjust the numbers to test\n```\nAfter making changes, save the file and rerun the benchmark using the following command:\n```bash\npython main.py\n```\n\n### Testing Primality of a Single Number\n\nTo test whether a given number is prime using the AKS primality test (either the standard version or the optimized FFT version), you can use the following code.\n\n1. **Test Primality with Normal AKS Algorithm**\n\nYou can test the primality of a number using the standard AKS primality test by importing and calling the normal_aks_primality_test() function from the src/aks.py file.\n\nExample usage:\n```python from src.aks import normal_aks_primality_test\n\nnumber = 101\n\n# Test primality using the normal AKS algorithm\nis_prime = normal_aks_primality_test(number)\n\nprint(f\"{number} is {'prime' if is_prime else 'not prime'}.\")\n```\n\n**Sample Output:**\n```\n101 is prime.\n```\n2. **Test Primality with FFT-Optimized AKS Algorithm**\n\nYou can also test primality using the FFT-optimized AKS algorithm by calling the fft_aks_primality_test() function in a similar manner.\n\nExample usage:\n```python\nfrom src.aks import fft_aks_primality_test\n\nnumber = 101\n\n# Test primality using the FFT-optimized AKS algorithm\nis_prime_fft = fft_aks_primality_test(number)\n\nprint(f\"{number} is {'prime' if is_prime_fft else 'not prime'}.\")\n```\n\n**Sample Output:**\n```\n101 is prime.\n```\n### Extending the Project ###\n\nThis project is designed to be modular and extensible. You can easily add new features or optimize the existing ones by modifying the following files:\n\n`src/fft.py`: This performs polynomial multiplication using both naive and FFT-based approach, which can also be used to implement any other algorithm for polynomial multiplication. \n\n`src/aks.py`: This contains implementation of standard AKS algorithm as well as FFT-based implementation of AKS algorithm, which can be used for other primality tests. \n\n`src/benchmarks.py`: Customize the benchmarking script by changing the input sizes or adding new profiling configurations.\n\n### Example Use Cases ###\n* **Simple Primality Testing**: Quickly check if a number is prime using either the normal AKS algorithm or the FFT-optimized version.\n* **Performance Comparison**: Use the benchmarking tools to compare the runtime of the normal and FFT-based AKS algorithms.\n* **Optimization**: Extend the project by implementing advanced optimizations for AKS or integrating other primality testing algorithms.\n\n## Detailed Explanation\n### What is the AKS Primality Test?\n\nThe AKS Primality Test is a deterministic test that checks whether a given number $\\( n \\)$ is prime. Before this test, primality tests like the Miller-Rabin test were probabilistic, which meant they might not always provide the correct answer. The AKS test, however, is deterministic and runs in polynomial time.\n\nThe AKS test consists of several steps:\n\n**Perfect Power Check**  \n   Verify if $\\( n \\)$ is a perfect power. If $\\( n = a^b \\)$ for integers $\\( a \u003e 1 \\)$ and $\\( b \u003e 1 \\)$, then $\\( n \\)$ is **not prime**.\n\n2. **Smallest \\( r \\) Calculation**  \n   Find the smallest integer \\( r \\) such that the order of \\( n \\mod r \\), denoted as \\( \\text{ord}_r(n) \\), satisfies:  \n   $\\[\n   \\text{ord}_r(n) \u003e \\log^2(n)\n   \\]$\n\n3. **GCD Check**  \n   Check if $\\( \\gcd(a, n) = 1 \\)$ for all integers $\\( a \\)$ where $\\( 1 \\leq a \\leq r \\)$. If $\\( \\gcd(a, n) \u003e 1 \\)$ for any $\\( a \\)$, $\\( n \\)$ is **not prime**.\n\n4. **Polynomial Congruence**  \n   Verify the following polynomial congruence:  \n   $\\[\n   (x + a)^n \\equiv x^n + a \\pmod{n, x^r - 1} \\quad \\text{for all } 1 \\leq a \\leq \\lfloor \\sqrt{\\phi(r)} \\log(n) \\rfloor\n   \\] $ \n   If the congruence holds, $\\( n \\)$ is **prime**; otherwise, it is **composite**.\nThe main computational challenge in the AKS test comes from the polynomial multiplication used in the congruence checks. This is where the **FFT** optimization comes in.\n\n### Optimization with FFT\n\n\nIn this project, we optimize the AKS (Agrawal-Kayal-Saxena) primality test by incorporating the Fast Fourier Transform (FFT) to speed up the polynomial multiplication steps. The standard AKS algorithm involves polynomial arithmetic, which is the most computationally expensive part of the algorithm. By using FFT, we can perform these polynomial multiplications much faster, thus improving the overall performance of the primality test.\n\n#### The Role of FFT in AKS\n\nThe main computational cost of the AKS primality test comes from polynomial multiplication, which is involved in checking certain conditions related to the order of elements and congruence relations. In the standard AKS algorithm, polynomial multiplication is performed naively, which has a time complexity of $\\( O(n^2) \\)$, where $\\( n \\)$ is the degree of the polynomials.\n\nFFT, on the other hand, provides a way to multiply polynomials in $\\( O(n \\log n) \\)$ time. This drastic improvement in time complexity is due to the fact that FFT allows us to evaluate polynomials at certain points in a highly efficient manner, enabling us to perform pointwise multiplication and inverse FFT to obtain the product of two polynomials.\n\n#### Polynomial Multiplication Using FFT\n\nLet $\\( A(x) \\)$ and $\\( B(x) \\)$ be two polynomials of degree $\\( n \\)$:\n\n$\\[\nA(x) = a_0 + a_1 x + a_2 x^2 + \\cdots + a_{n-1} x^{n-1}\n\\]\n\\[\nB(x) = b_0 + b_1 x + b_2 x^2 + \\cdots + b_{n-1} x^{n-1}\n\\]$\n\nThe naive polynomial multiplication requires $\\( O(n^2) \\)$ operations, where we compute each coefficient of the resulting polynomial by multiplying terms of $\\( A(x) \\)$ and $\\( B(x) \\)$. However, with FFT, we transform the polynomials to the frequency domain, multiply the corresponding coefficients, and then transform them back to the coefficient domain.\n\n#### Steps for Polynomial Multiplication with FFT:\n\n1. **Transform the polynomials**: Apply the FFT to both polynomials $\\( A(x) \\)$ and $\\( B(x) \\)$, which converts the polynomials into their evaluations at specific points (the roots of unity).\n   \n   $\\[\n   A(\\omega^k) \\quad \\text{and} \\quad B(\\omega^k) \\quad \\text{for} \\quad k = 0, 1, 2, \\dots, n-1\n   \\]$\n   where $\\( \\omega \\)$ is a primitive $\\( n \\)$-th root of unity.\n\n2. **Pointwise multiplication**: Multiply the transformed values pointwise:\n\n   $\\[\n   C(\\omega^k) = A(\\omega^k) \\cdot B(\\omega^k)\n   \\]$\n\n3. **Inverse Transform**: Apply the inverse FFT to $\\( C(\\omega^k) \\)$ to obtain the coefficients of the resulting polynomial $\\( C(x) \\)$.\n\n4. **Recover the result**: The coefficients of the resulting polynomial are the values obtained from the inverse FFT. We round them to integers to remove any imaginary components introduced by the numerical methods in FFT.\n\n#### Equations Involved\n\nGiven the polynomials $\\( A(x) \\)$ and $\\( B(x) \\)$, their FFT-based multiplication involves the following equations:\n\n1. **FFT Transformation**:\n   \n   $\\[\n   A(\\omega^k) = \\sum_{i=0}^{n-1} a_i \\omega^{ik}\n   \\]$\n   $\\[\n   B(\\omega^k) = \\sum_{i=0}^{n-1} b_i \\omega^{ik}\n   \\]$\n\n2. **Pointwise Multiplication**:\n   \n   $\\[\n   C(\\omega^k) = A(\\omega^k) \\cdot B(\\omega^k)\n   \\]$\n\n3. **Inverse FFT**:\n   \n   $\\[\n   C(x) = \\frac{1}{n} \\sum_{k=0}^{n-1} C(\\omega^k) \\omega^{-ik}\n   \\]$\n\nThe result $\\( C(x) \\)$ gives the product of the polynomials $\\( A(x) \\)$ and $\\( B(x) \\)$.\n\n#### Time Complexity\n\n- **Naive Polynomial Multiplication**: The naive approach to polynomial multiplication has a time complexity of $\\( O(n^2) \\)$, where $\\( n \\)$ is the degree of the resulting polynomial.\n  \n- **FFT-based Polynomial Multiplication**: By using FFT, we reduce the time complexity to $\\( O(n \\log n) \\)$, where $\\( n \\)$ is the number of coefficients. This provides a significant speedup, especially for large polynomials.\n\n#### Benefit in AKS Primality Test\n\nBy replacing the naive polynomial multiplication with FFT-based multiplication, the overall time complexity of the AKS primality test is reduced, especially for larger numbers. FFT reduces the time taken for the polynomial multiplications that occur during the polynomial congruence check, allowing the test to scale better with larger input numbers.\n\n### Summary\n\n- **Naive polynomial multiplication**: $\\( O(n^2) \\)$\n- **FFT-based polynomial multiplication**: $\\( O(n \\log n) \\)$\n- **Improvement in AKS**: FFT optimization speeds up polynomial multiplication, improving the overall runtime of the AKS primality test for large inputs.\n\nBy leveraging FFT, this project significantly enhances the performance of the AKS primality test, enabling it to handle larger numbers efficiently, making it more practical for use in real-world applications.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foptimized-brain%2Faks-algorithm-optimized-with-fast-fourier-transform","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Foptimized-brain%2Faks-algorithm-optimized-with-fast-fourier-transform","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Foptimized-brain%2Faks-algorithm-optimized-with-fast-fourier-transform/lists"}