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
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
Outside of code tags, you may need to use entities for some characters:
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.