Compute Prime Composite Factors

[Total: 0    Average: 0/5]

This will break down a number in the prime composite factors of that number.

Head Code:

<head>
/*
 * This computes the prime composite break down factors.
 */
<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 printFactors(_v)
{
 if(isPrime(_v)) out(_v);
 else
  for(var i=2;i<=(_v/2);i++)
    if(_v%i==0 && isPrime(i))
    {
      out(i);
      factors=printFactors(_v/i);
      break;
    }
}
function out(_i)
{
 var _r=document.form1.result;
 _r.value=_r.value?_r.value.toString()+","+_i.toString():_i.toString();
}
function reduce()
{
 var _f=document.form1;
 var _s=parseInt(_f.start.value);
 _f.result.value="";
 printFactors(_s);
}
</script>

Body Code:

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

Times Viewed: 4

Leave a Reply

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