Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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;
}

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).

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
220 views
Welcome To Ask or Share your Answers For Others

1 Answer

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

Try

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...