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

@dn0rmand/project-euler-tools

v1.3.0

Published

Set of tools for Project Euler

Downloads

467

Readme

project-euler-tools

Set of tools for Project Euler

Installation

With npm do:

$ npm install @dn0rmand/project-euler-tools

Examples

BigMap / BigSet

Same as the javascript Map / Set but allows going over the limit of 2^24 entries

const { BigMap, BigSet } = require('@dn0rmand/project-euler-tools');

const m = new BigMap();
const s = new BigSet();

m.set('key', 'value');
s.add('key');

binomial

Helper to calculate n!/p!(n-1)! with memoization

const { binomial } = require('@dn0rmand/project-euler-tools');

console.log(binomial(5, 2)); // n=5 , p=2
console.log(binomial(5n, 2n)); // calculate using BigInt

BitArray

Binary array to store only true(1) or false(0). It uses an Uint8Array array so its max size is 8 times the max size of a Unt8Array

const { BitArray } = require('@dn0rmand/project-euler-tools');

const ar = BitArray(1024);
ar.set(12, true);
ar.get(12);

digits

Returns the digits of a value in a specified base ( defaults to 10 ). Supports both Number and BigInt

const { digits } = require('@dn0rmand/project-euler-tools');

const a = digits(12345); // a = [1,2,3,4,5]
const b = digits(0x12345n, 16); // b = [1, 2, 3, 4, 5]

isPrime

Same as is-number-prime but with a shortcut for numbers lower than 20

DistinctCollection

Collection of distincts numbers

const { DistinctCollection } = require('@dn0rmand/project-euler-tools');

const values = new DistinctCollection();
values.push(1);
values.push(2);
values.push(1);
values.push(3);
values.push(2);

console.log(values.length); // 3
console.log([...values].join(',')); // 1,2,3

divisors

Gets the list of divisors of a number. List is not sorted

const { divisors } = require('@dn0rmand/project-euler-tools');

for(const d of divisors(10)) {
  console.log(d);
}

for(const d of divisors(10, myIsPrimeFunction)) {
  console.log(d);
}

divisors(10, undefined, d => {
  console.log(d);
});

divisorsCount

Gets the number of divisors of a number.

const { divisorsCount } = require('@dn0rmand/project-euler-tools');

console.log(divisorsCount(10));

fibonacci

Calculates the fibonacci value of a number with modulo, using matrix.

const { fibonacci } = require('@dn0rmand/project-euler-tools');

console.log(fibonacci(20000, 1E8));

linearRecurrence

Tries to find a linearRecurrence for a given array of values

polynomial

Tries to find a polynomial for a given set of values

primeHelper

Lots of helpers related to prime numbers including mobius and PHI functions.

primes

Prime generator function.

const { primes } = require('@dn0rmand/project-euler-tools');

const start = 50;
for(const p of primes(start)) {
  if (p > 100) { break; }
  console.log(p);
}

TimeLogger

Allows to calculate how long a process took

const { TimeLogger } = require('@dn0rmand/project-euler-tools');

const result = TimeLogger.wrap('processing', _ => doStuff()); 

result value will be the value returned by doStuff and the elapsed time will be output to the console

Tracer

Helper to trace progress while running long calculations. The trace will be output only if a second elapsed since the previous call to print

const { Tracer } = require('@dn0rmand/project-euler-tools');

const tracer = new Tracer(true);
for (let k = 1; k <= MAX; k++) {
  tracer.print(_ => MAX - k);
  doStuffWithK(k);
}
tracer.clear();

src/numberHelper and src/bigintHelper

Those are automatically included by require('@dn0rmand/project-euler-tools');
They add the following prototypes to Number and BigInt

  • gcd(b)
    calculates the greatest common divisor of the value and b

  • lcm(b)
    calculates the least common multiple of the value and b

  • isCoPrime(b)
    Checks if the value is coprime with b

  • modMul(product, modulo)
    calculates (value*product) % modulo

  • modPow(power, modulo)
    calculates (value ** power) % modulo

  • modInv(modulo)
    calculates the modulo inverse

  • modDiv(divisor, modulo)
    calculates (value * product.modInv(modulo)) % modulo

The following prototypes are also added to BigInt

  • toExponential(maxDigits)
    Returns the number converted to a string in the exponential format. For example the following code with output 1.23457e11
const v = 123456781234n;
console.log(v.toExponential(5));
  • divise(divisor, precision)
    Divides the BigInt value by the divisor (a BigInt too) with the required precision and returns the result as a Number