Modular nested exponentiation
An algorithm that computes modular nested exponentiation efficiently.
🚩 Table of Contents
🗺️ 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:
$ 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:
$ pip install mod-nest-exp
A development version can be installed from GitHub source using setuptools:
$ git clone https://github.com/avivbrook/modular-nested-exponentiation.git
$ cd modular-nested-exponentiation
$ python setup.py install
💡 Examples
Small inputs
>>> 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:
>>> 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
>>> 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
=================================================================
Release files for mod-nest-exp 1.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| mod-nest-exp-1.1.1.tar.gz | 24.8 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| mod_nest_exp-1.1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 50.5 kB
Release files / mod-nest-exp-1.1.1.tar.gz
| Download URL | mod-nest-exp-1.1.1.tar.gz |
|---|---|
| Size | 24.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
01aa8b3ab57bdf6e00eb6c4d19061c7ecefe50b394acafb7993f1f3606717e64
|
|
BLAKE2b-256 checksum How to use checksums |
d96125ab0a7547a1d5334bbeea27c3195e53f24c2e0362ff5248aca42b70cb37
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.3
|
Release files / mod_nest_exp-1.1.1-py3-none-any.whl
| Download URL | mod_nest_exp-1.1.1-py3-none-any.whl |
|---|---|
| Size | 25.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
90a90f4ded4dcafa11326f1c9e4e46522081b05fef0729b994637d72fec224d3
|
|
BLAKE2b-256 checksum How to use checksums |
47f1325f1627dec115f8e18ba76b1cffc8c3cd1b10d0c7c5471fde73394e9f71
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.1 importlib_metadata/3.10.0 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.3
|