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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.