Calcola il MCD con il metodo di Euclide delle differenze successive

 

 
 

Codice sorgente

 
  <html>
<head>
<title>Il mio primo programma javascript per il calcolo del MCD con Euclide</title>

<script>
//###########################################################################
//## Copyright (C) 2005 Ubaldo Pernigo
//## Author Ubaldo Pernigo
//## Support can be obtained from author at http://www.pernigo.com
//## This program is free software. You can redistribute it and/or modify
//## it under the terms of the GNU General Public License as published by
//## the Free Software Foundation; either version 2 of the License.
//###########################################################################
//## date who what
//## 20050225 ubi Prima versione per lezione gruppo potenziamento
//###########################################################################

function EuclideUno(){
var a=0;
var a1=0;
var b=0;
var b1=0;
var s=0;
var contatore=0;
var diff=0;
var messaggiovideo="";

a1 = prompt("inserisci il primo numero","");
b1 = prompt("inserisci il secondo numero","");
a = parseFloat(a1);
b = parseFloat(b1);

if (a < b)
{
s = a;
a = b;
b = s;
}
diff = a - b;

messaggiovideo = a +"(a) - " + b + "(b) = " + diff + "<br>";
document.write(messaggiovideo);

while (diff != 0)
{
a = b;
b = diff;

if (a < b)
{
s = a;
a = b;
b = s;
}

diff = a - b
messaggiovideo = a + "(a) - " + b + "(b) = " + diff + "<br>";
document.write(messaggiovideo);
contatore++;
}

messaggiovideo = "<hr><h3>Il MCD tra " + a1 + " (a) e " + b1 + " (b) " + a + "</h3><br>";
document.write(messaggiovideo);
}

</script>

</head>
<h2>Calcola il MCD con il metodo di Euclide delle differenze successive</h2>
&nbsp;<p><input type="Button" value="Esegui programma" onClick="EuclideUno();"</p>

</html>