Nodejs Number Check isPerfPow(nroot)

Here you can find the source of isPerfPow(nroot)

Method Source Code

Number.prototype.isPerfPow = function(nroot){
  // check if it is an positive integer ommitted
  var primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53];
  var root,log2,t;
  t = Math.abs(this);//from w w  w  . j  a  va  2 s  . co m
  log2 = Math.floor(Math.log(t)/Math.LN2);
  // reduces the number of tries to 11. at most
  if(this.isPow2()) return true;
  // sqrt is optimized in most cases, so try it first
  root = Math.round(Math.sqrt(t));
  if(t - (root * root) == 0){
    if(arguments.length  > 0){
      nroot[0] = root;
      nroot[1] = 2;
    }
    return true
  }
  // cube root is only in the next ECMAScript standard, sadly
  for(var i=1;primes[i] <= log2;i++){
    root = Math.round(Math.pow(t,1/primes[i]));
    if(t - (Math.pow(root,primes[i])) == 0){
      if(arguments.length  > 0){
        nroot[0] = Math.round(root);
        nroot[1] = primes[i];
      }
      return true
    }
  }
  return false;
};

Related

  1. isInRangeisInRange(lower, upper)
    (function(){
    "use strict";
    Number.prototype.isInRange = function isInRange(lower, upper){
      if (this >= lower && this <= upper){
        return true;
      if (this <= lower && this >= upper){
        return true;
      return false;
    };
    
  2. isMultipleof(num)
    Number.prototype.isMultipleof = function (num) {
      if (this % num === 0) {
        return true;
      return false;
    var result = 0;
    for (var i = 1; i < 1000; i++) {
      if (i.isMultipleof(3) || i.isMultipleof(5) ) {
    ...
    
  3. isNegativeZero()
    Number.prototype.isNegativeZero = function() {
      var n = this;
      return n === 0 && (((n = +n) || (1 / n)) < 0);
    
  4. isPalindrome()
    Number.prototype.isPalindrome = function() {
      return this == this.reverse();
    var max = 0
      , x;
    for (var i = 100; i < 999; i++) {
      for (var j = i; j < 999; j++) {
        x = i * j;
        if (x.isPalindrome() && x > max) {
    ...
    
  5. isPositive()
    Number.prototype.isPositive = function() {
      return this > 0;
    
  6. isPositive()
    Number.prototype.isPositive = function(){
      return this > 0;
    var numb = new Number(10);
    console.log(numb.isPositive());