Math to Compute Next Prime Number

[Total: 0    Average: 0/5]

This will compute prime numbers.

Head Code:

<head>
/*
 * This computes the next prime number in sequence
 */
<script>
function isPrime(_n)
{
 var _isPrime=true;
 var _sqrt=Math.sqrt(_n);
 for(var _i=2;_i<=_sqrt;_i++)
  if((_n%_i)==0) _isPrime=false;
 return _isPrime;
}
function nextPrime(_s,_n)
{
  while(_n>0)if(isPrime(_s++))_n--;
  return --_s;
}
function reduce()
{
 var _f=document.form1;
 var _s=parseInt(_f.start.value);
 var _n=parseInt(_f.nums.value);
 _f.result.value=nextPrime(_s,_n);
}
</script>

Body Code:

<form name=form1>
Start<input type=text name=start value=14><BR>
Number<input type=text name=nums value=2><BR>
Result<input type=text name=result><BR>
<input value="Compute Now" type=button onclick="reduce()">
</form>

Times Viewed: 26

Leave a Reply

Your email address will not be published. Required fields are marked *