boring-math.recursive-functions 0.8.1__tar.gz → 1.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.
Files changed (21) hide show
  1. boring_math_recursive_functions-1.0.0/.gitignore +7 -0
  2. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/CHANGELOG.md +11 -12
  3. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/PKG-INFO +8 -7
  4. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/README.rst +3 -3
  5. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/pyproject.toml +17 -7
  6. boring_math_recursive_functions-1.0.0/src/boring_math/recursive_functions/__init__.py +59 -0
  7. boring_math_recursive_functions-1.0.0/src/boring_math/recursive_functions/ackermann.py +53 -0
  8. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/src/boring_math/recursive_functions/cli/__init__.py +6 -3
  9. boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/cli/ackermann.py → boring_math_recursive_functions-1.0.0/src/boring_math/recursive_functions/cli/ackermann_cli.py +10 -12
  10. boring_math_recursive_functions-1.0.0/src/boring_math/recursive_functions/cli/fibonacci_cli.py +75 -0
  11. boring_math_recursive_functions-1.0.0/src/boring_math/recursive_functions/fibonacci.py +71 -0
  12. boring_math_recursive_functions-0.8.1/tests/test_ackermann.py → boring_math_recursive_functions-1.0.0/tests/ackermann/test_ackermann_list.py +1 -1
  13. boring_math_recursive_functions-1.0.0/tests/fibonacci/test_fibonacci_float.py +125 -0
  14. boring_math_recursive_functions-1.0.0/tests/fibonacci/test_fibonacci_int.py +87 -0
  15. boring_math_recursive_functions-0.8.1/.gitignore +0 -11
  16. boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/__init__.py +0 -50
  17. boring_math_recursive_functions-0.8.1/src/boring_math/recursive_functions/examples.py +0 -114
  18. boring_math_recursive_functions-0.8.1/tests/test_fibonacci.py +0 -68
  19. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/LICENSE +0 -0
  20. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/src/boring_math/recursive_functions/cli/py.typed +0 -0
  21. {boring_math_recursive_functions-0.8.1 → boring_math_recursive_functions-1.0.0}/src/boring_math/recursive_functions/py.typed +0 -0
@@ -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
@@ -14,29 +14,28 @@ See [Semantic Versioning 2.0.0](https://semver.org).
14
14
 
15
15
  ## Releases and other important milestones
16
16
 
17
- ### Version 0.8.0 - PyPI release date 2025-08-04
17
+ ### Version 1.0.0 - PyPI release date 2025-10-11
18
18
 
19
- Infrastructure updated, now similar to pythonic-fp projects.
19
+ Completely rewrote Fibonacci library and created cli implementation.
20
20
 
21
- First version released under boring-math namespace.
21
+ Docs now use Sphinx.
22
22
 
23
- ### Version 0.7.0 - PyPI release date 2025-07-31
23
+ ### Version 0.8.1 - PyPI release date 2025-08-04
24
24
 
25
- First version released under boring-math namespace.
25
+ Infrastructure updated, now similar to Pythonic FP projects.
26
26
 
27
- New PyPI name is `boring-math.recursive-functions
27
+ ### Version 0.7.1 - PyPI release date 2025-07-31
28
28
 
29
- ### Version 0.6.1 - PyPI release date 2025-07-14
29
+ New PyPI name is `boring-math-recursive-functions
30
30
 
31
- Fixed munged CHANGELOG and Documentation links for PyPI.
31
+ ### Version 0.6.1 - PyPI release date 2025-07-14
32
32
 
33
- ### Version 0.6.0 - PyPI release date 2025-07-14
33
+ First (correct) version to use boring-math.recursive-functions
34
34
 
35
- - First version to use pythonic_fp namespace
36
- - no longer using dtools namespace packages
35
+ - fixed munged CHANGELOG and Documentation links.
37
36
 
38
37
  ### Version 0.5.0 - PyPI release date 2025-01-??
39
38
 
40
39
  - Part of the Boring Math projects
41
- - PyPI **bm.** namespace
40
+ - PyPI bm.recursive-functions
42
41
  - Migrated code from grscheller/boring-math repo
@@ -1,10 +1,10 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: boring-math-recursive-functions
3
- Version: 0.8.1
3
+ Version: 1.0.0
4
4
  Summary: Recursive Functions Package
5
5
  Keywords: ackermann,fibonacci,recursive,recursive functions
6
6
  Author-email: "Geoffrey R. Scheller" <geoffrey@scheller.com>
7
- Requires-Python: >=3.12
7
+ Requires-Python: >=3.13
8
8
  Description-Content-Type: text/x-rst
9
9
  Classifier: Development Status :: 3 - Alpha
10
10
  Classifier: Framework :: Pytest
@@ -14,10 +14,11 @@ Classifier: Operating System :: OS Independent
14
14
  Classifier: Programming Language :: Python :: 3.13
15
15
  Classifier: Typing :: Typed
16
16
  License-File: LICENSE
17
+ Requires-Dist: pythonic-fp-iterables>=5.1.2
17
18
  Requires-Dist: pytest>=8.4.1 ; extra == "test"
18
19
  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
20
+ Project-URL: Documentation, https://grscheller.github.io/boring-math/recursive-functions/development/build/html
21
+ Project-URL: Homepage, https://grscheller.github.io/boring-math/homepage/build/html
21
22
  Project-URL: Source, https://github.com/grscheller/boring-math-recursive-functions
22
23
  Provides-Extra: test
23
24
 
@@ -36,9 +37,9 @@ not yet defined values. Functions defined using previously well defined values
36
37
  having "base cases" are called "primitively recursive functions." Not all
37
38
  computable functions are primitively recursive.
38
39
 
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>`_
40
+ This pypi project is part of the
41
+ `boring math
42
+ <https://grscheller.github.io/boring-math>`_ projects.
42
43
 
43
44
  Documentation
44
45
  -------------
@@ -13,9 +13,9 @@ not yet defined values. Functions defined using previously well defined values
13
13
  having "base cases" are called "primitively recursive functions." Not all
14
14
  computable functions are primitively recursive.
15
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>`_
16
+ This pypi project is part of the
17
+ `boring math
18
+ <https://grscheller.github.io/boring-math>`_ projects.
19
19
 
20
20
  Documentation
21
21
  -------------
@@ -1,8 +1,8 @@
1
1
  [project]
2
2
  name = "boring-math-recursive-functions"
3
- version = "0.8.1"
3
+ version = "1.0.0"
4
4
  readme = "README.rst"
5
- requires-python = ">=3.12"
5
+ requires-python = ">=3.13"
6
6
  license = { file = "LICENSE" }
7
7
  authors = [{ name = "Geoffrey R. Scheller", email = "geoffrey@scheller.com" }]
8
8
  keywords = [
@@ -20,17 +20,20 @@ classifiers = [
20
20
  "Programming Language :: Python :: 3.13",
21
21
  "Typing :: Typed",
22
22
  ]
23
- dependencies = []
23
+ dependencies = [
24
+ "pythonic-fp-iterables>=5.1.2",
25
+ ]
24
26
  dynamic = ["description"]
25
27
 
26
28
  [project.urls]
27
29
  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
+ Documentation = "https://grscheller.github.io/boring-math/recursive-functions/development/build/html"
31
+ Homepage = "https://grscheller.github.io/boring-math/homepage/build/html"
30
32
  Source = "https://github.com/grscheller/boring-math-recursive-functions"
31
33
 
32
34
  [project.scripts]
33
- ackermann = "boring_math.recursive_functions.cli.ackermann:ackermann_cli"
35
+ ackermann_list = "boring_math.recursive_functions.cli.ackermann_cli:ackermann_list_cli"
36
+ fibonacci = "boring_math.recursive_functions.cli.fibonacci_cli:fibonacci"
34
37
 
35
38
  [project.optional-dependencies]
36
39
  test = [
@@ -45,9 +48,11 @@ build-backend = "flit_core.buildapi"
45
48
  name = "boring_math.recursive_functions"
46
49
 
47
50
  [tool.mypy]
48
- enable_incomplete_feature = ["NewGenericSyntax"]
51
+ enable_incomplete_feature = "NewGenericSyntax"
52
+ explicit_package_bases = true
49
53
  implicit_reexport = false
50
54
  local_partial_types = true
55
+ namespace_packages = true
51
56
  warn_redundant_casts = true
52
57
  warn_return_any = true
53
58
  warn_unused_configs = true
@@ -55,6 +60,7 @@ warn_unused_configs = true
55
60
  [tool.pylsp-mypy]
56
61
  enabled = true
57
62
  live-mode = true
63
+ dmypy = false
58
64
  strict = true
59
65
  report_progress = true
60
66
 
@@ -69,6 +75,10 @@ target-version = "py313"
69
75
  [tool.ruff.lint.flake8-quotes]
70
76
  docstring-quotes = "double"
71
77
 
78
+ [tool.ruff.lint.per-file-ignores]
79
+ "tests/*" = ["C901"]
80
+ "**/*.py" = ["E741"]
81
+
72
82
  [tool.ruff.format]
73
83
  quote-style = "single"
74
84
  docstring-code-line-length = 72
@@ -0,0 +1,59 @@
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
+ 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
+
32
+ ``ackermann(m,0) = ackermann(m-1,1)`` for ``m >= 0``
33
+
34
+ ``ackermann(m,n) = ackermann(m-1, ackermann(m,n-1))`` for ``m, n > 0``
35
+
36
+ Fibonacci sequences
37
+ -------------------
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-2025 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
+ Ackermann examples
17
+ ==================
18
+
19
+ Example implementations for Ackermann's function.
20
+
21
+ """
22
+ __all__ = ['ackermann_list']
23
+
24
+
25
+ def ackermann_list(m: int, n: int) -> int:
26
+ """Ackermann's Function.
27
+
28
+ .. note::
29
+
30
+ This implementation models the recursion with a Python list
31
+ instead of Python's "call stack". It then evaluates the
32
+ innermost ackermann function first. To naively use call stack
33
+ recursion would result in the loss of stack safety.
34
+
35
+ :param m: First argument to Ackermann's function.
36
+ :param n: Second argument to Ackermann's function.
37
+ :returns: A very hard to calculate useless value.
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]
@@ -23,10 +23,13 @@ section in the project's pyproject.toml file.
23
23
  Example applications
24
24
  --------------------
25
25
 
26
- - Ackermann program: ackermann
26
+ - Ackermann's function
27
27
 
28
- - boring_math.recursive_functions.cli.ackermann
28
+ - **ackermann_list:** Calculate values for a computable but not simple
29
+ recursive function.
29
30
 
30
- - uses a Python list to implement recursion
31
+ - Fibonacci sequences
32
+
33
+ - **fibonacci:** Generate Fibonacci sequences both forward and backward.
31
34
 
32
35
  """
@@ -13,35 +13,33 @@
13
13
  # limitations under the License.
14
14
 
15
15
  """
16
- ### Programs to evaluate Ackermann's function
16
+ Programs to evaluate Ackermann's function.
17
17
 
18
18
  .. note::
19
+
19
20
  The version of the Ackermann's function being used is defined recursively by
20
21
 
21
- ackermann(0,n) = n+1 for n >= 0
22
- ackermann(m,0) = ackermann(m-1,1) for m >= 0
23
- ackermann(m,n) = ackermann(m-1, ackermann(m, n-1)) for m,n > 0
22
+ - ``ackermann(0,n) = n+1 for n >= 0``
23
+ - ``ackermann(m,0) = ackermann(m-1,1) for m >= 0``
24
+ - ``ackermann(m,n) = ackermann(m-1, ackermann(m, n-1)) for m,n > 0``
24
25
 
25
26
  Ackermann's function is an example of a computable but not primitively
26
27
  recursive function.
27
28
 
28
- - program **ackermann** uses a Python List to implement recursion
29
+ - **ackermann_list:** Computes Ackermann's function by simulating recursion with a list.
29
30
 
30
31
  """
31
32
 
32
- from __future__ import annotations
33
-
34
33
  import sys
35
- from boring_math.recursive_functions.examples import ackermann_list
34
+ from boring_math.recursive_functions.ackermann import ackermann_list
36
35
 
37
36
 
38
- def ackermann_cli() -> None:
37
+ def ackermann_list_cli() -> None:
39
38
  """Evaluate Ackermann's function simulating recursion with a Python list.
40
39
 
41
- Usage: ``ackermann m n``
40
+ Usage: ``ackermann_list m n``
42
41
 
43
- - gets installed into the virtual environment
44
- - becomes numerically intractable after m=4 n=1
42
+ Becomes numerically intractable after m=4 n=1.
45
43
 
46
44
  """
47
45
  args = sys.argv[1:]
@@ -0,0 +1,75 @@
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
+ Programs involving Fibonacci sequences. These are installed into the
17
+ Python virtual environment.
18
+
19
+ - **fibonacci:** Prints generated Fibonacci sequences to stdout.
20
+
21
+ """
22
+
23
+ import sys
24
+ from collections.abc import Callable, Iterator
25
+ from typing import cast
26
+ from boring_math.recursive_functions.fibonacci import orderable_generator
27
+ from pythonic_fp.iterables.drop_take import take
28
+
29
+
30
+ def fibonacci() -> None:
31
+ """
32
+ Calculate Fibonacci sequences both forward ans backwards.
33
+
34
+ Usage: ``fibonacci [-r] f1 f2 n``
35
+
36
+ - gets installed into the virtual environment
37
+
38
+
39
+ """
40
+ nargs = len(args := sys.argv[1:])
41
+ if nargs < 3 or nargs > 4:
42
+ print('Error: Wrong number of arguments given', file=sys.stderr)
43
+ sys.exit(1)
44
+ if nargs == 3:
45
+ try:
46
+ fib1 = int(args[0])
47
+ fib2 = int(args[1])
48
+ n = int(args[2])
49
+ fib_gen = cast(
50
+ Callable[[int, int], Iterator[int]],
51
+ lambda f1, f2: orderable_generator(f1, f2, forward=True),
52
+ )
53
+ except ValueError:
54
+ print('Error: Non-integer argument given', file=sys.stderr)
55
+ sys.exit(1)
56
+ if nargs == 4:
57
+ match args[0]:
58
+ case '-r':
59
+ try:
60
+ fib1 = int(args[1])
61
+ fib2 = int(args[2])
62
+ n = int(args[3])
63
+ fib_gen = cast(
64
+ Callable[[int, int], Iterator[int]],
65
+ lambda f1, f2: orderable_generator(f1, f2, forward=False),
66
+ )
67
+ except ValueError:
68
+ print('Error: Non-integer argument given', file=sys.stderr)
69
+ sys.exit(1)
70
+ case opt:
71
+ print(f'Error: Unknown option "{opt}" given', file=sys.stderr)
72
+ sys.exit(1)
73
+
74
+ for fib in take(fib_gen(fib1, fib2), abs(n)):
75
+ print(fib)
@@ -0,0 +1,71 @@
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
+ Fibonacci examples
17
+ ==================
18
+
19
+ Example implementations for Fibonacci functions.
20
+
21
+ """
22
+
23
+ from collections.abc import Iterator
24
+ from typing import Protocol, Self
25
+
26
+ __all__ = ['orderable_generator']
27
+
28
+
29
+ class Orderable(Protocol):
30
+ def __lt__(self: Self, other: Self) -> bool: ...
31
+
32
+
33
+ class Ring(Protocol):
34
+ def __add__(self: Self, other: Self) -> Self: ...
35
+ def __sub__(self: Self, other: Self) -> Self: ...
36
+ def __mult__(self: Self, other: Self) -> Self: ...
37
+ def __mod__(self: Self, other: Self) -> Self: ...
38
+
39
+
40
+ class OrderedRing(Orderable, Ring, Protocol):
41
+ def __mod__(self: Self, other: Self) -> Self: ...
42
+
43
+
44
+ def orderable_generator[T: OrderedRing](
45
+ fib0: T, fib1: T, forward: bool = True
46
+ ) -> Iterator[T]:
47
+ """
48
+ Generate a Fibonacci or reverse Fibonacci sequence.
49
+
50
+ - ``fib0=0, fib1=1`` generates
51
+
52
+ - the sequence ``0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89 ...``
53
+
54
+ - ``fib0=5, fib1=3, forward = False`` generates
55
+
56
+ - the sequence ``5, 3, 2, 1, 1, 0, 1, -1, 2, -3, 5, -8, ...``
57
+
58
+ :param fib0: Zeroth numeric element of the sequence.
59
+ :param fib1: Next numeric element of the sequence.
60
+ :param reverse: Generate sequence in reverse order.
61
+ :returns: An iterator iterating over a Fibonacci sequence.
62
+
63
+ """
64
+ if forward:
65
+ while True:
66
+ yield fib0
67
+ fib0, fib1 = fib1, fib0 + fib1
68
+ else:
69
+ while True:
70
+ yield fib0
71
+ fib0, fib1 = fib1, fib0 - fib1
@@ -12,7 +12,7 @@
12
12
  # See the License for the specific language governing permissions and
13
13
  # limitations under the License.
14
14
 
15
- from boring_math.recursive_functions.examples import ackermann_list
15
+ from boring_math.recursive_functions.ackermann import ackermann_list
16
16
 
17
17
  class Test_ackerman:
18
18
  def test_ack_list(self) -> None:
@@ -0,0 +1,125 @@
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 collections.abc import Iterator, Callable
16
+ from math import sqrt
17
+ from typing import cast
18
+ from boring_math.recursive_functions.fibonacci import orderable_generator
19
+
20
+ fib_gen = cast(
21
+ Callable[[float, float], Iterator[float]],
22
+ lambda n, m: orderable_generator(n, m, forward=True),
23
+ )
24
+
25
+ fib_rev_gen = cast(
26
+ Callable[[float, float], Iterator[float]],
27
+ lambda n, m: orderable_generator(n, m, forward=False),
28
+ )
29
+
30
+
31
+ class TestFibonacciFloat:
32
+ def test_int(self) -> None:
33
+ float_fibs: list[float] = []
34
+ fibs = fib_gen(0.0, 1.0)
35
+ fib = next(fibs)
36
+ while fib < 60.0:
37
+ float_fibs.append(fib)
38
+ fib = next(fibs)
39
+ assert float_fibs == [0.0, 1.0, 1.0, 2.0, 3.0, 5.0, 8.0, 13.0, 21.0, 34.0, 55.0]
40
+
41
+ float_fibs = []
42
+ fibs = fib_gen(0.25, 0.5)
43
+ fib = next(fibs)
44
+ while fib < 25.0:
45
+ float_fibs.append(fib)
46
+ fib = next(fibs)
47
+ exact_float_fibs = [0.25, 0.5, 0.75, 1.25, 2.0, 3.25, 5.25, 8.5, 13.75, 22.25]
48
+ for nn in range(10):
49
+ assert abs(float_fibs[nn] - exact_float_fibs[nn]) < 0.000001
50
+
51
+ float_fibs = []
52
+ fibs = fib_rev_gen(8.5, 5.25)
53
+ fib = next(fibs)
54
+ while fib > 0.0000025:
55
+ float_fibs.append(fib)
56
+ fib = next(fibs)
57
+ exact_float_fibs = [8.5, 5.25, 3.25, 2.0, 1.25, 0.75, 0.5, 0.25, 0.25]
58
+ assert len(float_fibs) == 9
59
+ for nn in range(9):
60
+ assert abs(float_fibs[nn] - exact_float_fibs[nn]) < 0.000001
61
+
62
+ class TestFibonacciRatio:
63
+ def test_golden_ratio_1(self) -> None:
64
+ phi = (1 + sqrt(5))/2
65
+
66
+ fibs = fib_gen(1.0, 1.0)
67
+
68
+ loops, ratio, fib_numerator = 0, -1.0, next(fibs)
69
+ while loops < 100:
70
+ loops += 1
71
+ fib_denominator, fib_numerator = fib_numerator, next(fibs)
72
+ ratio = fib_numerator / fib_denominator
73
+ if abs(ratio - phi) < 0.0000025:
74
+ break
75
+
76
+ err = abs(ratio - phi)
77
+ assert err < 0.0000025
78
+
79
+ assert loops < 100
80
+ assert loops < 50
81
+ assert loops < 25
82
+ assert loops < 20
83
+
84
+ def test_golden_ratio_2(self) -> None:
85
+ phi = (1 + sqrt(5))/2
86
+
87
+ fibs = fib_gen(-182.413, 142.1215)
88
+
89
+ loops, ratio, fib_numerator = 0, -1.0, next(fibs)
90
+ while loops < 100:
91
+ loops += 1
92
+ fib_denominator, fib_numerator = fib_numerator, next(fibs)
93
+ ratio = fib_numerator / fib_denominator
94
+ if abs(ratio - phi) < 0.0000025:
95
+ break
96
+
97
+ err = abs(ratio - phi)
98
+ assert err < 0.0000025
99
+
100
+ assert loops < 100
101
+ assert loops < 50
102
+ assert loops < 25
103
+ assert loops < 20
104
+
105
+ def test_golden_ratio_3(self) -> None:
106
+ phi = (1 + sqrt(5))/2
107
+ neg_inv_phi = -1.0/phi
108
+
109
+ fibs = fib_rev_gen(1.2015, 0.8203)
110
+
111
+ loops, ratio, fib_denominator = 0, -1.0, next(fibs)
112
+ while loops < 100:
113
+ loops += 1
114
+ fib_numerator, fib_denominator = fib_denominator, next(fibs)
115
+ ratio = fib_numerator / fib_denominator
116
+ if abs(ratio - neg_inv_phi) < 0.0000025:
117
+ break
118
+
119
+ err = abs(ratio - neg_inv_phi)
120
+ assert err < 0.0000025
121
+
122
+ assert loops < 100
123
+ assert loops < 50
124
+ assert loops < 25
125
+ assert loops < 20
@@ -0,0 +1,87 @@
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 collections.abc import Callable, Iterator
16
+ from typing import cast
17
+ from boring_math.recursive_functions.fibonacci import orderable_generator
18
+
19
+ fib_gen = cast(
20
+ Callable[[int, int], Iterator[int]],
21
+ lambda n, m: orderable_generator(n, m, forward=True),
22
+ )
23
+
24
+ fib_rev_gen = cast(
25
+ Callable[[int, int], Iterator[int]],
26
+ lambda n, m: orderable_generator(n, m, forward=False),
27
+ )
28
+
29
+
30
+ class Test_fibonacci_int:
31
+ def test_int(self) -> None:
32
+ int_fibs: list[int] = []
33
+ fibs = fib_gen(0, 1)
34
+ fib = next(fibs)
35
+ while fib < 60:
36
+ int_fibs.append(fib)
37
+ fib = next(fibs)
38
+ assert int_fibs == [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55]
39
+
40
+ int_fibs = []
41
+ fib0 = 1
42
+ fib1 = 1
43
+ fibs = fib_gen(fib0, fib1)
44
+ fib = next(fibs)
45
+ while fib < 90:
46
+ int_fibs.append(fib)
47
+ fib = next(fibs)
48
+ assert int_fibs == [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
49
+
50
+ int_fibs = []
51
+ fib0 = 1
52
+ fib1 = 1
53
+ fibs = fib_rev_gen(fib0, fib1)
54
+ for _ in range(11):
55
+ fib = next(fibs)
56
+ int_fibs.append(fib)
57
+ assert int_fibs == [1, 1, 0, 1, -1, 2, -3, 5, -8, 13, -21]
58
+
59
+ int_fibs = []
60
+ fibs = fib_rev_gen(1, -1)
61
+ for _ in range(10):
62
+ fib = next(fibs)
63
+ int_fibs.append(fib)
64
+ assert int_fibs == [1, -1, 2, -3, 5, -8, 13, -21, 34, -55]
65
+
66
+ int_fibs = []
67
+ fibs = fib_gen(5, 3)
68
+ for _ in range(9):
69
+ fib = next(fibs)
70
+ int_fibs.append(fib)
71
+ assert int_fibs == [5, 3, 8, 11, 19, 30, 49, 79, 128]
72
+
73
+ int_fibs = []
74
+ fibs = fib_gen(-21, 13)
75
+ fib = next(fibs)
76
+ while fib < 15:
77
+ int_fibs.append(fib)
78
+ fib = next(fibs)
79
+ assert int_fibs == [-21, 13, -8, 5, -3, 2, -1, 1, 0, 1, 1, 2, 3, 5, 8, 13]
80
+
81
+ int_fibs = []
82
+ fibs = fib_gen(20, -14)
83
+ fib = next(fibs)
84
+ while fib > -35:
85
+ int_fibs.append(fib)
86
+ fib = next(fibs)
87
+ assert int_fibs == [20, -14, 6, -8, -2, -10, -12, -22, -34]
@@ -1,11 +0,0 @@
1
- # minimal version - only add to when necessary
2
- **/__pycache__/
3
-
4
- # testing
5
- .pytest_cache/
6
-
7
- # distribution/packaging
8
- dist/
9
-
10
- # tooling
11
- .mypy_cache
@@ -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,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
-