Generando el triángulo de Pascal con JS.

En este artículo se muestra un script que permite generar el triángulo de Pascal (o de Tartaglia) mediante un script de JavaScript. Usted sólo debe indicar el número de filas que debe tener el triángulo.

Íntimamente relaccionados con este artículo, se recomienda leer estos otros:


Indice.


1.- Generar el triángulo de N filas.

Escriba el número de filas que va a tener el triángulo de Tartaglia.

Número de filas :

Triángulo de Pascal o Tartaglia:

 

 



2.- Código de las funciones.


<script type="text/javascript">
function factorial(p)
{
f=1;
for ( i=1 ; i <= p ; i++)
{
f *= i;
}
return f;
}

function combinaciones(m,n)
{
if(n<=m){
var dividendo1 = factorial(m);
var divisor1 = factorial(m-n);
var divisor = factorial(n);
var dividendo = dividendo1/divisor1;
var combinacion = Math.round(dividendo/divisor);
return combinacion;
}else return;
}

function tabla(filas)
{
var start = new Date().getTime();
var filas = parseInt(filas);
var texto = "<table border='0' cellspacing='3'";
texto += " style='font-size:11px;font-family:verdana'>";
var valor = 0;
var resultados = new Array();
texto += "<tr>";
for (i=-1; i<filas;i++){
if (i==-1)texto += "<td></td>";
else texto += "<td>N:" + i + "</td>";
}
texto += "</tr>";
for (m = 0; m < filas; m++)
{
texto += "<tr>";
for (n=0 ; n <filas; n++)
{
if (n == 0) texto += "<td>M:" + m +"</td>";
texto += "<td style='width:25;";
texto += "text-align:right;border:1px solid grey;'>";
if (n<=m){
valor = combinaciones(m,n);
texto += valor;
}
else texto += "";
texto += "</td>"
}
texto += "</tr>";
}
texto += "</table>"
document.getElementById('triangulo').innerHTML = texto;
var finish = new Date().getTime();
var tiempoEmpleado = finish - start;
var textoTiempo = "Tiemplo empleado en el cálculo: <b>" + tiempoEmpleado + " ms</b>";
document.getElementById('contadortemporal').innerHTML = textoTiempo;
}
</script>


3.- Comparativa de navegadores.

La tabla muestra los tiempos (en ms.) de cálculo del triángulo en los navegadores Internet Explorer 5.5, Firefox 1.04 y Opera 7.54 para distintos números de filas. Ordenador: AMD Duron 900 MHz, 256 M de RAM, sistema operatico Windows ME

Nº filas Firefox IE Opera
5 60 0 60
10 0 0 50
20 110 210 110
30 330 1540 280
35 430 3240 330
40 710 5940 440
45 830 9720 650
50 1200 14880 880



Añadir a Favoritos Internet Explorer

Menú

Categorías


Lo Último

  Mis programas  Matemáticas  Geometría  Programación WEB  Flash MX  Variados
Dedicado a ANGELES y a RAMON.
Realizado por José Antonio López Lorenzo (JALL)
Vigo - Galicia - España -Europa