#!/usr/bin/perl
#
# Solve a made-up problem in which stationary neutron decays into
#   stationary proton, plus electron and anti-neutrino which go in
#   opposite directions.  What is the speed of the electron?
#
# Quick 'n dirty.
#
# MWR 12/14/2003

$debug = 1;

$c = 2.9979e8;
$mn = 1.6749286E-27;
$mp = 1.6726231E-27;
$me = 9.1093898E-31;

$delta = ($mn - $mp)*$c*$c;
$start_v = 0.0;
$end_v = 1.0;
$delta_v = 0.001;

while ($done == 0) {

 if ($debug > 0) {
   printf "about to start loop %12.6e %12.6e by %12.6e \n",
       $start_v, $end_v, $delta_v;
 }

 for ($v = $start_v; $v < $end_v; $v += $delta_v) {

  $gamma = 1.0/(sqrt(1.0 - $v*$v));
  $x1 = $gamma*$me*$c*$c;
  $x2 = $gamma*$me*$v;
  $xtot = $x1 + $x2;

  printf " v %12.6e  gamma %12.6e delta %12.6e  x1+x2 %12.6e \n", 
              $v, $gamma, $delta, $xtot;


  if ($xtot > $delta) {
    if ($debug > 0) {
      printf "  found it ... \n";
    }
    $start_v = $v - 2*$delta_v;
    $end_v = $v + 2*$delta_v; 
    $delta_v = $delta_v * 0.01;

    if ($delta_v < 1e-7) {
      $done = 1;
    }
    last;
  }

 }
}

exit 0;
