feat(interview-prep): Gamma Function (#17371)
This commit is contained in:
committed by
Kristofer Koishigawa
parent
bc0e213529
commit
d71a949a4f
@ -4367,6 +4367,69 @@
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Gamma function",
|
||||
"description": ["<div class=\"rosetta\"><p class=\"rosetta__paragraph\">Implement one algorithm (or more) to compute the <a class=\"rosetta__link--wiki\" href=\"https://en.wikipedia.org/wiki/Gamma function\" title=\"wp: Gamma function\">Gamma</a> ($\\Gamma$) function (in the real field only).</p><p class=\"rosetta__paragraph\">The Gamma function can be defined as:</p><br/><p class=\"rosetta__paragraph\"><span class=\"rosetta__text--indented\">::::: <big><big> $\\Gamma(x) = \\displaystyle\\int_0^\\infty t^{x-1}e^{-t} dt$</big></big></span></p></div>"],
|
||||
"solutions": [
|
||||
"function gamma(x) {\n var p = [0.99999999999980993, 676.5203681218851, -1259.1392167224028,\n 771.32342877765313, -176.61502916214059, 12.507343278686905,\n -0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7\n ];\n \n var g = 7;\n if (x < 0.5) {\n return Math.PI / (Math.sin(Math.PI * x) * gamma(1 - x));\n }\n\n x -= 1;\n var a = p[0];\n var t = x + g + 0.5;\n for (var i = 1; i < p.length; i++) {\n a += p[i] / (x + i);\n }\n \n var result=Math.sqrt(2 * Math.PI) * Math.pow(t, x + 0.5) * Math.exp(-t) * a;\n\n return result;\n}\n"
|
||||
],
|
||||
"tests": [
|
||||
{
|
||||
"text": "<code>gamma</code> should be a function.",
|
||||
"testString": "assert(typeof gamma=='function','<code>gamma</code> should be a function.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[0]+')</code> should return a number.",
|
||||
"testString": "assert(typeof gamma(tests[0])=='number','<code>gamma('+tests[0]+')</code> should return a number.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[0]+')</code> should return <code>'+results[0]+'</code>.",
|
||||
"testString": "assert.equal(gamma(tests[0]),results[0],'<code>gamma('+tests[0]+')</code> should return <code>'+results[0]+'</code>.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[1]+')</code> should return <code>'+results[1]+'</code>.",
|
||||
"testString": "assert.equal(gamma(tests[1]),results[1],'<code>gamma('+tests[1]+')</code> should return <code>'+results[1]+'</code>.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[2]+')</code> should return <code>'+results[2]+'</code>.",
|
||||
"testString": "assert.equal(gamma(tests[2]),results[2],'<code>gamma('+tests[2]+')</code> should return <code>'+results[2]+'</code>.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[3]+')</code> should return <code>'+results[3]+'</code>.",
|
||||
"testString": "assert.equal(gamma(tests[3]),results[3],'<code>gamma('+tests[3]+')</code> should return <code>'+results[3]+'</code>.')"
|
||||
},
|
||||
{
|
||||
"text": "<code>gamma('+tests[4]+')</code> should return <code>'+results[4]+'</code>.",
|
||||
"testString": "assert.equal(gamma(tests[4]),results[4],'<code>gamma('+tests[4]+')</code> should return <code>'+results[4]+'</code>.')"
|
||||
}
|
||||
],
|
||||
"id": "5a23c84252665b21eecc7e76",
|
||||
"challengeType": "3",
|
||||
"releasedOn": "June 1, 2018",
|
||||
"files": {
|
||||
"indexjs": {
|
||||
"key": "indexjs",
|
||||
"ext": "js",
|
||||
"name": "index",
|
||||
"contents": [
|
||||
"function gaussianElimination (A,b) {",
|
||||
" // Good luck!",
|
||||
"}"
|
||||
],
|
||||
"head": [],
|
||||
"tail": [
|
||||
"let tests=[.1,.2,.3,.4,.5];",
|
||||
"let results=[",
|
||||
" 9.513507698668736,",
|
||||
" 4.590843711998803,",
|
||||
" 2.9915689876875904,",
|
||||
" 2.218159543757687,",
|
||||
" 1.7724538509055159",
|
||||
"];"
|
||||
]
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"title": "Sailors, coconuts and a monkey problem",
|
||||
"description": [
|
||||
|
Reference in New Issue
Block a user