boring-math-number-theory 2.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ # Minimal version - only add to when necessary
2
+ **/__pycache__/
3
+ dist/
4
+ **/.dmypy.json
5
+ **/.mypy_cache/
6
+ **/.pytest_cache/
7
+ **/.ruff_cache
@@ -0,0 +1,221 @@
1
+ # CHANGELOG
2
+
3
+ PyPI boring-math-number-theory project.
4
+
5
+ ## Semantic Versioning
6
+
7
+ Strict 3 digit semantic versioning.
8
+
9
+ - **MAJOR** version incremented for incompatible API changes
10
+ - **MINOR** version incremented for backward compatible added functionality
11
+ - **PATCH** version incremented for backward compatible bug fixes
12
+
13
+ See [Semantic Versioning 2.0.0](https://semver.org).
14
+
15
+ ## Releases and other important milestones
16
+
17
+ ### Update - 2025-10-09
18
+
19
+ Created new repo for boring-math-number-theory instead of
20
+ renaming boring-math-integer-math and re-purposing it.
21
+ Will archive the boring-math-integer-math GitHub repo.
22
+ The PyPI repo will be archived after the next coordinated
23
+ release.
24
+
25
+ ### Update - 2025-10-08
26
+
27
+ Broke out combinatorics library (all two functions)
28
+ to its own GitHub repo: boring-math-combinatorics
29
+
30
+ Eventually boring-math-integer-math will be renamed
31
+ to boring-math-number-theory.
32
+
33
+ Both are part of my Boring Math hobby projects.
34
+
35
+ ### Version 1.0.2 - PyPI release date 2025-08-04
36
+
37
+ Bad circulararray requirement in pyproject.toml
38
+
39
+ ### Version 1.0.0 - PyPI release date 2025-08-04
40
+
41
+ After some flaying and several releases I finally got things settled
42
+ down with a 0.8.2 release. I updated docstrings to reflect a name
43
+ change, integer_math.num_theory -> integer_math.number_theory and
44
+ never made the change! Everything worked but the docstring had wrong
45
+ info. After making the changes and running pytest tests, I realized
46
+ I made an API breaking change. Sorry...
47
+
48
+ ### Version 0.7.1 - PyPI release date 2025-07-14
49
+
50
+ - Fixed munged CHANGELOG and Documentation links for PyPI
51
+
52
+ ### Version 0.7.0 - PyPI release date 2025-07-14
53
+
54
+ - First version to use pythonic_fp namespace
55
+ - no longer using dtools namespace packages
56
+
57
+ ### Version 0.5.1 - PyPI release date 2025-04-TBD
58
+
59
+ - Made compatible with latest release dltools
60
+ - dtools.circular-array 3.12.0
61
+ - dtools.fp 1.6.0
62
+ - dtools.queues 0.27.0
63
+ - dtools.tuples 0.27.0
64
+
65
+ ### Version 0.5.0 - PyPI release date 2025-01-17
66
+
67
+ - First PyPI release as bm.integer-math
68
+ - new doc location is
69
+ [here](https://grscheller.github.io/boring-math-docs/)
70
+
71
+ ### Version 0.4.7 - PyPI release date 2024-11-18
72
+
73
+ - Some minor additions
74
+ - consistency changes across all grscheller namespace PyPI repos
75
+
76
+ ### Version 0.4.6 - PyPI release date 2024-10-20
77
+
78
+ - removed docs from repo
79
+ - docs for all grscheller namespace projects maintained
80
+ [here](https://grscheller.github.io/grscheller-pypi-namespace-docs/)
81
+
82
+ ### Version 0.4.4.5 - commit date 2024-09-08
83
+
84
+ - preparing to add more composite number tests
85
+ - primality tests based on Wilson's theorem
86
+ - becomes too slow when calculation starts using BigInts
87
+ - added number theory symbols functions
88
+ - implemented: `legendre_symbol(a, p)`
89
+ - implemented: `jacobi_symbol(a, n)`
90
+
91
+ ### Version 0.4.4.4 - commit date 2024-09-08
92
+
93
+ - re-implemented function primes
94
+ - no longer using sieve of Eratosthenes algorithm
95
+ - now using Wilson's theorem and incremental factorial generation
96
+
97
+ ### Version 0.4.4 - PyPI release date 2024-09-01
98
+
99
+ - removed MathJax expressions
100
+ - not worth the increased maintenance
101
+ - can get almost the same results using `expresions` and unicode
102
+
103
+ ### Version 0.4.3.1 - commit date 2024-08-17
104
+
105
+ - MathJax expressions used to render non-code mathematical expressions
106
+ - pdoc accepts MathJax expressions with --math option
107
+ - `$ pdoc --math -o . grscheller.boring_math`
108
+
109
+ ### Version 0.4.3 - PyPI release date 2024-08-17
110
+
111
+ - updated to use grscheller.circular-array 3.4.1
112
+ - this version of CA compares first with identity before trying equality
113
+ - like Python builtins do
114
+ - now using pdoc instead of pdoc3 for documentation
115
+ - replaced github markdown with std markdown
116
+
117
+ ### Version 0.4.0 - PyPI release date 2024-07-12
118
+
119
+ - updated to use grscheller.circular-array 3.3.1
120
+ - minimum Python version now 3.12
121
+ - target Python version now 3.13
122
+
123
+ ### Version 0.3.1 - PyPI release date 2024-03-09
124
+
125
+ - reflecting grscheller.circular-array PyPI dependency change
126
+ - dependencies = ["grscheller.circular-array >= 0.2.0, < 2.1"]
127
+
128
+ ### Version 0.3.0 - PyPI release date 2024-03-02
129
+
130
+ - decided it a good time for a PyPI release
131
+ - want to use some of the newer features in new endeavors
132
+ - vastly improved documentation over last PyPI release
133
+
134
+ ### Version 0.2.2.0 - commit date 2024-02-28
135
+
136
+ - renamed integer_math.mkCoprime to integer_math.coprime
137
+ - did it now before PyPI v0.3.0
138
+ - realized pdoc uses Markdown as its preferred markup language
139
+ - some differences
140
+ - incorporates some GitHub Markdown additions
141
+ - will format google-style & numpydoc style comments
142
+ - lists are one dimensional, not hierarchical
143
+
144
+ ### Version 0.2.0.0 - commit date 2024-02-21
145
+
146
+ - All implementations going forward for integer_math module will use
147
+ just integer based algorithms. I can't really compete with C based
148
+ code from the Python standard library. The algorithms will then be
149
+ of use if ported to a platform without floating point arithmetic.
150
+ - added two new functions to the integer_math module
151
+ - iSqrt() finds the int square root of an int
152
+ - does same thing as math.isqrt()
153
+ - isSqr() return true if integer argument is a perfect square
154
+
155
+ ### Version 0.1.3 - PyPI release date 2024-02-20
156
+
157
+ - Forgot to update pyproject.toml dependencies
158
+ - dependencies = ["grscheller.circular-array >= 0.1.1, < 1.1"]
159
+
160
+ ### Version 0.1.2 - PyPI release date 2024-01-30
161
+
162
+ - now needs CircularArray v0.1.2
163
+ - integer_math comb uses new foldL method of CircularArray
164
+ - CircularArray was split out of grscheller.datastructures
165
+ - test suite written
166
+
167
+ ### Version 0.1.1 - PyPI release date 2024-01-20
168
+
169
+ - fixed some negative value edge cases
170
+ - lcm(0,0) now gives 0 instead of a divide by zero exception
171
+ - some authors leave lcm(0, 0) as undefined
172
+ - lcm(0, 0) = 0 does make sense
173
+ - since `a * 0 = 0` for all `a >= 0`
174
+ - 0 is the smallest non-negative integer a such that `a * 0 = 0`
175
+ - most math theorems remain true for this case
176
+ - README.md improvements
177
+
178
+ ### Version 0.1.0 - PyPI release date 2024-01-17
179
+
180
+ - initial PyPI grscheller.boring-math release
181
+ - updated pyproject.toml to align with grscheller.datastructures
182
+
183
+ ### Version 0.0.9 - commit date 2024-01-14
184
+
185
+ - changed project's name from boring_math to boring-math
186
+ - both GitHub repo and future PyPI repo
187
+ - more in alignment with what I see on PyPI
188
+ - project is grscheller.boring-math
189
+ - package is still grscheller.boring_math
190
+
191
+ ### Version 0.0.8 - commit date 2024-01-14
192
+
193
+ - working on pyproject.toml infrastructure for PyPI releases
194
+ - will use Flit as the packaging/publishing tool
195
+ - replaced bin/ scripts with `boring_math.integer_math_cli` entry-points
196
+
197
+ ### Version 0.0.4 - commit date 2024-01-10
198
+
199
+ - first coding changes in years!
200
+ - gh-pages configured
201
+
202
+ ### Version 0.0.0.3 - commit date 2023-12-06
203
+
204
+ - added pyproject.toml
205
+
206
+ ### Version 0.0.0.2 - commit date 2023-12-06
207
+
208
+ - got package working again
209
+ - did not understand iterators that well when I first wrote this package
210
+ - replaced my `take` with `itertools.islice`
211
+ - generated docs from docstrings with pdoc3
212
+
213
+ ### Version 0.0.0.1 - commit date 2023-12-06
214
+
215
+ - fixed Markdown issues with first commit
216
+ - Added .gitignore file to anticipate pytest & __pycache__ directories
217
+
218
+ ### Version 0.0.0.0 - commit date 2023-12-06
219
+
220
+ - first commit of source code with with the old pipfile build
221
+ infrastructure removed.
@@ -0,0 +1,176 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
@@ -0,0 +1,52 @@
1
+ Metadata-Version: 2.4
2
+ Name: boring-math-number-theory
3
+ Version: 2.0.0
4
+ Summary: Number Theory Library
5
+ Keywords: gcd,lcm,number-theory,primes
6
+ Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
7
+ Requires-Python: >=3.13
8
+ Description-Content-Type: text/x-rst
9
+ Classifier: Development Status :: 3 - Alpha
10
+ Classifier: Framework :: Pytest
11
+ Classifier: Intended Audience :: Education
12
+ Classifier: Intended Audience :: Science/Research
13
+ Classifier: License :: OSI Approved :: Apache Software License
14
+ Classifier: Operating System :: OS Independent
15
+ Classifier: Programming Language :: Python :: 3.13
16
+ Classifier: Typing :: Typed
17
+ License-File: LICENSE
18
+ Requires-Dist: pythonic-fp-circulararray>=6.0.0
19
+ Requires-Dist: pythonic-fp-iterables>=5.1.2
20
+ Requires-Dist: pytest>=8.4.1 ; extra == "test"
21
+ Project-URL: Changelog, https://github.com/grscheller/boring-math-number-theory/blob/main/CHANGELOG.md
22
+ Project-URL: Documentation, https://grscheller.github.io/boring-math/number_theory/development/build/html
23
+ Project-URL: Homepage, https://grscheller.github.io/boring-math
24
+ Project-URL: Source, https://github.com/grscheller/boring-math-number-theory
25
+ Provides-Extra: test
26
+
27
+ Boring Math - Number Theory
28
+ ===========================
29
+
30
+ PyPI project
31
+ `boring-math-number-theory
32
+ <https://pypi.org/project/boring-math-number-theory>`_.
33
+
34
+ Python module implementing a number theory library.
35
+
36
+ This PyPI project is part of the
37
+ `Boring Math
38
+ <https://grscheller.github.io/boring-math>`_ projects.
39
+
40
+ Documentation
41
+ -------------
42
+
43
+ Documentation for this project is hosted on
44
+ `GitHub Pages
45
+ <https://grscheller.github.io/boring-math/number-theory/development/build/html>`_.
46
+
47
+ Copyright and License
48
+ ---------------------
49
+
50
+ Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
51
+ License, Version 2.0. See the LICENSE file for details.
52
+
@@ -0,0 +1,25 @@
1
+ Boring Math - Number Theory
2
+ ===========================
3
+
4
+ PyPI project
5
+ `boring-math-number-theory
6
+ <https://pypi.org/project/boring-math-number-theory>`_.
7
+
8
+ Python module implementing a number theory library.
9
+
10
+ This PyPI project is part of the
11
+ `Boring Math
12
+ <https://grscheller.github.io/boring-math>`_ projects.
13
+
14
+ Documentation
15
+ -------------
16
+
17
+ Documentation for this project is hosted on
18
+ `GitHub Pages
19
+ <https://grscheller.github.io/boring-math/number-theory/development/build/html>`_.
20
+
21
+ Copyright and License
22
+ ---------------------
23
+
24
+ Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
25
+ License, Version 2.0. See the LICENSE file for details.
@@ -0,0 +1,82 @@
1
+ [project]
2
+ name = "boring-math-number-theory"
3
+ version = "2.0.0"
4
+ readme = "README.rst"
5
+ requires-python = ">=3.13"
6
+ license = { file = "LICENSE" }
7
+ authors = [{ name = "Geoffrey R. Scheller", email = "geoffrey@scheller.com" }]
8
+ keywords = [
9
+ "gcd",
10
+ "lcm",
11
+ "number-theory",
12
+ "primes",
13
+ ]
14
+ classifiers = [
15
+ "Development Status :: 3 - Alpha",
16
+ "Framework :: Pytest",
17
+ "Intended Audience :: Education",
18
+ "Intended Audience :: Science/Research",
19
+ "License :: OSI Approved :: Apache Software License",
20
+ "Operating System :: OS Independent",
21
+ "Programming Language :: Python :: 3.13",
22
+ "Typing :: Typed",
23
+ ]
24
+ dependencies = [
25
+ "pythonic-fp-circulararray>=6.0.0",
26
+ "pythonic-fp-iterables>=5.1.2",
27
+ ]
28
+ dynamic = ["description"]
29
+
30
+ [project.urls]
31
+ Changelog = "https://github.com/grscheller/boring-math-number-theory/blob/main/CHANGELOG.md"
32
+ Documentation = "https://grscheller.github.io/boring-math/number_theory/development/build/html"
33
+ Homepage = "https://grscheller.github.io/boring-math"
34
+ Source = "https://github.com/grscheller/boring-math-number-theory"
35
+
36
+ [project.optional-dependencies]
37
+ test = [
38
+ "pytest>=8.4.1",
39
+ ]
40
+
41
+ [build-system]
42
+ requires = ["flit_core>=3.12,<4"]
43
+ build-backend = "flit_core.buildapi"
44
+
45
+ [tool.flit.module]
46
+ name = "boring_math.number_theory"
47
+
48
+ [tool.mypy]
49
+ enable_incomplete_feature = "NewGenericSyntax"
50
+ explicit_package_bases = true
51
+ implicit_reexport = false
52
+ local_partial_types = true
53
+ namespace_packages = true
54
+ warn_redundant_casts = true
55
+ warn_return_any = true
56
+ warn_unused_configs = true
57
+
58
+ [tool.pylsp-mypy]
59
+ enabled = true
60
+ live-mode = true
61
+ dmypy = false
62
+ strict = true
63
+ report_progress = true
64
+
65
+ [tool.pytest.ini_options]
66
+ addopts = "-ra"
67
+ consider_namespace_packages = true
68
+ testpaths = ["tests/"]
69
+
70
+ [tool.ruff]
71
+ target-version = "py313"
72
+
73
+ [tool.ruff.lint.flake8-quotes]
74
+ docstring-quotes = "double"
75
+
76
+ [tool.ruff.lint.per-file-ignores]
77
+ "tests/*" = ["C901"]
78
+ "**/*.py" = ["E741"]
79
+
80
+ [tool.ruff.format]
81
+ quote-style = "single"
82
+ docstring-code-line-length = 72
@@ -0,0 +1,272 @@
1
+ # Copyright 2016-2025 Geoffrey R. Scheller
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ """Number Theory Library
16
+
17
+ Collection of integer related functions useful in number theory.
18
+
19
+ """
20
+
21
+ from collections.abc import Iterator
22
+ from typing import Final
23
+ from pythonic_fp.circulararray.auto import CA
24
+ from pythonic_fp.iterables.folding import fold_left
25
+
26
+ __all__ = [
27
+ 'gcd',
28
+ 'lcm',
29
+ 'coprime',
30
+ 'iSqrt',
31
+ 'isSqr',
32
+ 'is_prime',
33
+ 'legendre_symbol',
34
+ 'jacobi_symbol',
35
+ 'primes',
36
+ 'primes_capped',
37
+ 'primes_wilson',
38
+ ]
39
+
40
+
41
+ def gcd(m: int, n: int, /) -> int:
42
+ """Uses Euclidean algorithm to compute the gcd of two integers.
43
+
44
+ .. note::
45
+ - mathematically the gcd of 0 and 0 does not exist
46
+ - taking ``gcd(0, 0) = 1`` is a better choice than ``math.gcd(0, 0) = 0``
47
+
48
+ - eliminates lcm & coprime functions having to edge case test
49
+ - also ``gcd(0, 0)`` returning 1 instead of 0 more mathematically justified
50
+
51
+ :param m: first int for gcd calculation
52
+ :param n: second int for gcd calculation
53
+ :returns: gcd of the absolute values of ``m`` and ``n``
54
+
55
+ """
56
+ if 0 == m == n:
57
+ return 1
58
+ m, n = abs(m), abs(n)
59
+ while n > 0:
60
+ m, n = n, m % n
61
+ return m
62
+
63
+
64
+ def lcm(m: int, n: int, /) -> int:
65
+ """Find the least common multiple (lcm) of two integers.
66
+
67
+ :param m: first int for lcm calculation
68
+ :param n: second int for lcm calculation
69
+ :returns: lcm of the absolute values of ``m`` and ``n``
70
+
71
+ """
72
+ m //= gcd(m, n)
73
+ return abs(m * n)
74
+
75
+
76
+ def coprime(m: int, n: int, /) -> tuple[int, int]:
77
+ """Makes 2 integers coprime by dividing out their common factors.
78
+
79
+ :param m: first int for coprime calculation
80
+ :param n: second int for coprime calculation
81
+ :returns: coprimed values with original signs, ``(0, 0)`` when ``n = m = 0``
82
+
83
+ """
84
+ common = gcd(m, n)
85
+ return m // common, n // common
86
+
87
+
88
+ def iSqrt(n: int, /) -> int:
89
+ """Integer square root of a non-negative integer.
90
+
91
+ :param n: integer whose integer square root is to be found
92
+ :returns: the unique ``m`` such that ``m*m <= n < (m+1)*(m+1)``
93
+ :raises ValueError: if ``n < 0``
94
+
95
+ """
96
+ if n < 0:
97
+ msg = 'iSqrt(n): n must be non-negative'
98
+ raise ValueError(msg)
99
+ high = n
100
+ low = 1
101
+ while high > low:
102
+ high = (high + low) // 2
103
+ low = n // high
104
+ return high
105
+
106
+
107
+ def isSqr(n: int, /) -> bool:
108
+ """Determine if argument is a perfect square.
109
+
110
+ :param n: integer to check
111
+ :returns: true only if integer argument is a perfect square
112
+
113
+ """
114
+ return False if n < 0 else n == iSqrt(n) ** 2
115
+
116
+
117
+ def legendre_symbol(a: int, p: int) -> int:
118
+ """Calculate the Legendre Symbol ``(a/p)`` where p is an odd prime.
119
+
120
+ .. note::
121
+
122
+ See https://en.wikipedia.org/wiki/Legendre_symbol
123
+
124
+ :param a: any integer
125
+ :param p: any prime ``p > 2``, does not check that ``p`` is actually prime
126
+ :returns: the Legendre Symbol ``(a/p) ∈ {-1, 0, 1}``
127
+ :raises ValueError: if ``abs(p) < 3``
128
+
129
+ """
130
+ p = abs(p)
131
+ if p < 3:
132
+ msg = 'p must be a prime greater than 2'
133
+ raise ValueError(msg)
134
+ a = a % p
135
+
136
+ if a == 0:
137
+ return 0
138
+ else:
139
+ for x in range(1, p):
140
+ if x * x % p == a:
141
+ return 1
142
+ return -1
143
+
144
+
145
+ def jacobi_symbol(a: int, n: int) -> int:
146
+ """Calculate the Jacobi Symbol ``(a/n)``.
147
+
148
+ .. note::
149
+
150
+ See https://en.wikipedia.org/wiki/Jacobi_symbol
151
+
152
+ :param a: any integer
153
+ :param n: any positive odd integer
154
+ :returns: the Jacobi Symbol ``(a/p) ∈ {-1, 0, 1}``
155
+ :raises ValueError: if ``n`` is not a positive odd integer
156
+
157
+ """
158
+ if n <= 0 or n % 2 == 0:
159
+ msg = 'n must be a positive odd integer'
160
+ raise ValueError(msg)
161
+
162
+ a = a % n
163
+ t = 1
164
+ while a != 0:
165
+ while a % 2 == 0:
166
+ a = a // 2
167
+ r = n % 8
168
+ if r == 3 or r == 5:
169
+ t = -t
170
+ n, a = a, n
171
+ if n % 4 == 3 and a % 4 == 3:
172
+ t = -t
173
+ a = a % n
174
+
175
+ if n == 1:
176
+ return t
177
+ else:
178
+ return 0
179
+
180
+
181
+ def primes_wilson(start: int = 2) -> Iterator[int]:
182
+ """Prime number generation using Wilson's Theorem.
183
+
184
+ .. note::
185
+
186
+ Wilson's Theorem:
187
+ ``∀(n>1)``, ``n`` is prime if and only if ``(n-1)! % n ≡ -1``
188
+
189
+ :param start: first value to check, defaults to 2
190
+ :returns: an infinite iterator of prime numbers
191
+
192
+ """
193
+ if start < 2:
194
+ n = 2
195
+ fact = 1
196
+ else:
197
+ n = start
198
+ fact = CA(range(2, n)).foldl(lambda j, k: j * k, 1)
199
+ while True:
200
+ if fact % n == n - 1:
201
+ yield n
202
+ fact *= n
203
+ n += 1
204
+
205
+
206
+ def primes_capped(start: int, end: int) -> Iterator[int]:
207
+ """Yield all primes ```p``` where ``start <= p <= end``.
208
+
209
+ :param start: first value to check
210
+ :param start: last value to check
211
+ :returns: an iterator of all primes ``p`` where ``start <= p <= end``.
212
+
213
+ """
214
+ for ii in primes_wilson(start):
215
+ if ii < end:
216
+ yield ii
217
+ elif ii == end:
218
+ yield ii
219
+ break
220
+ else:
221
+ break
222
+
223
+
224
+ def primes(start: int = 2, end: int | None = None) -> Iterator[int]:
225
+ """Yield all primes ``p`` where ``start <= p <= end``.
226
+
227
+ .. warning::
228
+
229
+ If ``end`` is not given, returned iterator is infinite.
230
+
231
+ :param start: first value to check, defaults to 2
232
+ :returns: an iterator of all primes ``p`` where ``start <= p <= end``
233
+
234
+ """
235
+ if end is None:
236
+ return primes_wilson(start)
237
+ else:
238
+ return primes_capped(start, end)
239
+
240
+
241
+ def is_prime(n: int, /) -> bool:
242
+ """Test if argument is a prime number, uses Wilson's Theorem.
243
+
244
+ :param n: integer to check if prime
245
+ :returns: true only if ``n`` is prime
246
+
247
+ """
248
+ _factors:Final[int]=2*3*5*7*11*13*17
249
+
250
+ if (n := abs(n)) < 2:
251
+ return False
252
+
253
+ if n >= _factors:
254
+ if gcd(n, _factors) > 1:
255
+ return False
256
+
257
+ if n < _factors:
258
+ return (
259
+ fold_left(
260
+ range(2, n),
261
+ lambda j, k: j * k,
262
+ 1,
263
+ ) % n == n - 1
264
+ )
265
+ else:
266
+ return (
267
+ fold_left(
268
+ range(_factors, n),
269
+ lambda j, k: j * k,
270
+ _factors,
271
+ ) % n == n - 1
272
+ )
@@ -0,0 +1,128 @@
1
+ # Copyright 2023-2024 Geoffrey R. Scheller
2
+ #
3
+ # Licensed under the Apache License, Version 2.0 (the "License");
4
+ # you may not use this file except in compliance with the License.
5
+ # You may obtain a copy of the License at
6
+ #
7
+ # http://www.apache.org/licenses/LICENSE-2.0
8
+ #
9
+ # Unless required by applicable law or agreed to in writing, software
10
+ # distributed under the License is distributed on an "AS IS" BASIS,
11
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
+ # See the License for the specific language governing permissions and
13
+ # limitations under the License.
14
+
15
+ from boring_math.number_theory import gcd, lcm, coprime
16
+ from boring_math.number_theory import is_prime, primes, primes_wilson
17
+ from boring_math.number_theory import legendre_symbol, jacobi_symbol
18
+
19
+ class Test_simple_ones:
20
+ def test_gcd(self)-> None:
21
+ assert gcd(0, 0) == 1 # mathematically does not exist
22
+ assert gcd(1, 1) == 1
23
+ assert gcd(1, 5) == 1
24
+ assert gcd(5, 1) == 1
25
+ assert gcd(0, 5) == 5
26
+ assert gcd(21, 0) == 21
27
+ assert gcd(2, 5) == 1
28
+ assert gcd(5, 2) == 1
29
+ assert gcd(15, 35) == 5
30
+ assert gcd(35, 15) == 5
31
+ assert gcd(2*3*5*7, 3*5*7*11) == 3*5*7
32
+ assert gcd(123454321, 11111) == 11111
33
+ assert gcd(123454321, 1111) == 1
34
+
35
+ def test_lcm(self) -> None:
36
+ assert lcm(5, 0) == 0
37
+ assert lcm(0, 11) == 0
38
+ assert lcm(0, 0) == 0
39
+ assert lcm(3, 5) == 15
40
+ assert lcm(2*3*25*7, 3*5*11) == 2*3*25*7*11
41
+
42
+ def test_mkCoprime(self) -> None:
43
+ assert coprime(0, 0) == (0, 0)
44
+ assert coprime(5, 0) == (1, 0)
45
+ assert coprime(0, 4) == (0, 1)
46
+ assert coprime(1, 4) == (1, 4)
47
+ assert coprime(6, 15) == (2, 5)
48
+ assert coprime(2*3*4*5, 3*4*5*11) == (2, 11)
49
+
50
+ def test_primes(self) -> None:
51
+ assert len(list(primes(10, 5))) == 0
52
+ assert len(list(primes(11, 5))) == 0
53
+ assert len(list(primes(end=11))) == 5
54
+ assert len(list(primes(end=12))) == 5
55
+ assert len(list(primes(start=5, end=11))) == 3
56
+ assert len(list(primes(start=4, end=12))) == 3
57
+
58
+ cnt = 0
59
+ primeList: list[int] = []
60
+ for kk in primes_wilson(36):
61
+ cnt += 1
62
+ if cnt > 5:
63
+ assert False
64
+ primeList.append(kk)
65
+ if kk >= 53:
66
+ break
67
+ assert primeList == [37, 41, 43, 47, 53]
68
+
69
+ cnt = 0
70
+ primeList = []
71
+ for kk in primes(36):
72
+ cnt += 1
73
+ if cnt > 5:
74
+ assert False
75
+ primeList.append(kk)
76
+ if kk >= 53:
77
+ break
78
+ assert primeList == [37, 41, 43, 47, 53]
79
+
80
+ generated = list(primes(10, 50))
81
+ assert generated == [11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
82
+ generated = list(primes(10, 8))
83
+ assert generated == []
84
+ generated = list(primes(0, 3))
85
+ assert generated == [2, 3]
86
+ generated = list(primes(-5, 4))
87
+ assert generated == [2, 3]
88
+
89
+ def test_is_prime(self) -> None:
90
+ assert not is_prime(0)
91
+ assert not is_prime(1)
92
+ assert is_prime(2)
93
+ assert is_prime(3)
94
+ assert not is_prime(4)
95
+ assert is_prime(5)
96
+ assert not is_prime(6)
97
+ assert is_prime(7)
98
+ assert not is_prime(100)
99
+ assert is_prime(101)
100
+ assert not is_prime(111)
101
+ assert not is_prime(11111)
102
+
103
+ def test_symbols(self) -> None:
104
+ legendre3 = [0, 1, -1, 0, 1, -1, 0, 1, -1]
105
+ for ii in range(0, 9):
106
+ assert legendre_symbol(ii, 3) == jacobi_symbol(ii, 3) == legendre3[ii]
107
+
108
+ legendre5 = [0, 1, -1, -1, 1, 0, 1, -1, -1, 1]
109
+ for ii in range(0, 10):
110
+ assert legendre_symbol(ii, 5) == jacobi_symbol(ii, 5) == legendre5[ii]
111
+
112
+ legendre127 = [0, 1, 1, -1, 1, -1, -1, -1, 1, 1, -1, 1, -1, 1, -1,
113
+ 1, 1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, -1, -1, -1, 1]
114
+ for ii in range(0, 31):
115
+ assert legendre_symbol(ii, 127) == jacobi_symbol(ii, 127) == legendre127[ii]
116
+
117
+ jacobi1 = [1] * 100
118
+ for ii in range(0, 100):
119
+ assert jacobi_symbol(ii, 1) == jacobi1[ii] == 1
120
+
121
+ jacobi7 =[0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1]
122
+ for ii in range(0, 14):
123
+ assert jacobi_symbol(ii, 7) == jacobi7[ii]
124
+
125
+ jacobi15 = [0, 1, 1, 0, 1, 0, 0, -1, 1, 0, 0, -1, 0, -1, -1,
126
+ 0, 1, 1, 0, 1, 0, 0, -1, 1, 0, 0, -1, 0, -1, -1]
127
+ for ii in range(0, 30):
128
+ assert jacobi_symbol(ii, 15) == jacobi15[ii]