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

diophantine-methods

v1.1.2

Published

Solve Diophantine Equations and Diophantine Approximations.

Downloads

14

Readme

diophantine-methods

Solve Diophantine Equations and Diophantine Approximations. Zero dependencies and heavily optimized.

Installation

npm install diophantine-methods

Diophantine Equations

Turn a linear equation written symbolically into a DiophantineEquation object. The variables of the object are parameterized and manipulated in terms of free variables t_1, t_2, ... t_{n-1}.

import { returnDiophantineEquation } from 'diophantine-methods'

// Example: The set of nickels, dimes, and quarter combinations that equal two dollars.
const equation = returnDiophantineEquation("5n + 10d + 25q = 200");

equation.getVariables();    // [n, d, q]
equation.getCoefficients(); // [5, 10, 25]
equation.getSolution();     // 200

// Returns a ParametricEquation object.
equation.n;

// View the parameteric space and evaluate it for integer values of t₁, t₂.
equation.n.toString();      // 40 + 5t₁ + 10t₂
equation.d.toString();      // -5t₂
equation.q.toString();      // -1t₁

const solutions = [];
equation.getVariables().forEach((v, index) => {
    solutions.push(equation[v].evalWithParameters([0, -1]));
});
console.log(solutions);     // [30, 5, 0] or 30 nickels, 5 dimes, 0 quarters.

// Evaluate equations.
equation.evalWithVariables([30, 5, 0]); // 200. It works!

// Generate solutions directly.
equation.randomSolution();              // [715, -205, -53]
equation.leastPositiveSolution();       // [0, 0, 8] 
equation.leastPositiveMagnitude();      // [1, 2, 7]

// Not all diophantine equations are solvable.
equation2 = returnDiophantineEquation("6n - 12d + 8q = 5"); // "No solutions."

Geometric Interpretation

The ParametricEquation objects return n-dimensional planes that only intersect at integer coordinates.

The vector with the least positive magnitude (point B) is the vector nearest the intersection of the plane and its normal vector (point C) with integer coordinates in its terminal point.

The vector with the least positive solution is the nearest projection onto one of the ParametricEquation planes (point A).

(Image from Geogebra.)

Diophantine Approximations

Find rational numbers p/q that are within epsilon distance of a real number.

import * as methods from 'diophantine-methods';

const realNum = Math.E;
const epsilon = 0.0000001;

// Returns approximations p/q as [p, q].
methods.continuedApproximation(realNum, epsilon);   // [23225, 8544]
methods.fareyApproximation(realNum, epsilon);       // [15062, 5541]
methods.dirichletApproximation(realNum, epsilon);   // [25946, 9545]

For details on the approximation algorithms and time complexity see main.tex.

Won Outstanding Poster Presentation @ CUNY DRP 2023