php 找出两个数字相加并乘以某物

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9604744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 07:14:43  来源:igfitidea点击:

Find what 2 numbers add to something and multiply to something

phpnumbersfactoring

提问by Belgin Fish

Hey so I'm making a factoring program and I'm wondering if anyone can give me any ideas on an efficient way to find what two numbers multiple to a specified number, and also add to a specified number.

嘿,所以我正在制作一个因式分解程序,我想知道是否有人可以给我任何关于找到两个数字乘以指定数字的有效方法的想法,并且还添加到指定数字。

for example I may have

例如我可能有

(a)(b) = 6

(a)(b) = 6

a + b = 5

a + b = 5

So essentially i just need a way to find the a and b values. In this case they would be 2 and 3.

所以基本上我只需要一种方法来找到 a 和 b 值。在这种情况下,它们将是 2 和 3。

Can anyone give me any ideas on where to start? Negative numbers must also be considered for use.

谁能给我任何关于从哪里开始的想法?还必须考虑使用负数。

回答by danii

Come on guys, there is no need to loop, just use simple math to solve this equation system:

来吧伙计们,不需要循环,只需使用简单的数学来解决这个方程组:

a*b = i;

a*b = i;

a+b = j;

a+b = j;

a = j/b;

a = j/b;

a = i-b;

a = ib;

j/b = i-b; so:

j/b = ib; 所以:

b + j/b + i = 0

b + j/b + i = 0

b^2 + i*b + j = 0

b^2 + i*b + j = 0

From here, its a quadratic equation, and it's trivial to find b(just implement the quadratic equation formula) and from there get the value for a.

从这里,它的一元二次方程,这是微不足道的发现b(只需实现二次方程公式),并从那里得到了价值

EDIT:

编辑:

There you go:

你去吧:

function finder($add,$product)
{

 $inside_root = $add*$add - 4*$product;

 if($inside_root >=0)
 {

     $b = ($add + sqrt($inside_root))/2;
     $a = $add - $b;

     echo "$a+$b = $add and $a*$b=$product\n";

 }else
 {
   echo "No real solution\n";
 }
}

Real live action:

真人实拍:

http://codepad.org/JBxMgHBd

http://codepad.org/JBxMgHBd

回答by DaveRandom

Here is how I would do that:

这是我将如何做到这一点:

$sum = 5;
$product = 6;

$found = FALSE;
for ($a = 1; $a < $sum; $a++) {
  $b = $sum - $a;
  if ($a * $b == $product) {
    $found = TRUE;
    break;
  }
}

if ($found) {
  echo "The answer is a = $a, b = $b.";
} else {
  echo "There is no answer where a and b are both integers.";
}

Basically, start at $a = 1and $b = $sum - $a, step through it one at a time since we know then that $a + $b == $sumis always true, and multiply $aand $bto see if they equal $product. If they do, that's the answer.

基本上,在启动$a = 1$b = $sum - $a,通过它一步一个脚印的时间,因为我们后来才知道,$a + $b == $sum始终是真实的,并多次$a$b看他们是否相等$product。如果他们这样做,那就是答案。

See it working

看到它工作

Whether that is the most efficientmethod is very much debatable.

这是否是最有效的方法值得商榷。

回答by Brendon Dugan

With the multiplication, I recommend using the modulo operator (%) to determine which numbers divide evenly into the target number like:

对于乘法,我建议使用模运算符 (%) 来确定哪些数字可以平均分为目标数字,例如:

$factors = array();
for($i = 0; $i < $target; $i++){
    if($target % $i == 0){
        $temp = array()
        $a = $i;
        $b = $target / $i;
        $temp["a"] = $a;
        $temp["b"] = $b;
        $temp["index"] = $i;
        array_push($factors, $temp);
    }
}

This would leave you with an array of factors of the target number.

这将为您留下一系列目标数字的因子。

回答by Michael Borgwardt

That's basically a set of 2 simultaneous equations:

这基本上是一组 2 个联立方程

x*y = a
X+y = b

(using the mathematical convention of x and y for the variables to solve and a and b for arbitrary constants).

(对要求解的变量使用 x 和 y 的数学约定,对任意常数使用 a 和 b 的数学约定)。

But the solution involves a quadratic equation (because of the x*y), so depending on the actual values of a and b, there may not be a solution, or there may be multiple solutions.

但是解涉及二次方程(因为x*y),所以根据a和b的实际值,可能没有解,也可能有多个解。