npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

number-pairings

v1.7.5

Published

JavaScript number pairings functions

Downloads

689

Readme

number-pairings

JavaScript number pairing functions

This is a fun project about pairing functions, the coding of two natural numbers into one and vice versa. Now implemented are:

  • Pairing for finite fields (both dimensions are limited) (field).
  • Pairings for one finite and one infinite dimension (stack_y, stack_x).
  • Cantor pairing (Cantor), and a half variant for unordered pairs (half).
  • elegant pairing (elegant).
  • POTO pairing (poto). The formulas are derived from here.

There is also a composition operator available that can mix arbitrary (finite and infinite) pairings recursively. With this operator it is possible to encode lists of natural numbers into a single number and back.

Installation

Prerequisite: Node.js needs to be installed.

  1. Make a folder
  2. Open a console in that new folder
  3. Install locally with: npm install number-pairings

Usage

const np = require( "number-pairings" )
let pair = np.Cantor()
console.log( pair.split( 100 ) )
// => [ 4, 9 ] ( = [x, y] )
console.log( pair.join( 4, 9 ) )
// => 100 ( = z )

Replace np.Cantor with e.g. np.elegant to try out the elegant pairing.

Build

Build from source: npm run build.

Limitations

  • There are no overflow checks. Since z (result of the join functions) is always in the order of a multiplication of x and y (results of the split functions) the library only works up to some numbers. There are no warnings given. Use with caution.
  • There are other possible number pairings that could be included. Some of them are not "dense", i.e. more than one pair may encode a number. Here the focus is on dense pairings.

Further links

License

MIT, see license file in the repository.

Examples

const np = require("number-pairings")
let p = np.Cantor()
p.join( 10, 34 ) // => 1024
p.split( 1024 ) // => [ 10, 34 ]
p = np.elegant()
p.join( 10, 34 ) // => 1200
p.split( 1200 ) // => [ 10, 34 ]
p = np.poto()
p.join( 10, 34 ) // => 70655
p.split( 70655 ) // => [ 10, 34 ]
p = np.half()
p.join( 10, 34 ) // => 605
p.join( 34, 10 ) // => 605
p.split( 605 ) // => [ 10, 34 ]
p = np.field( 2, 3 )
p.join( 1, 2 ) // => 5
p.join( 2, 2 ) // => undefined (out of bound)
p.split( 5 ) // => [ 1, 2 ]
p = np.stack_x( 5 )
p.join( 2, 4 ) // => 14
p.join( 2, 5 ) // => undefined (out of bound)
// note: use stack_y the same way as stack_x
p.split( 14 ) // => [ 2, 4 ]
p = np.composition( [ 2, 3, 4, 5 ] )
p.join( [ 0, 1, 2, 3 ] ) // => 86
p.split( 86 ) // => [ 0, 1, 2, 3 ]
p.join( [ 1, 0, 0, 0 ] ) // => 1
p.join( [ 0, 1, 0, 0 ] ) // => 2
p.join( [ 0, 0, 1, 0 ] ) // => 6
p.join( [ 0, 0, 0, 1 ] ) // => 24