【问题标题】:How do i find a factor of an integer in javascript?如何在javascript中找到整数的因子?
【发布时间】:2015-05-12 18:54:04
【问题描述】:

我需要创建一个计算器,用户输入一个数字,它会计算给定数字的因数、立方和平方。

下面是我正在使用的代码。我不知道如何计算出这个因素。任何意见,将不胜感激。

document.getElementById('calculate').addEventListener('click', estimateTotal);

function estimateTotal(event) {
	event.preventDefault();
	var initial2 = document.getElementById('initial').value;
	
	document.getElementById('factor').value = 0;
	document.getElementById('sqaure').value = initial2 * initial2;
	document.getElementById('cube').value = initial2 * initial2 * initial2;
}
<form id="calculator" method="POST">
    <p>Please enter a number between 0 and 50 <input name="initial" id="initial" type="text" size="20" required><button id="calculate">Calculate</button></p>
    <p>The Factorial of your number is: <input name="factor" id="factor" class="factor" type="text" size="20"></p>
    <p>The Square of your number is:<input name="sqaure" id="sqaure" class="sqaure" type="text" size="20"></p>
    <p>The Cube of your number is:<input name="cube" id="cube" class="cube" type="text" size="20"></p>
</form>

【问题讨论】:

  • 使用分解算法。在 Google 上搜索“整数分解算法”。见en.wikipedia.org/wiki/Integer_factorization。顺便说一句,您的 HTML 指的是“阶乘”;您对阶乘或因数(其中一个整数当然可能不止一个)感兴趣吗?
  • 因子!= 因子。您真正需要计算什么?
  • 明确因素,html 部分是错误的。 :)
  • @MinNaingOo 谢谢,我会看看 :)

标签: javascript html math


【解决方案1】:
  **recursive JavaScript function factor(n)**

 **Check this link:** **http://www.javascripter.net/math/primes/factorization.htm**

function factor(n) {
 if (isNaN(n) || !isFinite(n) || n%1!=0 || n==0) return ''+n;
 if (n<0) return '-'+factor(-n);
     var minFactor = leastFactor(n);
 if (n==minFactor) return ''+n;
 return minFactor+'*'+factor(n/minFactor);
}

【讨论】:

    【解决方案2】:

    对因子和阶乘都试试这个

    document.getElementById('calculate').addEventListener('click', estimateTotal);
    
    function estimateTotal(event) {
    	event.preventDefault();
    	var initial2 = document.getElementById('initial').value;
    	
    	document.getElementById('Factorial').value = fact(initial2);
      document.getElementById('factor').value = factors(initial2);
    	document.getElementById('sqaure').value = initial2 * initial2;
    	document.getElementById('cube').value = initial2 * initial2 * initial2;
    	
    }
    
    
       function fact(n)
                {
                    if(n == 0)
                        return 1;
                    else
                        return (n*fact(n-1));
                }
    
    
      function factors(num)
    {
     var
      n_factors = [],
      i;
     
     for (i = 1; i <= Math.floor(Math.sqrt(num)); i += 1)
      if (num % i === 0)
      {
       n_factors.push(i);
       if (num / i !== i)
        n_factors.push(num / i);
      }
     n_factors.sort(function(a, b){return a - b;});  // numeric sort
     return n_factors;
    }
    <p>Please enter a number between 0 and 50 <input name="initial" id="initial" type="text" size="20" required><button id="calculate">Calculate</button></p>
    <p>The Factorial of your number is: <input name="factor" id="Factorial" class="factor" type="text" size="20"></p>
    
    <p>The Factor of your number is: <input name="factor" id="factor" class="factor" type="text" size="20"></p>
    <p>The Sqaure of your number is:<input name="sqaure" id="sqaure" class="sqaure" type="text" size="20"></p>
    <p>The Cube of your number is:<input name="cube" id="cube" class="cube" type="text" size="20"></p>

    【讨论】:

      猜你喜欢
      • 2017-02-15
      • 1970-01-01
      • 2012-01-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-27
      相关资源
      最近更新 更多