https://github.com/avivbrook/modular-nested-exponentiation
An algorithm that computes modular nested exponentiation efficiently.
https://github.com/avivbrook/modular-nested-exponentiation
algorithm computational-number-theory jupyter-notebook modular-arithmetic modular-exponentiation number-theory positive-integers pypi python
Last synced: 8 months ago
JSON representation
An algorithm that computes modular nested exponentiation efficiently.
- Host: GitHub
- URL: https://github.com/avivbrook/modular-nested-exponentiation
- Owner: avivbrook
- License: gpl-3.0
- Created: 2021-03-17T09:32:38.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2021-04-05T13:08:32.000Z (over 4 years ago)
- Last Synced: 2025-03-15T21:39:49.798Z (9 months ago)
- Topics: algorithm, computational-number-theory, jupyter-notebook, modular-arithmetic, modular-exponentiation, number-theory, positive-integers, pypi, python
- Language: Python
- Homepage:
- Size: 931 KB
- Stars: 1
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Modular nested exponentiation
An algorithm that computes modular nested exponentiation efficiently.
[](https://choosealicense.com/licenses/gpl-3.0/)
[](https://github.com/avivbrook/modular-nested-exponentiation/)
[](https://codecov.io/gh/avivbrook/modular-nested-exponentiation)
[](https://pypi.org/project/mod-nest-exp/)
[](https://pypi.org/project/mod-nest-exp/#files)
[](https://pepy.tech/project/mod-nest-exp)
## 🚩 Table of Contents
- [Overview](#%EF%B8%8F-overview)
- [Prerequisites](#%EF%B8%8F-prerequisites)
- [Installation](#-installation)
- [Examples](#-examples)
## 🗺️ Overview
`mod-nest-exp` exports a Python function `mod_nest_exp` that takes as input an arbitrarily long sequence of positive integers `a₁, a₂, ..., aₙ` and a positive integer `m` and computes `a₁^(a₂^(···^aₙ)) mod m` efficiently (that is, without computing the value of the nested exponent).
To date, this problem was not solvable by computers in the general case.
## 🏳️ Prerequisites
`mod-nest-exp` requires Python v3.6+.
For best performance, install `gmpy2` and `sympy`:
```console
$ apt install libgmp-dev libmpfr-dev libmpc-dev # required for gmpy2
$ pip install gmpy2 sympy
```
The libraries offer more efficient alternatives to a number of functions used as subroutines in the core module.
## 🔧 Installation
The recommended installation method is from [PyPI](https://pypi.org/project/mod-nest-exp/):
```console
$ pip install mod-nest-exp
```
A development version can be installed from GitHub source using `setuptools`:
```console
$ git clone https://github.com/avivbrook/modular-nested-exponentiation.git
$ cd modular-nested-exponentiation
$ python setup.py install
```
## 💡 Examples
### Small inputs
```python
>>> from mod_nest_exp import mod_nest_exp
>>> mod_nest_exp([6,5,4,3,2], 1948502738) # 6^(5^(4^(3^2))) mod 1948502738
951546056
```
### Larger inputs
Here we demonstrate a computation that is not possible with simple modular exponentiation functions such as `pow`:
```python
>>> from random import randint
>>> seq = [randint(1, 2**64) for _ in range(5)]
>>> seq
[6038140174510300905, 11769918488496772646, 2874847465098133786, 9008748983185995190, 13009674817390511365]
>>> m = randint(1, 2**64)
>>> m
6790053138492639247
>>> mod_nest_exp(seq, m)
3426314670852891859
```
### Benchmark the main function
```python
>>> from mod_nest_exp.core.benchmarks import benchmark_core
>>> benchmark_core(list_lengths=(10, 100, 1000), bit_lengths=(16, 128, 1024), mod_bit_lengths=(16, 32, 64))
Running mod_nest_exp on sequences of l pseudorandom b-bit positive integers over a B-bit modulus (1000 runs per table entry)
=================================================================
sequence length l
10 100 1000
----------------- ----------------- -----------------
B b mean stdev mean stdev mean stdev
-----------------------------------------------------------------
16 | 0.08 0.04 0.08 0.03 0.10 0.03
16 128 | 0.08 0.11 0.08 0.03 0.10 0.04
1024 | 0.08 0.03 0.08 0.03 0.11 0.04
-----------------------------------------------------------------
16 | 0.34 0.32 0.34 0.24 0.35 0.24
32 128 | 0.33 0.23 0.34 0.23 0.36 0.23
1024 | 0.33 0.22 0.33 0.24 0.37 0.24
-----------------------------------------------------------------
16 | 8.82 34.83 6.20 21.27 7.18 30.35
64 128 | 7.66 30.70 6.71 22.72 7.60 26.92
1024 | 5.94 25.10 6.67 20.78 6.76 26.28
=================================================================
```