Skip to content

Instantly share code, notes, and snippets.

View kobtsev-m's full-sized avatar
🎯
Focusing

Michael Kobtsev kobtsev-m

🎯
Focusing
View GitHub Profile
@TheGreatRambler
TheGreatRambler / szudzik.js
Last active August 7, 2024 14:57
A pairing function (szudzik) that supports negative numbers
// src https://codepen.io/sachmata/post/elegant-pairing
szudzik.getindex = function(x, y) {
var xx = x >= 0 ? x * 2 : x * -2 - 1;
var yy = y >= 0 ? y * 2 : y * -2 - 1;
return (xx >= yy) ? (xx * xx + xx + yy) : (yy * yy + xx);
};
szudzik.unpair = function(z) {
var sqrtz = Math.floor(Math.sqrt(z));