Execute a tail recursive function iteratively
Project description
tailrec
Provides a decorator tailrec which executes a tail recursive function iteratively
Installation
pip install tailrec
Examples
from tailrec import tailrec
@tailrec
def factorial(n: int, accum: int = 1) -> int:
"""Calculates n!"""
if n == 0:
return accum
else:
return factorial(n - 1, accum * n)
@tailrec
def fibonacci(n: int, current: int = 0, next_: int = 1) -> int:
"""Returns the n-th number of the fibonacci sequence"""
if n == 0:
return current
else:
return fibonacci(n - 1, next_, current + next_)
print(factorial(5)) # 120
print(factorial(1_100)) # 5343708488092637703...
print(fibonacci(5)) # 8
print(fibonacci(2_000)) # 42246963333923...
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tailrec-0.1.4.tar.gz
(2.6 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tailrec-0.1.4.tar.gz.
File metadata
- Download URL: tailrec-0.1.4.tar.gz
- Upload date:
- Size: 2.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.3 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2626e3f16c2223c375ecb73930b1baadb86490ff202aff35a8c0dc7ce23efcc8
|
|
| MD5 |
0250f214213dd830daaeb748c68a4553
|
|
| BLAKE2b-256 |
5dbe936d6d4792a60437a2cda314e843425136f4b6c3c8c346ed6741c7da1dbc
|
File details
Details for the file tailrec-0.1.4-py3-none-any.whl.
File metadata
- Download URL: tailrec-0.1.4-py3-none-any.whl
- Upload date:
- Size: 3.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.1 CPython/3.11.3 Windows/10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
02ccebdd250519c09e36d1a8868dc696e6b39e560ac8fd819332f0c0a90d4c4e
|
|
| MD5 |
46fd5819589d522add1f7dd6631f07d6
|
|
| BLAKE2b-256 |
10effd744cba9eb4b7e3241b172b2f01426c3ebd4bcab47410b79a9c497b9c06
|