Javascript Number hasDiv( n )

Description

Javascript Number hasDiv( n )


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

function sum( ary ) {
  var out = 0//from  w ww.  ja  v a  2 s .c o  m
  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
}

function isPerfectN( n ) {
  if (sum(divisors( n)) == n) {
    return true
  }
  else {
    return false
  }
}

function perfectsUnder( n ) {
  ary = [ 1 ]
  for( var i = 1; i < n; i++ ) {
    if ( isPerfectN( i ) ) {
      ary.push( i )
    }
  }
  return ary
}



PreviousNext

Related