除数除数 [英] divisors of divisors of a number

查看:108
本文介绍了除数除数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我们尝试找到数字N的所有除数D(数组),则D的每个d的除数也会在D的计算中自动计算。有没有办法找到D的所有除数?通过找到所有除数的除数,然后最后将它们求和得出数字N。显然,有一种方法,但是我想要一种没有重复计算的方法。我认为这可以通过递归来完成,但是我不知道如何进行。我正在提供计算数字N的所有除数的实现。我想以某种方式扩展它,以便在计算N的除数的同时,还计算所有除数的除数(并保存它们)。最后,我可以将它们加起来并得到我想要的。但是在整个过程中,有益的一点是,我也无需任何额外的努力(即重复计算)就获得了所有除数的除数。

If we try to find all the divisors D(array) of a number N, then the divisors of each d in D is also automatically calculated in the calculation of D. Is there a way that we can find all the divisors of a number N by finding the divisors of all its divisors, and then finally summing them up. Obviously, there is a way but I want a way which doesn't have repetitive calculation. I think this can be done by recursion, but I am not getting how to proceed. I am providing my implementation of calculating all the divisors of a number N. I want to extend it in a way that, while calculating the divisors of N, I also calculate the divisors of all the divisors(and save them). Finally, I can add them up and get what I wanted. But in the whole process the beneficial point is that I also got the divisors of all divisors without any extra effort(ie, repetitive calculation).

function divisors_of_a_number($n)//returns all the divisors of a number in an unsorted array
{
$i=1;
$s=bcsqrt($n);
while($i<=$s)
{
if(!(bcmod($n,$i)))
{
    $a[]=$i;
    if($i!=$s)
    $a[]=bcdiv($n,$i);
}
++$i;
}
return $a;
}

在此举一个例子来说明这一点-
说,N为6所以N的除数是-{1,2,3,6}。现在, 3是N的除数。这意味着除以3的数字(即其除数)也将除以N。因此,我们可以说 3的除数将除以N。类似地,对于N的每个除数d,我们都可以说d的除数也是N的除数。因此,当我们计算N的除数时,我们将计算N的所有除数。我想要一种方法,在计算N = 6的除数时,我也得到{1},{2},{3}的除数(并保存它们)而无需进行额外的计算(因为它们已经为6计算了)

Here's an example to clarify this- Say, N is 6. So divisors of N are- {1,2,3,6}. Now, '3' is a divisor of N. It means that the numbers which divide '3'(ie, its divisors) will also divide N. Thus, we can say the divisors of '3' will divide N. Similarly, for every divisor d of N, we can say that the divisors of d are also the divisors of N. So, when we compute the divisors for N, we compute all the divisors of, each of its divisor d. I want a way that while computing divisors for N=6, I get the divisors of {1},{2},{3} also(and save them) without extra computation( because all of them are already being computed for 6).

作为一个实时示例,如果N = 20,我希望我的函数在某种意义上能够返回数组数组。
现在外部数组包含作为除数20的键,即键将为{20,10,5,4,2,1}。
现在,与这些键关联的是数组。这些数组中的每一个都是其各自键的除数。现在,如果我取所有内部数组的元素的交集,我将得到20的除数。这意味着,即使我只计算20的除数,也将计算所有元素。但是,我想获得所需的输出无需任何额外的计算。额外的计算意味着我显然可以计算除数为20的除数,然后返回数组数组。但是,我不想这样做,因为我知道所有20除数的除数本身都是在计算20除数的同时计算的。我希望这可以弄清所有事情。

As a live example, if N=20, I want my function to work in a sense that it returns an array of arrays. Now the outer array contains the keys as the Divisors of 20 ie, the keys will be {20,10,5,4,2,1}. Now, associated with these keys are arrays. Each of these arrays are the divisors of their respective keys. Now, if I take the intersection of elements of all the inner arrays, I will get the divisors of 20. It means, all the elements are calculated even if I only calculate the divisors of 20. But, I want to get the required output without any extra computation. Extra computation means that I obviously can calculate the divisors of divisors of 20 and return the array of arrays. But, I don't want to do it because the I know "the divisors of all the divisors of 20 are themselves calculated while the calculation of divisors of 20". I hope this clarifies all the things.

推荐答案

我不确定您要什么,但是我认为这个简单的脚本可以为您提供帮助。让我知道它是否足够

am not sure what you want but i think this simple script can help you .. let me know if its sufficient or not

尝试

function arrayOfDivisors($x) {
    $divisors = array ();
    for($i = 1; $i < $x; $i ++) {
        if ($x % $i == 0) {
            $divisors [] = $i;
        }
    }
    return $divisors;
}

$divisors = arrayOfDivisors ( 20 );
var_dump ( $divisors ); // List Divisors
var_dump ( array_sum ( $divisors ) )  // Total

Output

array
  0 => int 1
  1 => int 2
  2 => int 4
  3 => int 5
  4 => int 10


int 22   // Total 

这篇关于除数除数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆