简体   繁体   English

最大公因数解说明

[英]Greatest common factor solution explanation

Have the function Division(num1,num2) take both parameters being passed and return the greatest common factor.让函数Division(num1,num2)接受传递的两个参数并返回最大公因数。 That is, return the greatest number that evenly goes into both numbers with no remainder.也就是说,返回平均进入两个数字且没有余数的最大数字。 For example: 12 and 16 both are divisible by 1, 2, and 4 so the output should be 4. The range for both parameters will be from 1 to 10^3.例如:12 和 16 都可以被 1、2 和 4 整除,所以输出应该是 4。这两个参数的范围都是从 1 到 10^3。 Can anyone please explain the solution here?任何人都可以在这里解释解决方案吗?

function Division(num1, num2) { 
    var largestFactor = 1; 
    var smallerNum; 

    if (num1 > num2) { 
        smallerNum = num2; 
    } else { 
        smallerNum = num1; 
    }          

    for (i = 1; i <= smallerNum; i++) { 
        if ((num1 % i === 0) && (num2 % i === 0) && (i > largestFactor)) { 
            largestFactor = i; 
        } 
    } 
    return largestFactor; 
} 
  • Why set a new variable = 1?为什么要设置一个新变量 = 1?
  • Why set second variable?为什么要设置第二个变量?
  • Why is it that with num1 > num2 , we get num2=smallerNum , and vice versa?为什么num1 > num2 ,我们得到num2=smallerNum ,反之亦然?
  • And explain why i <= smallerNum if i > largestfactor ?并解释为什么i <= smallerNum if i > largestfactor

why set aa new variable = 1?为什么设置一个新变量= 1?

It's a default value, so it can compare it to the counter.这是一个默认值,因此它可以将其与计数器进行比较。 All numbers different from 0 have a common denominator of 1. Since you want the variable to be used only within the context of the function and not create a global variable, you define the variable outside the loop.所有不为 0 的数字都有一个公分母为 1。由于您希望变量仅在函数的上下文中使用而不是创建全局变量,因此您可以在循环外定义该变量。 You didn't need to set it up to 1, because your loop already covers that, but you'll need to get rid of that pointless comparison (look at the bottom of this answer).您不需要将其设置为 1,因为您的循环已经涵盖了这一点,但是您需要摆脱这种毫无意义的比较(查看此答案的底部)。

why set second variable?为什么设置第二个变量?

There can't be a larger common denominator that is larger than one of the numbers.不能有一个大于其中一个数字的更大的公分母。 Hence, you should only check numbers until you reach the smallest one.因此,您应该只检查数字,直到达到最小的数字。

why if num1>num2 , num2=smallerNum, vice versa?为什么如果 num1>num2 ,num2=smallerNum,反之亦然?

If num1 is bigger than num2, num2 is the smaller one.如果 num1 大于 num2,则 num2 是较小的。 Viceversa, if num2 is bigger than num1, num1 is the smaller one.反之亦然,如果 num2 大于 num1,则 num1 是较小的。

and if please explain this part...why i<=smallerNum and if i > largestfactor?如果请解释这部分...为什么 i<=smallerNum 并且如果 i > maximumfactor?

The loop goes from 1 to the smallest number.循环从 1 到最小的数字。 i <= smallerNum says.. while i is smaller or equal than smallerNum keep running. i <= smallerNum说.. 而i小于或等于smallerNum继续运行。

i > largestfactor is comparing the last known largest factor to the new one. i > largestfactor将最后一个已知的最大因素与新的因素进行比较。 Kind of pointless to be honest (it will always return true, except for the first iteration because 1 is not bigger than 1).老实说有点毫无意义(它总是返回真,除了第一次迭代,因为 1 不大于 1)。

It sets largestFactor to be 1 because 1 is the minimum GCF for any two numbers greater than 0 and largestFactor is increased later on to get the actual GCF.它将largestFactor设置为 1,因为 1 是任何两个大于 0 的数字的最小 GCF,并且largestFactor会增加以获得实际的 GCF。

smallerNum is technically not needed for the functionality, but it'd increase performance by setting it and using it in the loop rather than calculating it on each iteration. smallerNum技术上讲,该功能不需要smallerNum ,但它可以通过设置它并在循环中使用它来提高性能,而不是在每次迭代时计算它。 It does not change.它不会改变。

If num1>num2 , num2 is smaller, so we use smallerNum = num2 to set smallerNum to be equal to num2 .如果num1>num2 ,则num2较小,因此我们使用smallerNum = num2smallerNum设置为等于num2 Note that this is very different from num2 = smallerNum as that sets num2 to be equal to smallerNum , changing the value of num2 rather than smallerNum .注意,这是非常不同num2 = smallerNum作为集num2等于smallerNum ,改变的值num2而非smallerNum Order of the values when using the assignment operator = is very important.使用赋值运算符=时的值顺序非常重要。 The same logic applies when num2>num1 , which is the purpose of the else.同样的逻辑适用于num2>num1 ,这是 else 的目的。

As the factors of a number is always less than or equal to the number, the GCF of the two numbers is always going to be less than or equal to the smaller number.由于一个数的因数总是小于或等于该数,所以两个数的 GCF 总是会小于或等于较小的数。 This is why we calculate the smaller number in the beginning.这就是为什么我们在开始时计算较小的数字。 The i<=smallerNum tells the loop to keep running as long as i is less than or equal to smallerNum and stop once that is no longer true. i<=smallerNum告诉循环只要i小于或等于smallerNum就继续运行,并在不再为真时停止。 This is needed to make sure we don't increase i infinitely and since any number larger than the smaller number is irrelevant, we can stop once i is equal to smallerNum .这是需要确保我们不会无限增加i并且由于任何大于较小数字的数字都是无关紧要的,一旦i等于smallerNum ,我们就可以停止。

i > largestFactor compares if the current number being tested is larger than the previous found factor. i > largestFactor比较当前正在测试的数字是否大于先前找到的因子。 This isn't needed and can be removed since our loop only goes up.这不是必需的,可以删除,因为我们的循环只会上升。 Having this extra calculation would only slow down the program.进行这种额外的计算只会减慢程序的速度。

I hope to solve your problem我希望能解决你的问题

  for($i = $num1; $i > 0; $i --){
    if ($num1%$i == 0 && $num2%$i == 0){
      return $i;
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM