Sunday, December 2, 2012

Birthday Paradox

Another exercise from programmingpraxis.com. Original post can be found here.

#!/usr/bin/php
<?php

if ($argc != 3) {
   echo "Usage ".__FILE__." <numberOfPeople> <loopCount>".PHP_EOL;
   exit;
}
$num = $argv[1];
$loops = $argv[2];

echo "Running $loops tests with $num people...".PHP_EOL;

$numDupes = 0;
$tenth = floor($loops/10);
for ($i = 1; $i <= $loops; $i++) {

   $peopleArr = array();
   $dupe = FALSE;
   do {
      $birthday = makeBirthday();
      if (isset($peopleArr[$birthday])) {
         //We found a duplicate...
         $dupe = TRUE;
         break;
      }
      $peopleArr[$birthday] = TRUE;
   } while (count($peopleArr) < $num);

   if ($dupe) {
      $numDupes++;
   }

   if ($i%$tenth == 0) {
      echo "After $i tests, ".round((($numDupes/$i)*100),2)."%".PHP_EOL;
   }
   set_time_limit(10);
}

$overlaps = Round((($numDupes/$loops)*100),2);
echo "Found overlapping birthdays ".$overlaps."% of the time...".PHP_EOL;

function makeBirthday () {
   if (mt_rand(1,4) == 4) { //leap year
      $max = 366;
   } else { //normal year
      $max = 365;
   }
   return mt_rand(1,$max);
}

Saturday, December 1, 2012

Taxicab Number Test

My take on a taxicab number test routine. The original exercise can be found here.

#!/usr/bin/php
<?php

if ($argc != 2) {
   echo "Usage cube.php <number>".PHP_EOL;
   exit;
}
$num = $argv[1];

echo "Finding cubes less than ".$num.PHP_EOL;

$factor = 1;
$arr = array();
$bottom = array();
$top = array();
$half = floor($num/2);
//Modified the below loop to parition the cubes while its generating them.
//Thus preventing another loop to partition the cubes
do {
   $cube = pow($factor,3);
   if ($cube <= $half) {
      $bottom[$cube] = $factor;
   } else {
      $top[$factor] = $cube;
   }
   $factor++;
} while ($cube < $num);

echo "Found ".count($top)." cubes > lower half of list".PHP_EOL;
echo "Found ".count($bottom)." cubes <= lower half of list".PHP_EOL;
$total = (count($top)+count($bottom));
$perc = count($top)/$total;
echo "Only testing ".(round($perc*100,2))."%".PHP_EOL;

foreach ($top as $factor => $product) {
   $remainder = $num-$product;
   if (isset($bottom[$remainder])) {
      echo "Found ".$factor."^3 + ".$bottom[$remainder]."^3 = ".$num.PHP_EOL;
   }
}

Friday, September 14, 2012

Schedule Nagios Downtime From Powershell

After finally getting some time to setup Nagios at my work, we have some real availability monitoring! However in a small and somewhat neglected environment alerting can be a double edged sword. While we have learned some things, we are having intermittent issues with out mail server services. We also are getting alert bombed due to some automated reboots on our network. Now there are several options for solving this that exist in the Nagios community (I.E. this). The simple and workable solution we found was to tie the downtime scheduling right into the Powershell script. Here is the script in case it can help anyone else out!

[Reflection.Assembly]::LoadWithPartialName("System.Web") > $null
$url = "http://yourdomain/nagios/cgi-bin/cmd.cgi";
$ajax = new-object -com msxml2.xmlhttp;
$ajax.open("POST",$url,$false, "httpAuthUsername",
    "httpAuthPassword");
$ajax.setRequestHeader("Content-Type",
    "application/x-www-form-urlencoded");
$startTime = "{0:MM-dd-yyyy} 12:30:00" -f (Get-Date);
$endTime = "{0:MM-dd-yyyy} 13:30:00" -f (Get-Date);
$postStrMain = "cmd_typ=55&cmd_mod=2&"+
    "com_author=httpAuthUsername&"+ 
    "com_data=Automated Downtime Scheduled&"+
    "trigger=0&"+ 
    "start_time="+
    [System.Web.HttpUtility]::UrlEncode($startTime)+"&"+
    "end_time="+
    [System.Web.HttpUtility]::UrlEncode($endTime) +"&"+
    "fixed=1&hours=0&minutes=0&childoptions=1";
$hostName = "yourHostName"
$str = "host="+[System.Web.HttpUtility]::UrlEncode($hostName)+
    "&"+$postStrMain;
$ajax.setRequestHeader("Content-Length", $str.length);
$ajax.send($str);

The important parts to replace with your specific info are:

  • yourdomain
  • httpAuthUsername
  • httpAuthPassword
  • yourHostName

Thursday, June 21, 2012

Vampires + Lincoln + 8-bit = Magic

I really don't know where this whole Lincoln superhero thing came from. I can only assume the recipe was some new experiment formula thought up originally as a joke. 1 part historical figure + 1 random entry from the monster manual + lots of money = blockbuster? At any rate I'm really loving some of the material the internet is coming up with based on Hollywood's bad ideas. Many thanks to freddiew for the chuckles!