杨辉三角展开的JavaScript程序
2012-1-25 13:11
阅读:11439
标签:程序, 杨辉三角
作者:蒋迅
上面是一位美国学生的作业。这个学生按照字面的意思“展开”(expand) 代数式(a + b)n,真的有才。其实老师的意思是要学生得出象 (a + b)2 = a2 + 2ab + b2 这样的等式。但是题目过於抽象,学生不知所措。其实应该先从具体的自然数 n = 2, 3, ... 开始。
Source: Dauger Research
这个数学公式在中国被称作“杨辉三角”。之所以称为杨辉三角是因为宋代数学家杨辉在《详解九章算术》里讨论这种形式的数表,并说明此表引自贾宪的《释锁算术》。英文的名字是“Pascal Triangle”,取自法国数学家布莱士·帕斯卡(Blaise Pascal) 的名子。实际上,这个三角形的发现比杨辉和帕斯卡都要早。我以前写过一个Javascript程序帮助学生做杨辉三角的展开。现在贡献出来,给老师们参考。不过,这里不让运行自己的Javascript程序,所以我只能给出原程序,请读者自己下载试用。
<SCRIPT type="text/javascript">
<!--
var my_n;
var noIFrame = false;
function reLoadFrame(StrPoly,StrTable){
with(winTrig.document) {
close();
open();
write('<html>n');
write('<head>n');
write('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>n');
write('</head>n');
write('<body>n');
write(StrPoly + 'n');
write('<p>n');
write(StrTable + 'n');
write('</body>n');
write('</html>n');
close();
}
}
function InitFrame() {
with(winTrig.document) {
close();
open();
writeln('<html>');
writeln('<head>');
writeln('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>');
writeln('</head>');
writeln('<body>');
writeln("Please input a positive integer.");
writeln('</body>');
writeln('</html>');
close();
}
}
function doOnLoad() {
if(noIFrame) {
if(top.location == self.location || self.location != parent.frames[0].location) {
with(top.document) {
close();
open();
writeln('<html>');
writeln('<head>');
writeln('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>');
writeln('</head>');
writeln('<frameset rows="350,*">');
writeln('<frame name="pmain" src="'+ self.location + '" scrolling="no">');
writeln('<frame name="graph" src="about:blank">');
writeln('</frameset>');
writeln('</html>');
close();
}
return;
}
winTrig = parent.frames[1];
}
else
winTrig = window["EmbdFrame"];
InitFrame();
document.forms[0].reset();
document.forms[0].btnStart.disabled = true;
document.forms[0].expd.disabled = false;
}
function expandit() {
my_n = document.forms[0].user_power.value;
if (isNumber(my_n)) {
// coefficients of polynomial expansion.
c = new Array(my_n+1);
c[0] = new Array(1);
c[0][0] = 1;
for (i=1; i<= my_n; i++) {
c[i] = new Array(i+1);
c[i][0] = 1;
c[i][i] = 1;
for (j=1; j<i; j++) {
c[i][j] = c[i-1][j-1] + c[i-1][j];
}
}
// construct the polynomial expansion.
var txtPoly = "(x + y)" + my_n + " = n";
if (my_n == 1) { txtPoly += "x + y."}
else {
if (my_n == 2) { txtPoly += "x2 + 2xy + y2."}
else {
txtPoly += 'x' + my_n + 'n';
for(i=1; i<my_n; i++){
j = my_n - i;
coef = c[my_n][i];
// x^j*y^i
txtPoly += ' + ' + coef + 'x' + j + 'y' + i + 'n';
if (i%5 == 0) txtPoly += '
';
}
txtPoly += ' + y' + my_n + '.n';
// filter out power 1.
txtPoly = txtPoly.replace(/<sup>1</sup>/g,'');
}
}
var txtTable = "The corresponding Pascal's Triangle:n<p>";
txtTable += "<table border=0>";
for (i = 0; i <= my_n; i++) {
txtTable += "<tr>";
for (j = 0; j < my_n - i; j++) {
txtTable += "<td>";
txtTable += " "
txtTable += "</td>n";
}
for (j = my_n - i; j < 2*my_n + 1; j++) {
k = j - my_n + i;
txtTable += "<td align='center'>";
if (k%2 == 0 && k <= 2*i) {
txtTable += c[i][k/2];
}
else {
txtTable += " "
}
txtTable += "</td>n";
}
txtTable += "</tr>n";
}
txtTable += "</table>";
reLoadFrame(txtPoly,txtTable);
}
}
// data validation. Could be replaced by regular expression.
function isNumber(inputVal) {
inputStr = "" + inputVal;
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.charAt(i);
if (oneChar < "0" || oneChar > "9") {
return false;
}
return true;
}
}
function myReload() {
document.forms[0].user_power.value = 3;
doOnLoad();
javascript:location.reload();
}
// -->
</SCRIPT>
<FORM>
<h2 align="center">Expansion of Perfect nth Power and Pascal's Triangle</h2>
This script will generate the expansion of the perfect nth power (x + y)n.
<p>
<form name="formular">
请选择一个正整数: <input name="user_power" size=4 maxlength=2 value=3>
<input type=button name="btnStart" value="开始" onClick="doOnLoad()">
<input type=button name="expd" value="展开" onClick="expandit()" DISABLED="true">
<!-- <input type=reset value="重来" onClick="myReload()"> -->
</form>
<P> </P>
<IFRAME ID="IFrameExpd" Name="EmbdFrame" FRAMEBORDER=0 SRC="" WIDTH="100%" height="500">
<SCRIPT type="text/javascript">
<!--
noIFrame = true;
// -->
</SCRIPT>
</IFRAME>
<!--
var my_n;
var noIFrame = false;
function reLoadFrame(StrPoly,StrTable){
with(winTrig.document) {
close();
open();
write('<html>n');
write('<head>n');
write('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>n');
write('</head>n');
write('<body>n');
write(StrPoly + 'n');
write('<p>n');
write(StrTable + 'n');
write('</body>n');
write('</html>n');
close();
}
}
function InitFrame() {
with(winTrig.document) {
close();
open();
writeln('<html>');
writeln('<head>');
writeln('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>');
writeln('</head>');
writeln('<body>');
writeln("Please input a positive integer.");
writeln('</body>');
writeln('</html>');
close();
}
}
function doOnLoad() {
if(noIFrame) {
if(top.location == self.location || self.location != parent.frames[0].location) {
with(top.document) {
close();
open();
writeln('<html>');
writeln('<head>');
writeln('<title>Expansion of Perfect nth Power and Pascal's Triangle</title>');
writeln('</head>');
writeln('<frameset rows="350,*">');
writeln('<frame name="pmain" src="'+ self.location + '" scrolling="no">');
writeln('<frame name="graph" src="about:blank">');
writeln('</frameset>');
writeln('</html>');
close();
}
return;
}
winTrig = parent.frames[1];
}
else
winTrig = window["EmbdFrame"];
InitFrame();
document.forms[0].reset();
document.forms[0].btnStart.disabled = true;
document.forms[0].expd.disabled = false;
}
function expandit() {
my_n = document.forms[0].user_power.value;
if (isNumber(my_n)) {
// coefficients of polynomial expansion.
c = new Array(my_n+1);
c[0] = new Array(1);
c[0][0] = 1;
for (i=1; i<= my_n; i++) {
c[i] = new Array(i+1);
c[i][0] = 1;
c[i][i] = 1;
for (j=1; j<i; j++) {
c[i][j] = c[i-1][j-1] + c[i-1][j];
}
}
// construct the polynomial expansion.
var txtPoly = "(x + y)" + my_n + " = n";
if (my_n == 1) { txtPoly += "x + y."}
else {
if (my_n == 2) { txtPoly += "x2 + 2xy + y2."}
else {
txtPoly += 'x' + my_n + 'n';
for(i=1; i<my_n; i++){
j = my_n - i;
coef = c[my_n][i];
// x^j*y^i
txtPoly += ' + ' + coef + 'x' + j + 'y' + i + 'n';
if (i%5 == 0) txtPoly += '
';
}
txtPoly += ' + y' + my_n + '.n';
// filter out power 1.
txtPoly = txtPoly.replace(/<sup>1</sup>/g,'');
}
}
var txtTable = "The corresponding Pascal's Triangle:n<p>";
txtTable += "<table border=0>";
for (i = 0; i <= my_n; i++) {
txtTable += "<tr>";
for (j = 0; j < my_n - i; j++) {
txtTable += "<td>";
txtTable += " "
txtTable += "</td>n";
}
for (j = my_n - i; j < 2*my_n + 1; j++) {
k = j - my_n + i;
txtTable += "<td align='center'>";
if (k%2 == 0 && k <= 2*i) {
txtTable += c[i][k/2];
}
else {
txtTable += " "
}
txtTable += "</td>n";
}
txtTable += "</tr>n";
}
txtTable += "</table>";
reLoadFrame(txtPoly,txtTable);
}
}
// data validation. Could be replaced by regular expression.
function isNumber(inputVal) {
inputStr = "" + inputVal;
for (var i = 0; i < inputStr.length; i++) {
var oneChar = inputStr.charAt(i);
if (oneChar < "0" || oneChar > "9") {
return false;
}
return true;
}
}
function myReload() {
document.forms[0].user_power.value = 3;
doOnLoad();
javascript:location.reload();
}
// -->
</SCRIPT>
<FORM>
<h2 align="center">Expansion of Perfect nth Power and Pascal's Triangle</h2>
This script will generate the expansion of the perfect nth power (x + y)n.
<p>
<form name="formular">
请选择一个正整数: <input name="user_power" size=4 maxlength=2 value=3>
<input type=button name="btnStart" value="开始" onClick="doOnLoad()">
<input type=button name="expd" value="展开" onClick="expandit()" DISABLED="true">
<!-- <input type=reset value="重来" onClick="myReload()"> -->
</form>
<P> </P>
<IFRAME ID="IFrameExpd" Name="EmbdFrame" FRAMEBORDER=0 SRC="" WIDTH="100%" height="500">
<SCRIPT type="text/javascript">
<!--
noIFrame = true;
// -->
</SCRIPT>
</IFRAME>
笔者在这个程序里有一件事情没有做:假如有人输入一个任意的“n”,我们应该得到一个一般性的展开式。做到这点不难,有兴趣的读者不妨自己补上。
下面再给几个相关图形:
Source: Radoslav Jovanovic
Source: TeXample.net
Source: Marianne Parsons
Source: Dirk Laureyssens
转载本文请联系原作者获取授权,同时请注明本文来自蒋迅科学网博客。
链接地址:https://wap.sciencenet.cn/blog-420554-531567.html?mobile=1
收藏
当前推荐数:3
推荐到博客首页
网友评论6 条评论