Skip to main content

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.6.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tailrec-0.1.6-py3-none-any.whl (5.0 kB view details)

Uploaded Python 3

File details

Details for the file tailrec-0.1.6.tar.gz.

File metadata

  • Download URL: tailrec-0.1.6.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.3 Windows/10

File hashes

Hashes for tailrec-0.1.6.tar.gz
Algorithm Hash digest
SHA256 9405770a13c110c4832a488a13267741b353afef3c1e7b1f3c279099e0eba7e2
MD5 63e271e5c9acdf3d1b60c832f1568dad
BLAKE2b-256 e07e8709c06869be11f08d5d81b5a74eb6c4386a520f1d20935fe44c7782f718

See more details on using hashes here.

File details

Details for the file tailrec-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: tailrec-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 5.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.1.1 CPython/3.11.3 Windows/10

File hashes

Hashes for tailrec-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 f51c36117c72b75f6062233860d340ca9382a09a6ff22b4abef4625f2b0821d2
MD5 697d0608996102eec9220460f52e2b94
BLAKE2b-256 895679d89e68a91f10876b143076ed4457a56547c83ebab39ee0e6fe84356cd3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page