Thread: Pop quiz!
View Single Post
  #77  
Old 08-23-2004, 07:45 PM
Draknek's Avatar
Draknek Draknek is offline
Cursory
Member
 
Join Date: Mar 2003
Location: Bristol, England
Posts: 165
Send a message via MSN to Draknek
Default

That looks like the graph for 5 to an infinite number of balls.

Cat, are you sure that it's 9 tries minimum for 3 balls? I get 8.

EDIT: Nevermind. I wrote a program, which agrees with you.

Code:
#!/usr/bin/perl -w

use strict;

# Edit the next two values as appropriate

my $ballcount = 2;
my $floorcount = 100;

my $tries;
for ($tries = 0; 1; $tries++)
{
  if (howmanyfloors($tries,$ballcount) >= $floorcount)
  {
    print "It takes a maximum of $tries tries to check $floorcount floors with $ballcount balls.\n";
    exit;
  }
}

sub howmanyfloors
{
  my $tries = shift;
  my $ballcount = shift;
  
  # Can't check any floors without any balls
  return 0 if ($ballcount < 1);
     
  my $floors = 0;
  while ($tries > 0)
  {
    $tries -= 1;
    $floors += howmanyfloors($tries,$ballcount-1) + 1;
  }
  
  return $floors;
}
__________________
Self-referential sigs do not a humourous poster make.
Reply With Quote