Get all divisors for a number - Node.js Number

Node.js examples for Number:Algorithm

Description

Get all divisors for a number

Demo Code

Number.prototype.hasDiv = function( n ) {
  return Number.isInteger( this / n )
}

function sum( ary ) {
  var out = 0//from w w  w.  j a v  a  2  s.c om
  for (var n in ary)  {
    out += ary[n]
  }
  return out
}
function divisors( n ) {
  out = []
  for ( var i = 1; i < n; i++ ) {
    if ( n.hasDiv( i ) ) {
      out.push( i )
    }
  }
  return out
}

Related Tutorials