Home  >  Article  >  Web Front-end  >  jQuery method to find perfect numbers within n_jquery

jQuery method to find perfect numbers within n_jquery

WBOY
WBOYOriginal
2016-05-16 15:53:001530browse

The example in this article describes how jQuery finds perfect numbers within n. Share it with everyone for your reference. The specific analysis is as follows:

The perfection of a number depends on its factors (those numbers that can divide the original number).

For example: the factors of 12 are 1, 2, 3, 4 and 6. When the sum of the factors of a number is greater than the number itself, the number is called a "surplus" number. So 12 is a surplus number because its factors add up to 16. On the other hand, when the sum of the factors of a number is less than the number itself, the number is called a "deficient" number. So 10 is a deficit number because its factors (1, 2, and 5) only add up to 8.

The most meaningful and rarest numbers are those whose factors add up to exactly themselves. These numbers are perfect numbers.

-- "Fermat's Last Theorem"

To find a perfect number, you must first calculate the factors of the number. Baidu reviews what factors are.

Factor: If an integer n is divided by m, the result is an integer with no remainder, then we say that m is a factor of n. It should be noted that this relationship is only true when the dividend, divisor, and quotient are all integers and the remainder is zero. Conversely, we call n a multiple of m.

<!DOCTYPE html> 
<html> 
<head> 
 <meta charset="utf-8"> 
 <title>JS Bin</title> 
</head> 
<body> 
 <input type="text" id="num"/> 
 <button id="calc">计算</button> 
 <p id="result"></p> 
</body> 
</html> 

/* 
 * 寻找n以内的完美数 
 */ 
function $(id){ 
 return document.getElementById(id); 
} 
//判断是否为正整数 
function isIntNum(number){ 
 var num = number; 
 if((!isNaN(num)) && (parseInt(num) == parseFloat(num))){ 
  return true; 
 }else{ 
  return false; 
 } 
} 
$("calc").addEventListener("click",function(){ 
 var inputNum = $("num").value, 
   $result = $("result"), 
   factorArr = [], 
   resultArr = [], 
   i = 0, 
   j = 0, 
   sum = 0; 
 //检验输入是否为正整数 
 if(isIntNum(inputNum)){ 
  console.log("right"); 
 }else{ 
  $result.innerHTML = "输入错误:请输入正整数"; 
  return false; 
 } 
 //遍历所有数字 
 for(var k = 1;k < inputNum;k++){ 
  //每次计算需重置变量 
  factorArr.length = 0; 
  sum = 0; 
  //寻找当前数字的因数 
  for(i = 1;i < Math.floor(k/2)+1; i++){ 
   if(k % i === 0){ 
    factorArr.push(i); 
   } 
  } 
  //计算因数之和 
  for(var m = 0;m < factorArr.length;m++){ 
   sum += factorArr[m]; 
  } 
  //因素和等于当前数,则符合完全数标准 
  if(sum === k){ 
   resultArr.push(k); 
  } 
 } 
 $result.innerHTML=resultArr; 
});

I hope this article will be helpful to everyone’s jQuery programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn