0

在给定一组数字的情况下找到其他常见的整除数

我本质上是在尝试使用 javascript 解决视频中的内容。我可以得到所有的主要因素。但我不确定如何产生组合的主要因素数组。然后找到它的所有排列。

https://youtu.be/zWcfVC-oCNw?t=4m32s

例如X,一个数是否可​​以被 9 和 24 整除。因此X也可以被....整除?

function primeFactors(num) {
    let n = num
    let divisor = 2;
    let primes = [];
    while (divisor <= num) {
        if (num % divisor === 0) {
            primes.push(divisor);
            num /= divisor;
        } else {       
            divisor++;
        }

    }

    return primes;
}

console.log(primeFactors(9)) // [3, 3]
console.log(primeFactors(24)) //[2, 2, 2, 3]

// All combinations of [2, 2, 2, 3, 3] are valid answers
4

1 回答 1

0

Java中的解决方案。

/*
* Example solution to finding all possible numbers divisible by two different numbers.
* In response to stack overflow question at the following URL:
* https://stackoverflow.com/questions/42368860/find-all-common-divisible-numbers-given-set-of-divisibles
* Author James Pata
* Date 01/05/18
*/

public class primeFactorization
{
   public static void main(String[] args)
   {
      //CHOOSE ANY RANGE OF NUMBERS (>= 1 only)
      int testRange = 216;

      //CHOOSE ANY Two Numbers to Compare Divisibility Similarities 
      int number1 = 9;
      int number2 = 24;

      System.out.println("All numbers divisble by either " + number1 + " and " + number2 + " are: \n");      

      /*
      *The for loop will iterate through the testRange of numbers to be analyzed starting at 1.
      *The for loop has two local variables called tempVariabel that holds the remainder.
      *These tempVariables will be used to determine if divisible, and will then print corresponding confirmation
      * The final else statement was excluded intentionally to prevent the index variable, testNumber, from printing
      */
      for(int testNumber = 1; testNumber <= testRange; testNumber++) 
      {

         int tempVariable1 = testNumber%number1;
         int tempVariable2 = testNumber%number2;

         if(isNumberDivisibleBy(number1, testNumber) && isNumberDivisibleBy(number2, testNumber)) {
            System.out.println(testNumber + " is divisible by both " + number1 + " and " + number2);
            } else if(isNumberDivisibleBy(number1, testNumber)) {
                  System.out.println(testNumber + " is divisible by " + number1);
               } else if(isNumberDivisibleBy(number2, testNumber)) {
                  System.out.println(testNumber + " is divisible by " + number2);
                  }
       }

    }//endmain

    /*
     *The isNumberDivisbileBy takes two parameters, and returns true if
     *parameter testNumber is evenly divisible by number of choosing.
     *(testNumber is determined later determined by the for loop in the main method.)
     */   
    public static boolean isNumberDivisibleBy(int number, int testNumber)
    {
      if (testNumber%number == 0) {
         return true;
         } else { return false; }
    }

}//endclass

结果:

All numbers divisble by either 9 andS 24 are: 

9 is divisible by 9
18 is divisible by 9
24 is divisible by 24
27 is divisible by 9
36 is divisible by 9
45 is divisible by 9
48 is divisible by 24
54 is divisible by 9
63 is divisible by 9
72 is divisible by both 9 and 24
81 is divisible by 9
90 is divisible by 9
96 is divisible by 24
99 is divisible by 9
108 is divisible by 9
117 is divisible by 9
120 is divisible by 24
126 is divisible by 9
135 is divisible by 9
144 is divisible by both 9 and 24
153 is divisible by 9
162 is divisible by 9
168 is divisible by 24
171 is divisible by 9
180 is divisible by 9
189 is divisible by 9
192 is divisible by 24
198 is divisible by 9
207 is divisible by 9
216 is divisible by both 9 and 24

如果您想包含低于两个所选数字的数字以进行比较,可以轻松扩展此代码以包含该功能。但是,从数学上讲,我认为任何小于两个给定数字的数字都不能被 9 或 24 整除。(因为结果总是一个不正确的分数,因此小数小于一)

但是,如果您重新排列逻辑,以说明“哪些数字除以 9 或 24”,我会同意这种逻辑。再一次,扩展它并不困难。虽然,我很想清理我的功能来做一件事,而且只做一件事。在 Java 中,很可能包括与 javascript 不同的干净编码实践。

如果这不能帮助回答您的问题,请更详细地描述您到底想要做什么。我可以重新编写代码,然后将其翻译成 javascript(我在 javascript 方面表现不错,但在 Java 方面更强大)

于 2018-01-05T09:52:10.193 回答