primes/primes.html

96 lines
1.6 KiB
HTML

<html>
<head>
<script language="JavaScript">
<!--
function primes(form)
{
today = new Date();
sec = today.getTime();
max = form.max.value;
form.txt.value="";
for(cnt=3; cnt<= max;cnt+=2)
{
sq = Math.round(Math.sqrt(cnt));
isprime = true;
for(i=3; i<=sq; i+=2)
{
if(cnt % i == 0)
{
isprime = false;
break;
}
}
if(isprime)
{
form.txt.value = form.txt.value + cnt + "\n";
form.last.value = ""+cnt;
delete today;
today = new Date();
t = Math.round((today.getTime()- sec)/1000);
m=0;
while(t >= 60)
{
m +=1;
t -= 60;
}
if(t<10) {
t = "" + "0"+t;
}
form.time.value = ""+m+":"+t ;
}
}
return false;
}
//-->
</script>
</head>
<body>
<font face="arial"><b>Prime Number Finder</b> in Java Script<br>
haplo@mindstab.net</font><br><br>
<i><b>Warning:</b> JavaScript prime number finding process is very slow, and locks browser while working, so don't set maximum too high.</i><br><br>
<form name="form">
<table>
<tr>
<td valign="top">
Maximum:<br>
<input name="max" value="1000" size="10"><br>
<input type="button" value="Calculate" onClick="primes(this.form)">
</td>
<td width="50">
&nbsp;
</td>
<td valign="top">
Time:<br>
<input name="time" value="0:00" size="5">
</td>
</tr>
<tr>
<td height="50">
&nbsp;
</td></tr>
<tr>
<td>
Primes:<br>
<textarea name="txt" cols="15" rows="10"></textarea>
</td>
<td width="50">
&nbsp;
</td>
<td valign="top">
Last Prime Number:<br>
<input name="last" value="0" size="15">
</td>
</tr>
</table>
</form>
</body>
</html>