Created
January 22, 2020 22:17
-
-
Save john212/371ea1e9064d72ee4c46b2e678fefa5c to your computer and use it in GitHub Desktop.
Pascal's Triangle Coefficients
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Pascal's Triangle Coefficient Calculator</title> | |
</head> | |
<body> | |
<h2>Pascal's Triangle Coefficient Calculator</h2> | |
<P>This program will calculate the coefficients of Pascal's Triangle up to | |
any row "r" specified by the user. The formula used is from the Binomial Theorem | |
where the coefficients of each term of the expansion of the binomial | |
(x + y)<sup>n</sup> are given by <sub>n</sub>C<sub>r</sub> read "a (C)ombination of | |
"n" things taken "r" at a time". The formula for <sub>n</sub>C<sub>r</sub> is n!/(n-r)!r! | |
where "!" is the factorial symbol. Note that since "r' is indexed at zero, | |
that is, the first row means r = 0, that means n, the exponent of the | |
binomial expansion, will be n, but there will be n + 1 terms and | |
therefore n +1 coefficients of Pascal's Triangle.</P> | |
<p>Factorial n written n! means (n)(n-1)(n-2)...(1)</p> | |
<p>By definition 0! = 1.</p> | |
<P>For example, row 3 of Pascals Triangle means n=3 (third row | |
of Pascals Triangle starting at 0) and r = 3; n is fixed but r | |
will run from 0 to n, i,e, 0, 1, 2, and 3 in this example, | |
so the coefficients of Pascal's Triangle are:</P> | |
<ul> | |
<li>3Cr = 3!/(3-r)!r! </li> | |
<li>3C0 = 3!/(3-0)!0! = 6/6*1 = 1</li> | |
<li>3C1 = 3!/(3-1)!1! = 6/2*1 = 3</li> | |
<li>3C2 = 3!/(3-2)!2! = 6/1*2 = 3</li> | |
<li>3C3 = 3!/(3-3)!3! = 6/1*6 = 1</li> | |
</ul> | |
<P>This program is written in JavaScript.</P> | |
<P>By Mr. C. January, 2020</P> | |
<P>*********************results********************************</P> | |
<script> | |
main(); //must call the main() function to run the program. | |
function main() //Envelope program in main() function | |
{ //start main() function block | |
var ptrows = ""; // Pascal's Triangle rows, indexed at 0. | |
// ask user to input the number of rowa | |
ptrows = prompt("Enter rows of Pascals Triangle", "Type the value here"); | |
ptrows = parseInt(ptrows,10); //convert text input (string input) to an integer | |
// Data Validation - Ensure user input is >= 0 for r. | |
if(!(ptrows >= 0)) | |
{//start data validation if block | |
document.write("<P>ptrows=", ptrows); | |
document.write("<P>ptrows must be greater than or equal to zero"); | |
return; // stops execution of if block | |
}//endof data validation if block | |
document.write("<P><b>Pascals Triangle Coefficients</b></P>"); | |
document.write("<P>Number of Terms=", ptrows+1); | |
document.write("<P>N= ", ptrows); | |
// calculate the coefficients of each term | |
for(let i = 0; i <= ptrows; i++) | |
{ // start pt coefficient for loop | |
var ptcoefficient = 0; | |
ptcoefficient = nCr(ptrows,i); | |
document.write("<P>r = ", i, " coefficient ", ptcoefficient ); | |
} // endof pt coefficient for loop | |
}//endof main() function block | |
// ***************************************************************************** | |
// function to return the factorial of any passed integer | |
// note: the set of integers is abbreviated "Z" | |
function factorial(_Z) // Z is set of integers, _Z means variable is an argument | |
{ //start factorial function block | |
var counter = _Z; // will use the integer argument as the starting value of a counter | |
var factorialZ = 1; | |
if(_Z === 0) | |
{//start if _Z is 0 if block | |
factorialZ = 1; | |
return factorialZ; | |
}//endof if _Z is 0 if block | |
while (counter >= 1) | |
{//start while block | |
factorialZ = counter*factorialZ; | |
counter = counter - 1; | |
}//endof while block | |
return factorialZ; | |
} //endof factorial function block | |
// ***************************************************************************** | |
// ***************************************************************************** | |
// function nCr to return the combination of n things taken r at a time | |
function nCr(_n, _r) | |
{ //start nCr function block | |
var nMinusr = 0; | |
var combinations = 0; | |
nMinusr = _n - _r; | |
combinations = factorial(_n)/((factorial(nMinusr))*factorial(_r)); | |
return combinations; | |
} //endof nCr function block | |
// ***************************************************************************** | |
// ***************************************************************************** | |
// function nPr to return the permutation of n things taken r at a time | |
// TBD-use nCr for model | |
// ***************************************************************************** | |
// ***************************************************************************** | |
// Console log variables for debugging - comment in/out as needed | |
/* | |
TBD | |
*/ | |
// ***************************************************************************** | |
</script> | |
<p><b>To check your answers, visit one of these sites:</b> | |
<ul> | |
<li><a href="http://mathforum.org/dr.cgi/pascal.cgi">Interactive Pascal's Triangle at MathForum.org</a></li> | |
<li><a href="https://www.symbolab.com/solver/step-by-step/%5Cleft(x%2By%5Cright)%5E%7B5%7D">Binomial Expansion at Symbolab</a></li> | |
<li><a href="http://ed.ted.com/lessons/the-mathematical-secrets-of-pascal-s-triangle-wajdi-mohamed-ratemi">Pascal's Triangle - Wajdi Mohamed Ratemi - TED Video</a></li> | |
<li><a href="https://www.mathsisfun.com/algebra/binomial-theorem.html">The Binomial Theorem at MathIsFun.com</a><br><i>(check out the derivation of "Euler's Number" , aka "e".)</i></li> | |
</ul> | |
</p> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment