Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Stop once boy.

By: a guest on Dec 21st, 2010  |  syntax: Perl  |  size: 0.56 KB  |  views: 50  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # For this: http://www.thebigquestions.com/2010/12/21/are-you-smarter-than-google/
  2.  
  3. my($tg, $tb) = (0, 0);
  4. my $fams = shift @ARGV || 10;
  5.  
  6. my $N=0;
  7. my @families = map { [0, 0] } (1 .. $fams);
  8.  
  9. while($tb < @families) {
  10.   for(my $i=0; $i<@families; $i++) { # 1000 couples trying for a boy
  11.     next if $families[$i][1];
  12.     if(rand() < 0.5) { # Girl
  13.       $families[$i][0]++;
  14.       $tg++;
  15.     } else {
  16.       $families[$i][1]++;
  17.       $tb++;
  18.     }
  19.   }
  20.   printf "Generation %d: girls=%d, boys=%d, ratio=%d%%\n", ++$N, $tg, $tb, 100 * ($tg / ($tg + $tb));
  21. }