Skip to main content
added 81 characters in body
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

JavaScript (ES6), 163 120120 110 bytes

g=([j,...a],n,l)=>l?g(a,a.reduce((r,i)=>r+=i>j,n*l),l-1):n
f=(n,a=[],i=0,r=0,[j,...b]=a)=>n?a.splice(n%-~i,0,i)&&f|f(n/++i|0,a,i):gi?f(an,0b,i-1,b.reduce((r,k)=>r+=k>j,r*i)):r
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. Edit: Saved about 25% by making f invert and reverse the permutation, then making g convert the reversed permutation back to an index. Saved a further 10 bytes by combining the two recursive calls into a single function. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 163 120 bytes

g=([j,...a],n,l)=>l?g(a,a.reduce((r,i)=>r+=i>j,n*l),l-1):n
f=(n,a=[],i=0)=>n?a.splice(n%-~i,0,i)&&f(n/++i|0,a,i):g(a,0,i)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. Edit: Saved about 25% by making f invert and reverse the permutation, then making g convert the reversed permutation back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 163 120 110 bytes

f=(n,a=[],i=0,r=0,[j,...b]=a)=>n?a.splice(n%-~i,0,i)|f(n/++i|0,a,i):i?f(n,b,i-1,b.reduce((r,k)=>r+=k>j,r*i)):r
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. Edit: Saved about 25% by making f invert and reverse the permutation, then making g convert the reversed permutation back to an index. Saved a further 10 bytes by combining the two recursive calls into a single function. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}
added 53 characters in body
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

JavaScript (ES6), 163163 120 bytes

g=(a[j,...a],n,l)=>l?g(a,a.reduce((r,_,i)=>r+=i<=l&a.indexOf(l)<a.indexOf(i)=>r+=i>j,n*l--),l-1):n
f=(n,i=0,a=[],j=i-n%++ii=0)=>n?fa.splice(n/i|0n%-~i,0,i)&&f(n/++i|0,[...a.map(e=>e+=e>=j),j]i):g(a,0,i-1)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. Edit: Saved about 25% by making f invert and reverse the permutation, then making g invertsconvert the reversed permutation as it converts it back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 163 bytes

g=(a,n,l)=>l?g(a,a.reduce((r,_,i)=>r+=i<=l&a.indexOf(l)<a.indexOf(i),n*l--),l):n
f=(n,i=0,a=[],j=i-n%++i)=>n?f(n/i|0,i,[...a.map(e=>e+=e>=j),j]):g(a,0,i-1)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. g inverts the permutation as it converts it back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 163 120 bytes

g=([j,...a],n,l)=>l?g(a,a.reduce((r,i)=>r+=i>j,n*l),l-1):n
f=(n,a=[],i=0)=>n?a.splice(n%-~i,0,i)&&f(n/++i|0,a,i):g(a,0,i)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. Edit: Saved about 25% by making f invert and reverse the permutation, then making g convert the reversed permutation back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}
bugfix
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281

JavaScript (ES6), 154163 bytes

g=(a,n,l)=>l?g(a,a.reduce((r,_,i)=>r+=i<=l&a.indexOf(l,)<a.indexOf(i)<0,n*l--),l):n
f=(n,i=0,a=[],j=i-n%++i)=>n?f(n/i|0,i,[...a.map(e=>e+=e>=j),j]):g(a,0,i-1)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. g inverts the permutation as it converts it back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 154 bytes

g=(a,n,l)=>l?g(a,a.reduce((r,_,i)=>r+=i<=l&a.indexOf(l,i)<0,n*l--),l):n
f=(n,i=0,a=[],j=i-n%++i)=>n?f(n/i|0,i,[...a.map(e=>e+=e>=j),j]):g(a,0,i-1)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. g inverts the permutation as it converts it back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}

JavaScript (ES6), 163 bytes

g=(a,n,l)=>l?g(a,a.reduce((r,_,i)=>r+=i<=l&a.indexOf(l)<a.indexOf(i),n*l--),l):n
f=(n,i=0,a=[],j=i-n%++i)=>n?f(n/i|0,i,[...a.map(e=>e+=e>=j),j]):g(a,0,i-1)
<input type=number min=0 oninput=o.textContent=f(+this.value)><pre id=o>

0-indexed. Works by converting the index to a permutation, inverting it, then converting back to an index. g inverts the permutation as it converts it back to an index. Ungolfed:

function index(n) {
    var a = [0];
    for (var i = 1; n = Math.floor(n / i); i++) {
        var j = i - n % (i + 1);
        for (var k = 0; k < i; k++) {
            if (a[k] > j) a[k]++;
        }
        a.push(j);
    }
    a = [...a.keys()].map(k => a.indexOf(k));
    while (i) {
        n *= i--;
        j = a.pop();
        for (k = 0; k < i; k++) {
            if (a[k] > j) n++;
        }
    }
    return n;
}
Source Link
Neil
  • 177.2k
  • 12
  • 74
  • 281
Loading