Beefy Boxes and Bandwidth Generously Provided by pair Networks DiBona
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Behold! The power of recursion.

by pg (Canon)
on Oct 18, 2004 at 04:32 UTC ( #400058=note: print w/ replies, xml ) Need Help??


in reply to Behold! The power of recursion.

This program tells how many guesses you need for a number range, including total number of guesses for all numbers in the range, and the average: (the basic idea behind it is that, you have 1 number that you can hit by exactly 1 guess, you have 2 numbers that you can hit by exactly 2 guesses, you have 4 numbers that you can hit by exactly 3 guesses... you have 2 ** n numbers that you can hit by exactly n + 1 guesses...)

use strict; use warnings; my $total; for my $i (1 .. 20) { my $last = 2 ** $i - 1; $total += $i * 2 ** ($i - 1); my $avg =$total / (2 ** $i - 1); print "For range 1 .. $last, total guesses = $total, avg = $avg\n" +; }

Output:

For range 1 .. 1, total guesses = 1, avg = 1 For range 1 .. 3, total guesses = 5, avg = 1.66666666666667 For range 1 .. 7, total guesses = 17, avg = 2.42857142857143 For range 1 .. 15, total guesses = 49, avg = 3.26666666666667 For range 1 .. 31, total guesses = 129, avg = 4.16129032258065 For range 1 .. 63, total guesses = 321, avg = 5.09523809523809 For range 1 .. 127, total guesses = 769, avg = 6.05511811023622 For range 1 .. 255, total guesses = 1793, avg = 7.03137254901961 For range 1 .. 511, total guesses = 4097, avg = 8.01761252446184 For range 1 .. 1023, total guesses = 9217, avg = 9.00977517106549 For range 1 .. 2047, total guesses = 20481, avg = 10.0053737176356 For range 1 .. 4095, total guesses = 45057, avg = 11.0029304029304 For range 1 .. 8191, total guesses = 98305, avg = 12.0015871078012 For range 1 .. 16383, total guesses = 212993, avg = 13.0008545443447 For range 1 .. 32767, total guesses = 458753, avg = 14.0004577776421 For range 1 .. 65535, total guesses = 983041, avg = 15.0002441443503 For range 1 .. 131071, total guesses = 2097153, avg = 16.0001297006966 For range 1 .. 262143, total guesses = 4456449, avg = 17.0000686648127 For range 1 .. 524287, total guesses = 9437185, avg = 18.0000362396931 For range 1 .. 1048575, total guesses = 19922945, avg = 19.00001907350 +45


Comment on Re: Behold! The power of recursion.
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://400058]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2014-04-19 19:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls