找到所有可分的数字给出一组可分的

时间:2017-02-21 13:37:47

标签: javascript primes algebra prime-factoring khan-academy

在给定数字集的情况下找出其他常见的可分数数

我本质上是想用javascript来解决视频中的内容。我可以得到所有主要因素。但我不确定如何产生组合的素数因子。然后找到所有的修正。

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

e.g。 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

1 个答案:

答案 0 :(得分: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

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

但是,如果您重新排列逻辑,则说明&#34;哪些数字除以INTO 9或24&#34;我不同意这种逻辑。再一次,扩大这一点并不困难。虽然,我很想清理我的功能,做一件事,只做一件事。在Java中,最有可能包括与javascript不同的清晰编码实践。

如果这对您的问题没有帮助,请更准确地描述您想要做的事情。我可以重新编写代码,然后将其翻译成javascript (我在javascript中体面,但在Java中更强)