Mark Dominus on 2 Jul 2007 00:16:10 -0000


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: Math problem


> This is a bit of topic. My math skills are rusty to say the least.
> 
> I'm looking for the formula to calculate the number of iterations required
> to reduce N to 1 when N is divided by 2 (rounded up) each iteration.
> 
> To wit, N = 8
> iteration
> Number    	N
> 1		4
> 2		2
> 3		1

That is the logarithm to the base 2.

So:

        use POSIX 'ceil';
        for (1..16) {
           printf "%2d %1d\n", $_, ceil(log($_)/log(2));
        }


Hope this helps.
-
**Majordomo list services provided by PANIX <URL:http://www.panix.com>**
**To Unsubscribe, send "unsubscribe phl" to majordomo@lists.pm.org**