Note that there are some explanatory texts on larger screens.

plurals
  1. POdivisors of divisors of a number
    text
    copied!<p>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). </p> <pre><code>function divisors_of_a_number($n)//returns all the divisors of a number in an unsorted array { $i=1; $s=bcsqrt($n); while($i&lt;=$s) { if(!(bcmod($n,$i))) { $a[]=$i; if($i!=$s) $a[]=bcdiv($n,$i); } ++$i; } return $a; } </code></pre> <p>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).</p> <p>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.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload