Skip to main content

Convert integers to and from the factoradic number system

Project description

The factorial number system (also known as factoradic) is a way of representing an integer as the sum of multiples of factorials. All integers have a unique representation in the factoradic number system. For example, the number 1337 can be represented as:

1*6! + 5*5! + 0*4! + 2*3! + 2*2! + 1*1! + 0*0!

with coefficients 1 5 0 2 2 1 0. This is the unqiue factoradic representation of decimal 1337.

Factoradic numbers have uses in combinatorics, particularly in the numbering of permutations. This factoradic library is useful for converting to and from factoradic number representations both in Python and from the command-line.

Status

https://travis-ci.org/rob-smallshire/factoradic.svg?branch=master https://coveralls.io/repos/github/rob-smallshire/factoradic/badge.svg?branch=master

Installation

The factoradic package is available on the Python Package Index (PyPI):

https://badge.fury.io/py/factoradic.svg

The package supports Python 3 only. To install:

$ pip install factoradic

Python Interface

For full help:

>>> import factoradic
>>> help(factoradic)

In the meantime, here are some highlights.

To convert from an integer to factoradic use to_factoradic():

>>> from factoradic import to_factoradic
>>> factoradic.to_factoradic(1337)
[0, 1, 2, 2, 0, 5, 1]

The result is the list of coefficients where the factorial of each zero-based index gives a place value, and the item at that index is the coefficient by with the place value is to be multiplied. The elements are from least-significant to most-significant. Since the coefficient at any index must be less that or equal to the index, the coefficient at index 0 is always 0.

To convert from factoradic use from_factoradic():

>>> from factoradic import from_factoradic
>>> factoradic.from_factoradic([0, 1, 2, 2, 0, 5, 1])
1337

Command-Line Interface

There’s also a handy command-line interface. Run factoradic --help to see a list of commands:

$ factoradic --help
Factoradic.

Convert to and from the factorial number system.

Usage:
  factoradic from-integer <integer> [--expression]
  factoradic to-integer <coefficient-0> [<coefficient-1> [<coefficient-n>...]]

Options:
  -e --expression  Show as a mathematical expression.

To convert from an integer to factoradic, use the from-integer subcommand:

$ factoradic from-integer 1729
0 1 0 0 2 2 2

The coefficients are reported from least-significant to most-significant. The see the results as a math expression, specify the --expression flag:

$ factoradic from-integer 1729 --expression
2*6! + 2*5! + 2*4! + 0*3! + 0*2! + 1*1! + 0*0!

To convert from factoradic representation use the to-integer subcommand, specifying the coefficients from least-significant to most-significant:

$ factoradic to-integer 0 1 0 0 2 2 2
1729

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

factoradic-1.0.2.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

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

factoradic-1.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file factoradic-1.0.2.tar.gz.

File metadata

  • Download URL: factoradic-1.0.2.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for factoradic-1.0.2.tar.gz
Algorithm Hash digest
SHA256 4555fdd75b8b5ebf29a64498d98d77528f85576daf2eb01d2dee27a927a81d80
MD5 36b09c567d5d80a04ddfd5c63b1604bc
BLAKE2b-256 94ca60d5909275ee184a0d4682b0fb02198551eaf3e1e3cadc2911d63bc44e43

See more details on using hashes here.

File details

Details for the file factoradic-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for factoradic-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0bfb7c2e68b28ef8ee9e13efa6e5e969b3bb8f7c76e4b7494f1153e21a6f0750
MD5 62f7ccbc95399f4a6a3d78c3ca369458
BLAKE2b-256 bbe2c7de146f80024dd1a6eb4a74f96e27e57b4bb79536c08a6becd59ac1a05a

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