Skip to content

Instantly share code, notes, and snippets.

@jacksonwillis
Created April 27, 2012 19:06
Show Gist options
  • Save jacksonwillis/2511948 to your computer and use it in GitHub Desktop.
Save jacksonwillis/2511948 to your computer and use it in GitHub Desktop.
Superfactorial
Number.prototype.superfactorial = function() {
var a, e, d, b, c;
d = 1;
for(a = b = 2;2 <= this ? b <= this : b >= this;a = 2 <= this ? ++b : --b) {
for(e = c = 2;2 <= a ? c <= a : c >= a;e = 2 <= a ? ++c : --c) {
d *= e
}
}
return d
};
function range(c,b){return function(){var d=[];for(var a=c;c<=b?a<=b:a>=b;c<=b?a++:a--)d.push(a);return d}(this)}
Array.prototype.product = function() {
return this.reduce(function(a, b) {
return a * b
})
};
Number.prototype.superfactorial = function() {
return function(a) {
return 2 > a ? 1 : range(2, a).map(function(a) {
return range(2, a).product()
}).product()
}(this)
};
/* with Y combinator */
Number.prototype.superfactorial = function() {
return function(c) {
return function(a) {
return a(a)
}(function(a) {
return c(function(d) {
return a(a)(d)
})
})
}(function(c) {
return function(a) {
return 2 > a ? 1 : function(a) {
return function(a) {
return a(a)
}(function(b) {
return a(function(a) {
return b(b)(a)
})
})
}(function(a) {
return function(b) {
return 2 > b ? 1 : b * a(b - 1)
}
})(a) * c(a - 1)
}
})(this)
};
/* with U combinator */
Number.prototype.superfactorial = function() {
return function(a) {
return a(a)
}(function(a) {
return function(b) {
return 2 > b ? 1 : function(a) {
return a(a)
}(function(a) {
return function(b) {
return 2 > b ? 1 : b * a(a)(b - 1)
}
})(b) * a(a)(b - 1)
}
})(this)
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment