Skip to content

Instantly share code, notes, and snippets.

View torjusti's full-sized avatar
🤠
yee haw

Torjus Iveland torjusti

🤠
yee haw
  • Trondheim, Norway
View GitHub Profile
@torjusti
torjusti / mappings.js
Created May 23, 2017 22:23
Bijection which maps repetition-less permutations to a single integer in a minimal range, given the number of affected pieces.
const getIndex = (permutation, affected) => {
const indexes = affected.map(i => permutation.indexOf(i));
if (affected.length === 1) {
return permutation.indexOf(affected[0]);
}
let base = permutation.length;
let index = indexes[indexes.length - 1];
@torjusti
torjusti / cas.js
Created February 1, 2015 21:16
Improvements to my old pseudo CAS
var NumberTheory = {};
/**
* @param {number} n
* @param {number} k
*/
NumberTheory.binomial = function(n, k) {
if (n === k) return 1;
if (k === 1) return n;
return binomial(n - 1, k) + binomial(n - 1, k - 1);
@torjusti
torjusti / cas.js
Created January 4, 2015 15:16
Simple equation solving hack in JavaScript
// Prototype that stores information about a given polynomial.
function Polynomial(data) {
// Support providing already existing Polynomials.
this.polynomial = data instanceof Polynomial ? data.polynomial : data;
}
// Rank all coefficients in the polynomial and return the largest.
Polynomial.prototype.getLargestCoefficient = function() {
var re = /(\d+)/g, match, max = 0;
while(match = re.exec(this.getPolynomial()))