Git Product home page Git Product logo

combinadics's Introduction

Combinadics - Combinatorial number system for Javascript and Swift

This library is available in both JavaScript and Swift versions, providing encoding and decoding functions based on the combinatorial number system also known as combinadics. It allows you to encode an nCk combination (an array of numbers) into an integer and vice-versa.

The combinatorial number system is a compact integer representation of a combination, making it useful in algorithms. It allows for memory-efficient storage and retrieval of combinatorial objects, reducing space requirements in applications that handle large sets of combinations. Hashing and Indexing can then be implemented, for example when the combination is associated with some data.

Online Demo

The decode(N, k) function is available at: https://planetcalc.com/8592/

JavaScript Version

To use the JavaScript version of the "combinadics" library, you can simply include the js/combinadics.js file.

Swift Version

The Swift version of the "combinadics" library is located in swift/combinadics.swift

Usage

Once you have integrated the "combinadics" library into your project, you can start using the encode and decode functions.

Encoding

The encode function allows you to encode a combination of elements into a single integer value using the combinatorial number system.

// Picking 5 elements ( nCk where k=5 )
encode([4, 3, 2, 1, 0]); // returns 0
encode([5, 3, 2, 1, 0]); // returns 1
encode([5, 4, 2, 1, 0]); // returns 2
encode([5, 4, 3, 1, 0]); // returns 3
encode([5, 4, 3, 2, 0]); // returns 4
encode([5, 4, 3, 2, 1]); // returns 5
encode([6, 3, 2, 1, 0]); // returns 6

Decoding

The decode function allows you to decode an integer and get a combination, represented as an integer array sorted in descending order.

let k = 5;
decode(0, k); // returns [4, 3, 2, 1, 0]
decode(1, k); // returns [5, 3, 2, 1, 0]
decode(2, k); // returns [5, 4, 2, 1, 0]
decode(3, k); // returns [5, 4, 3, 1, 0]
decode(4, k); // returns [5, 4, 3, 2, 0]
decode(5, k); // returns [5, 4, 3, 2, 1]
decode(6, k); // returns [6, 3, 2, 1, 0]

License

The "combinadics" library is released under the MIT License.

combinadics's People

Contributors

keyeh avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.