Num Divisors

by Taylor Lopez

HTML

<body bgcolor="#FFFFFF" link="#0000FF" text="#000000" vlink="#660099">
<a name="top"></a>

<noscript><table width=720 border=0 cellspacing=1 cellpadding=1><tr><td>
<p align=center><font color=red face=Arial,Helvetica,sans-serif style="font-size:10pt;" ><b>
This calculator uses JavaScript.<br>
Please enable JavaScript and reload the page!
</b></font></p></td></tr></table></noscript>


<script type="text/javascript"><!--
google_ad_client = "pub-5507865760857847";
/* 728x90_20101016 */
google_ad_slot = "7989268040";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>

<p>
<table border="0" cellpadding="1" cellspacing="1">
<tbody><tr><td valign="top" width="564">

<table border="0" cellpadding="0" cellspacing="0" width="560"><tbody><tr><td>
<h3><font color="#000099" face="Verdana,Arial,Helvetica,sans-serif">

Divisors Calculator – <i>20 digits!</i>

</font></h3></td><!--td width=*--> <!--/td--><td valign="top" width="99"><!--g:plusone size="medium"--><!--/g:plusone--></td>
</tr></tbody></table>



<font face="Arial,Helvetica,sans-serif"><small>
<a class="cHead" href="../../index.htm"><img src="bt.gif" border="0" height="9" width="9">JavaScripter.net</a>
|
<a class="cHead" href="../index.htm">Math with JavaScript</a>
|
<a class="cHead" href="primefactorscalculator.htm">Prime Factors</a>
|
<a class="cHead" href="eulertotientfunction.htm">Totient</a>
|
<a class="cHead" href="100digitbigintcalculator.htm">100+ Digit Calculator</a>
</small></font>

<p>



<table border="0" cellpadding="1" cellspacing="0">
<tbody><tr>
<td class="cHead">&nbsp;Input a number:</td><td></td>
<td class="cHead"><nobr>Divisors (to select: click, Ctrl+A, Ctrl+C) 

<!--
<code>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</code>
<img border=0 width=9 height=9 src="bt.gif"><a class=cHead href="primefactorscalculator.htm">Prime Factors</a>
-->

</nobr></td>
<td class="cHead"><!--Run...

CSS

FORM  {
 font-family : Arial, Helvetica, sans-serif;
 font-size   : 10pt;
 margin      : 0px;
 padding     : 1px;
}

TEXTAREA  {
 font-family : Arial, Helvetica, sans-serif;
 font-size   : 10pt;
 overflow    : auto;
 width       : 320px;
 min-width   : 320px;
 max-width   : 320px;
 height      : 36px;
 min-height  : 36px;
}

.cHead  {
 font-family : Arial, Helvetica, sans-serif;
 font-size   : 10pt;
}

a.cHead       {
 text-decoration:none; 
 color:blue; 
}

a.cHead:hover  {
 text-decoration:underline; 
 color:blue; 
}

a.cHead:visited {
 text-decoration:none; 
 color:blue; 
}

JavaScript

/***************************************************************************\
    PRIMEFACTOR.JS
\***************************************************************************/

// function leastFactor(n) returns:
// * the smallest prime that divides n
// * NaN if n is NaN or Infinity
// *  0  if n is 0
// *  1  if n=1, n=-1, or n is not an integer

leastFactor = function(n) {
 if (isNaN(n) || !isFinite(n)) return NaN;  
 if (n==0) return 0;  
 if (n%1 || n*n<2) return 1;
 if (n%2==0) return 2;  
 if (n%3==0) return 3;  
 if (n%5==0) return 5;  
 var m = Math.sqrt(n);
 for (var i=7;i<=m;i+=30) {
  if (n%i==0)      return i;
  if (n%(i+4)==0)  return i+4;
  if (n%(i+6)==0)  return i+6;
  if (n%(i+10)==0) return i+10;
  if (n%(i+12)==0) return i+12;
  if (n%(i+16)==0) return i+16;
  if (n%(i+22)==0) return i+22;
  if (n%(i+24)==0) return i+24;
 }
 return n;
}

// Optimized version of leastFactor for Opera, Chrome, Firefox.
// In these browsers, "i divides n" is much faster as
// (q=n/i)==Math.floor(q)  than  n%i==0
if (
    navigator.userAgent.indexOf('Opera')  !=-1
 || navigator.userAgent.indexOf('Chrome') !=-1
 || navigator.userAgent.indexOf('Firefox')!=-1 )
{
 leastFactor = function(n) {
  if (isNaN(n) || !isFinite(n)) return NaN;   
  if (n==0) return 0;  
  if (n%1 || n*n<2) return 1;
  if (n%2==0) return 2;  
  if (n%3==0) return 3;  
  if (n%5==0) return 5;  
  var q, m = Math.sqrt(n);
  for (var i=7;i<=m;i+=30) {
   if ((q=n/i)==Math.floor(q))      return i;
   if ((q=n/(i+4))==Math.floor(q))  return i+4;
   if ((q=n/(i+6))==Math.floor(q))  return i+6;
   if ((q=n/(i+10))==Math.floor(q)) return i+10;
   if ((q=n/(i+12))==Math.floor(q)) return i+12;
   if ((q=n/(i+16))==Math.floor(q)) return i+16;
   if ((q=n/(i+22))==Math.floor(q)) return i+22;
   if ((q=n/(i+24))==Math.floor(q)) return i+24;
  }
  return n;
 }
}

// Optimized version for Internet Explorer avoids IE's 
// "slow script" warning at 5000000 script statements
// by grouping 48 divisibility checks...