boring-math.recursive-functions 0.8.1__tar.gz → 5.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.
- boring_math_recursive_functions-5.0.0/PKG-INFO +47 -0
- boring_math_recursive_functions-5.0.0/README.md +23 -0
- boring_math_recursive_functions-5.0.0/pyproject.toml +102 -0
- boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/__init__.py +59 -0
- boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/ackermann.py +53 -0
- {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-5.0.0}/src/boring_math/recursive_functions/cli/__init__.py +13 -10
- boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/cli/ackermann.py → boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/cli/ackermann_cli.py +22 -16
- boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/cli/fibonacci_cli.py +84 -0
- boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/fibonacci.py +72 -0
- boring_math_recursive_functions-0.8.1/.gitignore +0 -11
- boring_math_recursive_functions-0.8.1/CHANGELOG.md +0 -42
- boring_math_recursive_functions-0.8.1/PKG-INFO +0 -55
- boring_math_recursive_functions-0.8.1/README.rst +0 -31
- boring_math_recursive_functions-0.8.1/pyproject.toml +0 -74
- boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/__init__.py +0 -50
- boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/examples.py +0 -114
- boring_math_recursive_functions-0.8.1/tests/test_ackermann.py +0 -50
- boring_math_recursive_functions-0.8.1/tests/test_fibonacci.py +0 -68
- {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-5.0.0}/LICENSE +0 -0
- {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-5.0.0}/src/boring_math/recursive_functions/cli/py.typed +0 -0
- {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-5.0.0}/src/boring_math/recursive_functions/py.typed +0 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: boring-math-recursive-functions
|
|
3
|
+
Version: 5.0.0
|
|
4
|
+
Summary: Recursive functions package.
|
|
5
|
+
Keywords: ackermann,fibonacci,recursive,recursive functions
|
|
6
|
+
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
7
|
+
Requires-Python: >=3.14
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Framework :: Pytest
|
|
11
|
+
Classifier: Intended Audience :: Education
|
|
12
|
+
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
16
|
+
Classifier: Typing :: Typed
|
|
17
|
+
License-File: LICENSE
|
|
18
|
+
Requires-Dist: pythonic-fp-iterables>=6.0.0,<7
|
|
19
|
+
Project-URL: Changelog, https://github.com/grscheller/boring-math-recursive-functions/blob/main/CHANGELOG.md
|
|
20
|
+
Project-URL: Documentation, https://grscheller.github.io/boring-math-recursive-functions
|
|
21
|
+
Project-URL: Homepage, https://grscheller.github.io/boring-math/
|
|
22
|
+
Project-URL: Source, https://github.com/grscheller/boring-math-recursive-functions
|
|
23
|
+
|
|
24
|
+
# Boring Math - Recursive Functions
|
|
25
|
+
|
|
26
|
+
Project is to explore different ways to efficiently implement recursion
|
|
27
|
+
in Python.
|
|
28
|
+
|
|
29
|
+
PyPI project
|
|
30
|
+
[boring-math-recursive-functions](https://pypi.org/project/boring-math-recursive-functions).
|
|
31
|
+
This pypi project is part of the
|
|
32
|
+
[boring math](https://grscheller.github.io/boring-math) projects.
|
|
33
|
+
|
|
34
|
+
## Documentation
|
|
35
|
+
|
|
36
|
+
Documentation and other links for this project are hosted on
|
|
37
|
+
[GitHub Pages](https://grscheller.github.io/boring-math/projects/recursive_functions.html).
|
|
38
|
+
|
|
39
|
+
Functions defined using previously well defined values originating from
|
|
40
|
+
"base cases" are called "primitively recursive functions." Not all
|
|
41
|
+
computable functions are primitively recursive.
|
|
42
|
+
|
|
43
|
+
## Copyright and License
|
|
44
|
+
|
|
45
|
+
Copyright (c) 2023-2026 Geoffrey R. Scheller. Licensed under the Apache
|
|
46
|
+
License, Version 2.0. See the LICENSE file for details.
|
|
47
|
+
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
# Boring Math - Recursive Functions
|
|
2
|
+
|
|
3
|
+
Project is to explore different ways to efficiently implement recursion
|
|
4
|
+
in Python.
|
|
5
|
+
|
|
6
|
+
PyPI project
|
|
7
|
+
[boring-math-recursive-functions](https://pypi.org/project/boring-math-recursive-functions).
|
|
8
|
+
This pypi project is part of the
|
|
9
|
+
[boring math](https://grscheller.github.io/boring-math) projects.
|
|
10
|
+
|
|
11
|
+
## Documentation
|
|
12
|
+
|
|
13
|
+
Documentation and other links for this project are hosted on
|
|
14
|
+
[GitHub Pages](https://grscheller.github.io/boring-math/projects/recursive_functions.html).
|
|
15
|
+
|
|
16
|
+
Functions defined using previously well defined values originating from
|
|
17
|
+
"base cases" are called "primitively recursive functions." Not all
|
|
18
|
+
computable functions are primitively recursive.
|
|
19
|
+
|
|
20
|
+
## Copyright and License
|
|
21
|
+
|
|
22
|
+
Copyright (c) 2023-2026 Geoffrey R. Scheller. Licensed under the Apache
|
|
23
|
+
License, Version 2.0. See the LICENSE file for details.
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["flit_core>=3.12,<4"]
|
|
3
|
+
build-backend = "flit_core.buildapi"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "boring-math-recursive-functions"
|
|
7
|
+
version = "5.0.0"
|
|
8
|
+
description = "Recursive functions package."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.14" # floor: PEP 649/749 deferred annotations — class-level self-reference NameErrors on <3.14
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Geoffrey R. Scheller", email = "geoffrey@scheller.com" },
|
|
13
|
+
]
|
|
14
|
+
keywords = [
|
|
15
|
+
"ackermann",
|
|
16
|
+
"fibonacci",
|
|
17
|
+
"recursive",
|
|
18
|
+
"recursive functions",
|
|
19
|
+
]
|
|
20
|
+
classifiers = [
|
|
21
|
+
"Development Status :: 3 - Alpha",
|
|
22
|
+
"Framework :: Pytest",
|
|
23
|
+
"Intended Audience :: Education",
|
|
24
|
+
"License :: OSI Approved :: Apache Software License",
|
|
25
|
+
"Operating System :: OS Independent",
|
|
26
|
+
"Programming Language :: Python :: 3.14",
|
|
27
|
+
"Programming Language :: Python :: 3.15",
|
|
28
|
+
"Typing :: Typed",
|
|
29
|
+
]
|
|
30
|
+
dependencies = [
|
|
31
|
+
"pythonic-fp-iterables>=6.0.0,<7",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.scripts]
|
|
35
|
+
ackermann_list = "boring_math.recursive_functions.cli.ackermann_cli:ackermann_list_cli"
|
|
36
|
+
fibonacci = "boring_math.recursive_functions.cli.fibonacci_cli:fibonacci"
|
|
37
|
+
|
|
38
|
+
[project.urls]
|
|
39
|
+
Changelog = "https://github.com/grscheller/boring-math-recursive-functions/blob/main/CHANGELOG.md"
|
|
40
|
+
Documentation = "https://grscheller.github.io/boring-math-recursive-functions"
|
|
41
|
+
Homepage = "https://grscheller.github.io/boring-math/"
|
|
42
|
+
Source = "https://github.com/grscheller/boring-math-recursive-functions"
|
|
43
|
+
|
|
44
|
+
[dependency-groups]
|
|
45
|
+
docs = [
|
|
46
|
+
"furo>=2025.12.19",
|
|
47
|
+
"sphinx>=9.1",
|
|
48
|
+
]
|
|
49
|
+
test = [
|
|
50
|
+
"pytest>=8.4.1",
|
|
51
|
+
]
|
|
52
|
+
|
|
53
|
+
[tool.flit.module]
|
|
54
|
+
name = "boring_math.recursive_functions"
|
|
55
|
+
|
|
56
|
+
[tool.flit.sdist]
|
|
57
|
+
exclude = [
|
|
58
|
+
"docs/",
|
|
59
|
+
"tests/",
|
|
60
|
+
"**/.mypy_cache/",
|
|
61
|
+
"**/.ruff_cache/",
|
|
62
|
+
"**/.pytest_cache/",
|
|
63
|
+
"**/__pycache__/",
|
|
64
|
+
"**/.dmypy.json",
|
|
65
|
+
]
|
|
66
|
+
|
|
67
|
+
[tool.mypy]
|
|
68
|
+
explicit_package_bases = true
|
|
69
|
+
local_partial_types = true
|
|
70
|
+
namespace_packages = true
|
|
71
|
+
strict = true
|
|
72
|
+
warn_redundant_casts = true
|
|
73
|
+
warn_return_any = true
|
|
74
|
+
warn_unused_configs = true
|
|
75
|
+
|
|
76
|
+
[[tool.mypy.overrides]]
|
|
77
|
+
module = ["tests.*"]
|
|
78
|
+
strict_equality = false
|
|
79
|
+
|
|
80
|
+
[tool.pytest]
|
|
81
|
+
addopts = ["-ra"]
|
|
82
|
+
consider_namespace_packages = true
|
|
83
|
+
testpaths = ["tests/"]
|
|
84
|
+
|
|
85
|
+
[tool.ruff]
|
|
86
|
+
target-version = "py314"
|
|
87
|
+
|
|
88
|
+
[tool.ruff.lint]
|
|
89
|
+
ignore = ["RUF022", "E741"]
|
|
90
|
+
|
|
91
|
+
[tool.ruff.lint.flake8-quotes]
|
|
92
|
+
docstring-quotes = "double"
|
|
93
|
+
|
|
94
|
+
[tool.ruff.lint.per-file-ignores]
|
|
95
|
+
"tests/**/*.py" = [
|
|
96
|
+
"E731",
|
|
97
|
+
"C901",
|
|
98
|
+
]
|
|
99
|
+
|
|
100
|
+
[tool.ruff.format]
|
|
101
|
+
quote-style = "single"
|
|
102
|
+
docstring-code-line-length = 72
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
# Copyright 2016-2026 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
|
+
"""
|
|
16
|
+
Recursive Functions
|
|
17
|
+
-------------------
|
|
18
|
+
|
|
19
|
+
Project to explore efficient ways to implement recursive functions. This
|
|
20
|
+
project is more about exploring how to implement recursive functions
|
|
21
|
+
efficiently than exposing tools to do such.
|
|
22
|
+
|
|
23
|
+
.. admonition:: Ackermann's Function
|
|
24
|
+
|
|
25
|
+
Ackermann's function is an example of a function that is computable
|
|
26
|
+
but not primitively recursive. It quickly becomes computationally
|
|
27
|
+
intractable for relatively small values of m.
|
|
28
|
+
|
|
29
|
+
Ackermann function is defined recursively by
|
|
30
|
+
|
|
31
|
+
``ackermann(0,n) = n+1`` for ``n >= 0``
|
|
32
|
+
|
|
33
|
+
``ackermann(m,0) = ackermann(m-1,1)`` for ``m >= 0``
|
|
34
|
+
|
|
35
|
+
``ackermann(m,n) = ackermann(m-1, ackermann(m,n-1))`` for ``m, n > 0``
|
|
36
|
+
|
|
37
|
+
.. admonition:: Fibonacci Sequences
|
|
38
|
+
|
|
39
|
+
The Fibonacci sequence is usually taught in grade school as the
|
|
40
|
+
first recursive function that is not either an arithmetic or geometric
|
|
41
|
+
progression.
|
|
42
|
+
|
|
43
|
+
The Fibonacci sequence is traditionally defined as
|
|
44
|
+
|
|
45
|
+
``f₁ = 1``
|
|
46
|
+
|
|
47
|
+
``f₂ = 1``
|
|
48
|
+
|
|
49
|
+
``fₙ₊₂ = fₙ₊₁ + fₙ``
|
|
50
|
+
|
|
51
|
+
Actually, the Fibonacci sequence can be extended in both directions.
|
|
52
|
+
|
|
53
|
+
``..., 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 6, 13, ...``
|
|
54
|
+
|
|
55
|
+
"""
|
|
56
|
+
|
|
57
|
+
__author__ = 'Geoffrey R. Scheller'
|
|
58
|
+
__copyright__ = 'Copyright (c) 2016-2026 Geoffrey R. Scheller'
|
|
59
|
+
__license__ = 'Apache License 2.0'
|
|
@@ -0,0 +1,53 @@
|
|
|
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
|
+
"""
|
|
16
|
+
.. admonition:: Ackermann examples
|
|
17
|
+
|
|
18
|
+
Example implementations for Ackermann's function.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
__all__ = ['ackermann_list']
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def ackermann_list(m: int, n: int) -> int:
|
|
25
|
+
"""
|
|
26
|
+
.. admonition:: Ackermann's Function
|
|
27
|
+
|
|
28
|
+
:param m: First argument to Ackermann's function.
|
|
29
|
+
:param n: Second argument to Ackermann's function.
|
|
30
|
+
:returns: A very hard to calculate useless value.
|
|
31
|
+
|
|
32
|
+
.. note::
|
|
33
|
+
|
|
34
|
+
This implementation models the recursion with a Python list
|
|
35
|
+
instead of Python's call stack. It then evaluates the
|
|
36
|
+
innermost ackermann function first. To naively use call
|
|
37
|
+
stack recursion would quickly result in a stack overflow.
|
|
38
|
+
|
|
39
|
+
"""
|
|
40
|
+
acker = [m, n]
|
|
41
|
+
|
|
42
|
+
while len(acker) > 1:
|
|
43
|
+
mm, nn = acker[-2:]
|
|
44
|
+
if mm < 1:
|
|
45
|
+
acker[-1] = acker.pop() + 1
|
|
46
|
+
elif nn < 1:
|
|
47
|
+
acker[-2] = acker[-2] - 1
|
|
48
|
+
acker[-1] = 1
|
|
49
|
+
else:
|
|
50
|
+
acker[-2] = mm - 1
|
|
51
|
+
acker[-1] = mm
|
|
52
|
+
acker.append(nn - 1)
|
|
53
|
+
return acker[0]
|
|
@@ -13,20 +13,23 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
"""
|
|
16
|
-
|
|
17
|
-
----------------------------------------------------
|
|
16
|
+
.. admonition:: CGI script entry points
|
|
18
17
|
|
|
19
|
-
|
|
20
|
-
using the packaging build backend
|
|
21
|
-
section in the project's
|
|
18
|
+
Automatically generate Python scripts in an OS independent way
|
|
19
|
+
using the packaging build's backend, *flit* in this case. This
|
|
20
|
+
is configured by the [project.scripts] section in the project's
|
|
21
|
+
pyproject.toml file.
|
|
22
22
|
|
|
23
|
-
Example applications
|
|
24
|
-
--------------------
|
|
23
|
+
**Example applications:**
|
|
25
24
|
|
|
26
|
-
|
|
25
|
+
.. admonition:: Ackermann's function
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
- **ackermann_list:** Calculate values for a computable
|
|
28
|
+
but not simple recursive function.
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
.. admonition:: Fibonacci sequences
|
|
31
|
+
|
|
32
|
+
- **fibonacci:** Generate Fibonacci sequences both forwards
|
|
33
|
+
and backwards.
|
|
31
34
|
|
|
32
35
|
"""
|
|
@@ -13,35 +13,41 @@
|
|
|
13
13
|
# limitations under the License.
|
|
14
14
|
|
|
15
15
|
"""
|
|
16
|
-
|
|
16
|
+
.. admonition:: Programs to evaluate Ackermann's function.
|
|
17
17
|
|
|
18
|
-
.. note::
|
|
19
|
-
The version of the Ackermann's function being used is defined recursively by
|
|
18
|
+
.. note::
|
|
20
19
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
ackermann(m,n) = ackermann(m-1, ackermann(m, n-1)) for m,n > 0
|
|
20
|
+
The version of the Ackermann's function being used is defined
|
|
21
|
+
recursively by
|
|
24
22
|
|
|
25
|
-
|
|
26
|
-
|
|
23
|
+
- ``ackermann(0,n) = n+1 for n >= 0``
|
|
24
|
+
- ``ackermann(m,0) = ackermann(m-1,1) for m >= 0``
|
|
25
|
+
- ``ackermann(m,n) = ackermann(m-1, ackermann(m, n-1)) for m,n > 0``
|
|
27
26
|
|
|
28
|
-
|
|
27
|
+
Ackermann's function is an example of a computable but not primitively
|
|
28
|
+
recursive function.
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
- **ackermann_list:** Computes Ackermann's function by simulating recursion with a list.
|
|
31
31
|
|
|
32
|
-
|
|
32
|
+
"""
|
|
33
33
|
|
|
34
34
|
import sys
|
|
35
|
-
from boring_math.recursive_functions.
|
|
35
|
+
from boring_math.recursive_functions.ackermann import ackermann_list
|
|
36
36
|
|
|
37
37
|
|
|
38
|
-
def
|
|
38
|
+
def ackermann_list_cli() -> None:
|
|
39
39
|
"""Evaluate Ackermann's function simulating recursion with a Python list.
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
.. admonition:: Usage
|
|
42
|
+
|
|
43
|
+
.. code-block:: console
|
|
44
|
+
|
|
45
|
+
$ ackermann_list m n
|
|
46
|
+
|
|
47
|
+
.. note::
|
|
42
48
|
|
|
43
|
-
|
|
44
|
-
|
|
49
|
+
Ackermann's function becomes numerically intractable
|
|
50
|
+
after m=4 n=1.
|
|
45
51
|
|
|
46
52
|
"""
|
|
47
53
|
args = sys.argv[1:]
|
boring_math_recursive_functions-5.0.0/src/boring_math/recursive_functions/cli/fibonacci_cli.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# Copyright 2023-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
|
+
"""
|
|
16
|
+
.. admonition:: Programs involving Fibonacci sequences.
|
|
17
|
+
|
|
18
|
+
- **fibonacci:** Prints generated Fibonacci sequences to stdout.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
import sys
|
|
23
|
+
from collections.abc import Callable, Iterator
|
|
24
|
+
from typing import cast
|
|
25
|
+
from boring_math.recursive_functions.fibonacci import orderable_generator
|
|
26
|
+
from pythonic_fp.iterables.drop_take import take
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def fibonacci() -> None:
|
|
30
|
+
"""
|
|
31
|
+
.. admonition:: Fibonacci
|
|
32
|
+
|
|
33
|
+
Calculate Fibonacci sequences both forward ans backwards.
|
|
34
|
+
|
|
35
|
+
.. admonition:: Usage
|
|
36
|
+
|
|
37
|
+
.. code-block:: console
|
|
38
|
+
|
|
39
|
+
$ fibonacci [-r] f1 f2 n
|
|
40
|
+
|
|
41
|
+
where
|
|
42
|
+
|
|
43
|
+
- **f1**: first number in sequence
|
|
44
|
+
- **f2**: second number in sequence
|
|
45
|
+
- **n**: number of terms to output
|
|
46
|
+
- **-r** reverse direction of sequence
|
|
47
|
+
|
|
48
|
+
"""
|
|
49
|
+
nargs = len(args := sys.argv[1:])
|
|
50
|
+
if nargs < 3 or nargs > 4:
|
|
51
|
+
print('Error: Wrong number of arguments given', file=sys.stderr)
|
|
52
|
+
sys.exit(1)
|
|
53
|
+
if nargs == 3:
|
|
54
|
+
try:
|
|
55
|
+
fib1 = int(args[0])
|
|
56
|
+
fib2 = int(args[1])
|
|
57
|
+
n = int(args[2])
|
|
58
|
+
fib_gen = cast(
|
|
59
|
+
Callable[[int, int], Iterator[int]],
|
|
60
|
+
lambda f1, f2: orderable_generator(f1, f2, forward=True),
|
|
61
|
+
)
|
|
62
|
+
except ValueError:
|
|
63
|
+
print('Error: Non-integer argument given', file=sys.stderr)
|
|
64
|
+
sys.exit(1)
|
|
65
|
+
if nargs == 4:
|
|
66
|
+
match args[0]:
|
|
67
|
+
case '-r':
|
|
68
|
+
try:
|
|
69
|
+
fib1 = int(args[1])
|
|
70
|
+
fib2 = int(args[2])
|
|
71
|
+
n = int(args[3])
|
|
72
|
+
fib_gen = cast(
|
|
73
|
+
Callable[[int, int], Iterator[int]],
|
|
74
|
+
lambda f1, f2: orderable_generator(f1, f2, forward=False),
|
|
75
|
+
)
|
|
76
|
+
except ValueError:
|
|
77
|
+
print('Error: Non-integer argument given', file=sys.stderr)
|
|
78
|
+
sys.exit(1)
|
|
79
|
+
case opt:
|
|
80
|
+
print(f'Error: Unknown option "{opt}" given', file=sys.stderr)
|
|
81
|
+
sys.exit(1)
|
|
82
|
+
|
|
83
|
+
for fib in take(fib_gen(fib1, fib2), abs(n)):
|
|
84
|
+
print(fib)
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
# Copyright 2016-2026 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
|
+
"""
|
|
16
|
+
.. admonition:: Fibonacci sequence
|
|
17
|
+
|
|
18
|
+
Example implementation for Fibonacci sequences.
|
|
19
|
+
|
|
20
|
+
"""
|
|
21
|
+
|
|
22
|
+
from collections.abc import Iterator
|
|
23
|
+
from typing import Protocol, Self
|
|
24
|
+
|
|
25
|
+
__all__ = ['orderable_generator']
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
class Orderable(Protocol):
|
|
29
|
+
def __lt__(self: Self, other: Self) -> bool: ...
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
class Ring(Protocol):
|
|
33
|
+
def __add__(self: Self, other: Self) -> Self: ...
|
|
34
|
+
def __sub__(self: Self, other: Self) -> Self: ...
|
|
35
|
+
def __mult__(self: Self, other: Self) -> Self: ...
|
|
36
|
+
def __mod__(self: Self, other: Self) -> Self: ...
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class OrderedRing(Orderable, Ring, Protocol):
|
|
40
|
+
def __mod__(self: Self, other: Self) -> Self: ...
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
def orderable_generator[T: OrderedRing](
|
|
44
|
+
fib0: T, fib1: T, forward: bool = True
|
|
45
|
+
) -> Iterator[T]:
|
|
46
|
+
"""
|
|
47
|
+
.. admonition:: Generate a Fibonacci sequence
|
|
48
|
+
|
|
49
|
+
:param fib0: Zeroth numeric element of the sequence.
|
|
50
|
+
:param fib1: Next numeric element of the sequence.
|
|
51
|
+
:param forward: Generate sequence in forward or reverse order.
|
|
52
|
+
:yields: An iterator iterating over a Fibonacci sequence.
|
|
53
|
+
|
|
54
|
+
.. note::
|
|
55
|
+
|
|
56
|
+
- ``fibonacci.orderable_generator(fib0=0, fib1=1)`` generates
|
|
57
|
+
|
|
58
|
+
- the sequence ``0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...``
|
|
59
|
+
|
|
60
|
+
- ``fibonacci.orderable_generator(fib0=5, fib1=3, forward = False)`` generates
|
|
61
|
+
|
|
62
|
+
- the sequence ``5, 3, 2, 1, 1, 0, 1, -1, 2, -3, 5, -8, ...``
|
|
63
|
+
|
|
64
|
+
"""
|
|
65
|
+
if forward:
|
|
66
|
+
while True:
|
|
67
|
+
yield fib0
|
|
68
|
+
fib0, fib1 = fib1, fib0 + fib1
|
|
69
|
+
else:
|
|
70
|
+
while True:
|
|
71
|
+
yield fib0
|
|
72
|
+
fib0, fib1 = fib1, fib0 - fib1
|
|
@@ -1,42 +0,0 @@
|
|
|
1
|
-
# CHANGELOG
|
|
2
|
-
|
|
3
|
-
PyPI `boring-math.recursive-functions` 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
|
-
### Version 0.8.0 - PyPI release date 2025-08-04
|
|
18
|
-
|
|
19
|
-
Infrastructure updated, now similar to pythonic-fp projects.
|
|
20
|
-
|
|
21
|
-
First version released under boring-math namespace.
|
|
22
|
-
|
|
23
|
-
### Version 0.7.0 - PyPI release date 2025-07-31
|
|
24
|
-
|
|
25
|
-
First version released under boring-math namespace.
|
|
26
|
-
|
|
27
|
-
New PyPI name is `boring-math.recursive-functions
|
|
28
|
-
|
|
29
|
-
### Version 0.6.1 - PyPI release date 2025-07-14
|
|
30
|
-
|
|
31
|
-
Fixed munged CHANGELOG and Documentation links for PyPI.
|
|
32
|
-
|
|
33
|
-
### Version 0.6.0 - PyPI release date 2025-07-14
|
|
34
|
-
|
|
35
|
-
- First version to use pythonic_fp namespace
|
|
36
|
-
- no longer using dtools namespace packages
|
|
37
|
-
|
|
38
|
-
### Version 0.5.0 - PyPI release date 2025-01-??
|
|
39
|
-
|
|
40
|
-
- Part of the Boring Math projects
|
|
41
|
-
- PyPI **bm.** namespace
|
|
42
|
-
- Migrated code from grscheller/boring-math repo
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
Metadata-Version: 2.4
|
|
2
|
-
Name: boring-math-recursive-functions
|
|
3
|
-
Version: 0.8.1
|
|
4
|
-
Summary: Recursive Functions Package
|
|
5
|
-
Keywords: ackermann,fibonacci,recursive,recursive functions
|
|
6
|
-
Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
|
|
7
|
-
Requires-Python: >=3.12
|
|
8
|
-
Description-Content-Type: text/x-rst
|
|
9
|
-
Classifier: Development Status :: 3 - Alpha
|
|
10
|
-
Classifier: Framework :: Pytest
|
|
11
|
-
Classifier: Intended Audience :: Education
|
|
12
|
-
Classifier: License :: OSI Approved :: Apache Software License
|
|
13
|
-
Classifier: Operating System :: OS Independent
|
|
14
|
-
Classifier: Programming Language :: Python :: 3.13
|
|
15
|
-
Classifier: Typing :: Typed
|
|
16
|
-
License-File: LICENSE
|
|
17
|
-
Requires-Dist: pytest>=8.4.1 ; extra == "test"
|
|
18
|
-
Project-URL: Changelog, https://github.com/grscheller/boring-math-recursive-functions/blob/main/CHANGELOG.md
|
|
19
|
-
Project-URL: Documentation, https://grscheller.github.io/boring-math/recursive-functions/development/build/html/releases.html
|
|
20
|
-
Project-URL: Homepage, https://grscheller.github.io/boring-math/blob/main/README.md
|
|
21
|
-
Project-URL: Source, https://github.com/grscheller/boring-math-recursive-functions
|
|
22
|
-
Provides-Extra: test
|
|
23
|
-
|
|
24
|
-
Boring Math - Recursive Functions
|
|
25
|
-
=================================
|
|
26
|
-
|
|
27
|
-
PyPI project
|
|
28
|
-
`boring-math-recursive-functions
|
|
29
|
-
<https://pypi.org/project/boring-math-recursive-functions>`_.
|
|
30
|
-
|
|
31
|
-
The purpose of this project is to explore different ways to efficiently
|
|
32
|
-
implement recursion in Python.
|
|
33
|
-
|
|
34
|
-
Computable recursive functions can be defined with previously defined or even
|
|
35
|
-
not yet defined values. Functions defined using previously well defined values
|
|
36
|
-
having "base cases" are called "primitively recursive functions." Not all
|
|
37
|
-
computable functions are primitively recursive.
|
|
38
|
-
|
|
39
|
-
This PyPI project is part of of the grscheller
|
|
40
|
-
`boring-math namespace projects
|
|
41
|
-
<https://github.com/grscheller/boring-math/blob/main/README.md>`_
|
|
42
|
-
|
|
43
|
-
Documentation
|
|
44
|
-
-------------
|
|
45
|
-
|
|
46
|
-
Documentation for this project is hosted on
|
|
47
|
-
`GitHub Pages
|
|
48
|
-
<https://grscheller.github.io/boring-math/recursive-functions/development/build/html>`_.
|
|
49
|
-
|
|
50
|
-
Copyright and License
|
|
51
|
-
---------------------
|
|
52
|
-
|
|
53
|
-
Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
|
|
54
|
-
License, Version 2.0. See the LICENSE file for details.
|
|
55
|
-
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
Boring Math - Recursive Functions
|
|
2
|
-
=================================
|
|
3
|
-
|
|
4
|
-
PyPI project
|
|
5
|
-
`boring-math-recursive-functions
|
|
6
|
-
<https://pypi.org/project/boring-math-recursive-functions>`_.
|
|
7
|
-
|
|
8
|
-
The purpose of this project is to explore different ways to efficiently
|
|
9
|
-
implement recursion in Python.
|
|
10
|
-
|
|
11
|
-
Computable recursive functions can be defined with previously defined or even
|
|
12
|
-
not yet defined values. Functions defined using previously well defined values
|
|
13
|
-
having "base cases" are called "primitively recursive functions." Not all
|
|
14
|
-
computable functions are primitively recursive.
|
|
15
|
-
|
|
16
|
-
This PyPI project is part of of the grscheller
|
|
17
|
-
`boring-math namespace projects
|
|
18
|
-
<https://github.com/grscheller/boring-math/blob/main/README.md>`_
|
|
19
|
-
|
|
20
|
-
Documentation
|
|
21
|
-
-------------
|
|
22
|
-
|
|
23
|
-
Documentation for this project is hosted on
|
|
24
|
-
`GitHub Pages
|
|
25
|
-
<https://grscheller.github.io/boring-math/recursive-functions/development/build/html>`_.
|
|
26
|
-
|
|
27
|
-
Copyright and License
|
|
28
|
-
---------------------
|
|
29
|
-
|
|
30
|
-
Copyright (c) 2023-2025 Geoffrey R. Scheller. Licensed under the Apache
|
|
31
|
-
License, Version 2.0. See the LICENSE file for details.
|
|
@@ -1,74 +0,0 @@
|
|
|
1
|
-
[project]
|
|
2
|
-
name = "boring-math-recursive-functions"
|
|
3
|
-
version = "0.8.1"
|
|
4
|
-
readme = "README.rst"
|
|
5
|
-
requires-python = ">=3.12"
|
|
6
|
-
license = { file = "LICENSE" }
|
|
7
|
-
authors = [{ name = "Geoffrey R. Scheller", email = "geoffrey@scheller.com" }]
|
|
8
|
-
keywords = [
|
|
9
|
-
"ackermann",
|
|
10
|
-
"fibonacci",
|
|
11
|
-
"recursive",
|
|
12
|
-
"recursive functions",
|
|
13
|
-
]
|
|
14
|
-
classifiers = [
|
|
15
|
-
"Development Status :: 3 - Alpha",
|
|
16
|
-
"Framework :: Pytest",
|
|
17
|
-
"Intended Audience :: Education",
|
|
18
|
-
"License :: OSI Approved :: Apache Software License",
|
|
19
|
-
"Operating System :: OS Independent",
|
|
20
|
-
"Programming Language :: Python :: 3.13",
|
|
21
|
-
"Typing :: Typed",
|
|
22
|
-
]
|
|
23
|
-
dependencies = []
|
|
24
|
-
dynamic = ["description"]
|
|
25
|
-
|
|
26
|
-
[project.urls]
|
|
27
|
-
Changelog = "https://github.com/grscheller/boring-math-recursive-functions/blob/main/CHANGELOG.md"
|
|
28
|
-
Documentation = "https://grscheller.github.io/boring-math/recursive-functions/development/build/html/releases.html"
|
|
29
|
-
Homepage = "https://grscheller.github.io/boring-math/blob/main/README.md"
|
|
30
|
-
Source = "https://github.com/grscheller/boring-math-recursive-functions"
|
|
31
|
-
|
|
32
|
-
[project.scripts]
|
|
33
|
-
ackermann = "boring_math.recursive_functions.cli.ackermann:ackermann_cli"
|
|
34
|
-
|
|
35
|
-
[project.optional-dependencies]
|
|
36
|
-
test = [
|
|
37
|
-
"pytest>=8.4.1",
|
|
38
|
-
]
|
|
39
|
-
|
|
40
|
-
[build-system]
|
|
41
|
-
requires = ["flit_core>=3.12,<4"]
|
|
42
|
-
build-backend = "flit_core.buildapi"
|
|
43
|
-
|
|
44
|
-
[tool.flit.module]
|
|
45
|
-
name = "boring_math.recursive_functions"
|
|
46
|
-
|
|
47
|
-
[tool.mypy]
|
|
48
|
-
enable_incomplete_feature = ["NewGenericSyntax"]
|
|
49
|
-
implicit_reexport = false
|
|
50
|
-
local_partial_types = true
|
|
51
|
-
warn_redundant_casts = true
|
|
52
|
-
warn_return_any = true
|
|
53
|
-
warn_unused_configs = true
|
|
54
|
-
|
|
55
|
-
[tool.pylsp-mypy]
|
|
56
|
-
enabled = true
|
|
57
|
-
live-mode = true
|
|
58
|
-
strict = true
|
|
59
|
-
report_progress = true
|
|
60
|
-
|
|
61
|
-
[tool.pytest.ini_options]
|
|
62
|
-
addopts = "-ra"
|
|
63
|
-
consider_namespace_packages = true
|
|
64
|
-
testpaths = ["tests/"]
|
|
65
|
-
|
|
66
|
-
[tool.ruff]
|
|
67
|
-
target-version = "py313"
|
|
68
|
-
|
|
69
|
-
[tool.ruff.lint.flake8-quotes]
|
|
70
|
-
docstring-quotes = "double"
|
|
71
|
-
|
|
72
|
-
[tool.ruff.format]
|
|
73
|
-
quote-style = "single"
|
|
74
|
-
docstring-code-line-length = 72
|
|
@@ -1,50 +0,0 @@
|
|
|
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
|
-
"""
|
|
16
|
-
Recursive Functions Package
|
|
17
|
-
===========================
|
|
18
|
-
|
|
19
|
-
Package to explore efficient ways to implement recursive functions.
|
|
20
|
-
|
|
21
|
-
Example recursive functions
|
|
22
|
-
---------------------------
|
|
23
|
-
|
|
24
|
-
- module: boring_math.recursive_functions.examples
|
|
25
|
-
|
|
26
|
-
- Ackermann's function
|
|
27
|
-
|
|
28
|
-
- with list
|
|
29
|
-
|
|
30
|
-
- function: boring_math.recursive_functions.examples.ackermann_list
|
|
31
|
-
|
|
32
|
-
- Fibonacci sequences
|
|
33
|
-
|
|
34
|
-
- with generators
|
|
35
|
-
|
|
36
|
-
- function: boring_math.recursive_functions.examples.fibonacci_generator
|
|
37
|
-
- function: boring_math.recursive_functions.examples.rev_fibonacci_generator
|
|
38
|
-
|
|
39
|
-
CLI program entry points
|
|
40
|
-
------------------------
|
|
41
|
-
|
|
42
|
-
- package: boring_math.recursive_functions.cli
|
|
43
|
-
|
|
44
|
-
- module: boring_math.recursive_functions.cli.ackermann
|
|
45
|
-
|
|
46
|
-
"""
|
|
47
|
-
|
|
48
|
-
__author__ = 'Geoffrey R. Scheller'
|
|
49
|
-
__copyright__ = 'Copyright (c) 2016-2025 Geoffrey R. Scheller'
|
|
50
|
-
__license__ = 'Apache License 2.0'
|
|
@@ -1,114 +0,0 @@
|
|
|
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
|
-
"""
|
|
16
|
-
Examples of recursive functions
|
|
17
|
-
===============================
|
|
18
|
-
|
|
19
|
-
Examples implementations for various recursive functions.
|
|
20
|
-
|
|
21
|
-
Ackermann function
|
|
22
|
-
------------------
|
|
23
|
-
|
|
24
|
-
Ackermann's function is an example of a function that is computable
|
|
25
|
-
but not primitively recursive. It quickly becomes computationally
|
|
26
|
-
intractable for relatively small values of m.
|
|
27
|
-
|
|
28
|
-
Ackermann function is defined recursively by
|
|
29
|
-
|
|
30
|
-
- ``ackermann(0,n)=n+1 for n >= 0``
|
|
31
|
-
- ``ackermann(m,0)=ackermann(m-1,1) for m >= 0``
|
|
32
|
-
- ``ackermann(m,n)=ackermann(m-1, ackermann(m,n-1)) for m,n > 0``
|
|
33
|
-
|
|
34
|
-
Fibonacci sequences
|
|
35
|
-
-------------------
|
|
36
|
-
|
|
37
|
-
The Fibonacci sequence is usually taught in grade school as the
|
|
38
|
-
first recursive function that is not either an arithmetic or geometric
|
|
39
|
-
sequence.
|
|
40
|
-
|
|
41
|
-
The Fibonacci sequence is traditionally defined as
|
|
42
|
-
|
|
43
|
-
- ``f₁ = 1``
|
|
44
|
-
- ``f₂ = 1``
|
|
45
|
-
- ``fₙ₊₂ = fₙ₊₁ + fₙ``
|
|
46
|
-
|
|
47
|
-
Actually, a Fibonacci extends can extend in both directions.
|
|
48
|
-
|
|
49
|
-
- ..., 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 6, 13, ...
|
|
50
|
-
|
|
51
|
-
"""
|
|
52
|
-
|
|
53
|
-
from __future__ import annotations
|
|
54
|
-
|
|
55
|
-
from collections.abc import Iterator
|
|
56
|
-
|
|
57
|
-
__all__ = ['ackermann_list', 'fibonacci_generator', 'rev_fibonacci_generator']
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
def ackermann_list(m: int, n: int) -> int:
|
|
61
|
-
"""Ackermann's Function.
|
|
62
|
-
|
|
63
|
-
Evaluate Ackermann's function simulating recursion with a list.
|
|
64
|
-
|
|
65
|
-
.. note::
|
|
66
|
-
This implementation models the recursion with a Python list instead of Python's
|
|
67
|
-
"call stack". It then evaluates the innermost ackermann function first. To
|
|
68
|
-
naively use call stack recursion would result in the loss of stack safety.
|
|
69
|
-
|
|
70
|
-
"""
|
|
71
|
-
acker = [m, n]
|
|
72
|
-
|
|
73
|
-
while len(acker) > 1:
|
|
74
|
-
mm, nn = acker[-2:]
|
|
75
|
-
if mm < 1:
|
|
76
|
-
acker[-1] = acker.pop() + 1
|
|
77
|
-
elif nn < 1:
|
|
78
|
-
acker[-2] = acker[-2] - 1
|
|
79
|
-
acker[-1] = 1
|
|
80
|
-
else:
|
|
81
|
-
acker[-2] = mm - 1
|
|
82
|
-
acker[-1] = mm
|
|
83
|
-
acker.append(nn - 1)
|
|
84
|
-
return acker[0]
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
def fibonacci_generator(fib0: int = 0, fib1: int = 1) -> Iterator[int]:
|
|
88
|
-
"""Fibonacci iterator.
|
|
89
|
-
|
|
90
|
-
Generate a Fibonacci sequence instead of recursively evaluating it.
|
|
91
|
-
|
|
92
|
-
- returns an iterator to a Fibonacci sequence
|
|
93
|
-
- beginning fib0, fib1, fib0+fib1, ...
|
|
94
|
-
- default yields 0, 1, 1. 2, 3, 5, 8, 13, ...
|
|
95
|
-
|
|
96
|
-
"""
|
|
97
|
-
while True:
|
|
98
|
-
yield fib0
|
|
99
|
-
fib0, fib1 = fib1, fib0 + fib1
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
def rev_fibonacci_generator(fib0: int = 0, fib1: int = 1) -> Iterator[int]:
|
|
103
|
-
"""Reverse Fibonacci iterator.
|
|
104
|
-
|
|
105
|
-
Generate a reverse Fibonacci sequence instead of recursively evaluating it.
|
|
106
|
-
|
|
107
|
-
- Returns iterator iterating over the Fibonacci sequence in reverse order
|
|
108
|
-
- beginning fib1, fib0, fib1-fib0, ...
|
|
109
|
-
- default yields 1, -1, 2, -3, 5, -8, 13, ...
|
|
110
|
-
|
|
111
|
-
"""
|
|
112
|
-
while True:
|
|
113
|
-
fib0, fib1 = fib1, fib0 - fib1
|
|
114
|
-
yield fib0
|
|
@@ -1,50 +0,0 @@
|
|
|
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.recursive_functions.examples import ackermann_list
|
|
16
|
-
|
|
17
|
-
class Test_ackerman:
|
|
18
|
-
def test_ack_list(self) -> None:
|
|
19
|
-
assert ackermann_list(0, 0) == 1
|
|
20
|
-
assert ackermann_list(0, 5) == 6
|
|
21
|
-
assert ackermann_list(1, 0) == 2
|
|
22
|
-
assert ackermann_list(1, 1) == 3
|
|
23
|
-
assert ackermann_list(1, 2) == 4
|
|
24
|
-
assert ackermann_list(1, 3) == 5
|
|
25
|
-
assert ackermann_list(1, 4) == 6
|
|
26
|
-
assert ackermann_list(1, 5) == 7
|
|
27
|
-
assert ackermann_list(1, 6) == 8
|
|
28
|
-
assert ackermann_list(1, 7) == 9
|
|
29
|
-
assert ackermann_list(1, 8) == 10
|
|
30
|
-
assert ackermann_list(1, 27) == 29 # inferring from patterns
|
|
31
|
-
assert ackermann_list(2, 0) == 3
|
|
32
|
-
assert ackermann_list(2, 1) == 5
|
|
33
|
-
assert ackermann_list(2, 2) == 7
|
|
34
|
-
assert ackermann_list(2, 3) == 9
|
|
35
|
-
assert ackermann_list(2, 4) == 11
|
|
36
|
-
assert ackermann_list(2, 5) == 13
|
|
37
|
-
assert ackermann_list(2, 6) == 15
|
|
38
|
-
assert ackermann_list(2, 13) == 29 # inferring from patterns
|
|
39
|
-
assert ackermann_list(3, 0) == 5
|
|
40
|
-
assert ackermann_list(3, 1) == 13
|
|
41
|
-
assert ackermann_list(3, 2) == 29 # inferring from patterns
|
|
42
|
-
assert ackermann_list(3, 3) == 61 # inferring from patterns
|
|
43
|
-
assert ackermann_list(3, 4) == 125
|
|
44
|
-
assert ackermann_list(3, 8) == 2045 # not hand computed
|
|
45
|
-
assert ackermann_list(4, 0) == 13
|
|
46
|
-
# assert ackermann(4, 1) == 65533 # not hand computed!
|
|
47
|
-
assert ackermann_list(0, 21) == 22
|
|
48
|
-
assert ackermann_list(4, 0) == ackermann_list(3, 1)
|
|
49
|
-
assert ackermann_list(3, 7) == ackermann_list(2, ackermann_list(3, 6))
|
|
50
|
-
|
|
@@ -1,68 +0,0 @@
|
|
|
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.recursive_functions.examples import fibonacci_generator, rev_fibonacci_generator
|
|
16
|
-
|
|
17
|
-
class Test_fibonacci:
|
|
18
|
-
def test_fib(self) -> None:
|
|
19
|
-
someFibs: list[int] = []
|
|
20
|
-
fibs = fibonacci_generator()
|
|
21
|
-
fib = next(fibs)
|
|
22
|
-
while(fib < 60):
|
|
23
|
-
someFibs.append(fib)
|
|
24
|
-
fib = next(fibs)
|
|
25
|
-
assert someFibs == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
|
|
26
|
-
|
|
27
|
-
someFibs = []
|
|
28
|
-
fib0 = 1
|
|
29
|
-
fib1 = 1
|
|
30
|
-
fibs = fibonacci_generator(fib0, fib1)
|
|
31
|
-
fib = next(fibs)
|
|
32
|
-
while(fib < 90):
|
|
33
|
-
someFibs.append(fib)
|
|
34
|
-
fib = next(fibs)
|
|
35
|
-
assert someFibs == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
|
36
|
-
|
|
37
|
-
someFibs = []
|
|
38
|
-
fib0 = 1
|
|
39
|
-
fib1 = 1
|
|
40
|
-
fibs = rev_fibonacci_generator(fib0, fib1)
|
|
41
|
-
for n in range(10):
|
|
42
|
-
fib = next(fibs)
|
|
43
|
-
someFibs.append(fib)
|
|
44
|
-
assert someFibs == [1, 0, 1, -1, 2, -3, 5, -8, 13, -21]
|
|
45
|
-
|
|
46
|
-
someFibs = []
|
|
47
|
-
fibs = rev_fibonacci_generator()
|
|
48
|
-
for n in range(10):
|
|
49
|
-
fib = next(fibs)
|
|
50
|
-
someFibs.append(fib)
|
|
51
|
-
assert someFibs == [1, -1, 2, -3, 5, -8, 13, -21, 34, -55]
|
|
52
|
-
|
|
53
|
-
someFibs = []
|
|
54
|
-
fibs = rev_fibonacci_generator(5, 3)
|
|
55
|
-
for _ in range(12):
|
|
56
|
-
fib = next(fibs)
|
|
57
|
-
someFibs.append(fib)
|
|
58
|
-
assert someFibs == [3, 2, 1, 1, 0, 1, -1, 2, -3, 5, -8, 13]
|
|
59
|
-
|
|
60
|
-
someFibs = []
|
|
61
|
-
fibs = fibonacci_generator(-55, 34)
|
|
62
|
-
fib = next(fibs)
|
|
63
|
-
while(fib < 90):
|
|
64
|
-
someFibs.append(fib)
|
|
65
|
-
fib = next(fibs)
|
|
66
|
-
assert someFibs == [-55, 34, -21, 13, -8, 5, -3, 2, -1, 1, 0,
|
|
67
|
-
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
|
|
68
|
-
|
|
File without changes
|
|
File without changes
|