【问题标题】:How to find numbers divisible by multiple numbers using javascript如何使用javascript查找可被多个数字整除的数字
【发布时间】:2013-11-03 13:55:24
【问题描述】:

我从学习代码中休息了一段时间。我回来了,一切都从我的脑海中消失了。我想制作一个简单的程序来查找可被任何整数整除的数字(例如可被 5、6、9、10 或 4、7、25 整除)

到目前为止我做到了:

var multipleOf = function() {
    for (var i = 1; i < 1000; i*2) {
        if (i%3 === 0) {
            if (i%4 === 0) {
                if (i%5 === 0) {
                    return(i);
                }
            }
        }
    }
};

但是有一些问题。 它似乎没有按原样工作(我认为一些python可能会滑入其中) 它不可扩展,我必须为不同数量的数字更改代码(例如,所有素数 1-100 将是很多编码,而不是仅仅输入数字)

谁能帮我编写可以像这样运行的代码:

console.log(multipleOf(2,5,8,12,15,17,20))

【问题讨论】:

    标签: javascript math var


    【解决方案1】:

    JavaScript 有一个巧妙的方法可以轻松定义参数,恰当地命名为 arguments

    据我了解,您实际上要查找的是所提供参数的最小公倍数,这是一个相当简单的操作。这是一种方法,使用辅助函数来计算一对数字的 LCM,而这又需要一个函数来计算它们的 GCD。

    function multipleOf() {
        function gcf(a, b) { 
            return ( b == 0 ) ? a : gcf(b, a % b); 
        }
        function lcm(a, b) { 
            return a * b / gcf(a,b); 
        }
        function recurse(ar) {
            if (ar.length > 1) {
                // take the first two numbers, caculate their LCM, and put the result
                // back into the stack. Reduces array length by 1.
                ar.push( lcm( ar.shift() , ar.shift() ) );
                return recurse( ar );
            }
            else return ar[0];
        }
        // take arguments and build an array
        // arguments is an array-like object, but it doesn't have methods
        // such as `shift` or `push`, required for `recurse`
        // take this opportunity to ensure we have numbers.
        var ar = [], l = arguments.length, i;
        for( i=0; i<l; i++) ar[i] = parseInt(arguments[i],10);
        return recurse(ar);
    }
    

    【讨论】:

    • 谢谢,这是一个非常有用的答案,帮助很大! (对不起,我没有足够的代表来投票)
    【解决方案2】:

    到目前为止,这些解决方案似乎都指向构建一个函数来判断数字 X 是否可以被整数数组 [a,b,c,d] 整除。

    我相信使用sieve of Eratostenes 之类的东西来避免查询每个可能的数字并建立一个符合要求的数字列表会更有效。

    我会开始将原始输入扩展到每个值的每个主要除数,然后丢弃重复项。从此,我们知道素数数组[a,b,c]的所有倍数都以

    的形式出现
    Math.pow(a,x) * Math.pow(b,y) * Math.pow(c,z)
    

    唯一尚未定义的逻辑是,我应该按什么顺序调整每个权力以找到下一个数字。

    【讨论】:

      【解决方案3】:

      您可以传递一个数组而不是多个编号。

      var multipleOf = function(nos) {
         for(var i=0; i< nos.length; i++) {
             // Now you can use,
            //nos[i]; to access each passed integer.
         }
      };
      
      var nos = [2,5,8,12,15,17,20];
      
      multipleof(nos);
      

      或者您可以使用arguments

      var multipleOf = function() {
         // Loop through arguments
         for(var i=0; i< arguments.length; i++) {
            // Now you can use,
            //arguments[i]; to access each passed integer.
      
         }
      };
      
      multipleOf(2,5,8,12,15,17,20);
      

      arguments 是一种特殊类型的数组(虽然它不是真实的),您可以循环通过它来接受传递的参数。

      【讨论】:

      • 另一个有趣的问题。感谢您的回复,很抱歉,我无法为答案投票。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多