Back to mersenne-aries.sili.net main page
prob.php:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Mersenne-aries P-1 probability calculator</title>
    <link rel="stylesheet" href="/style.css" type="text/css">
</head>
<body>
<a href="/">Back to <i>mersenne-aries.sili.net</i> main page</a>
<hr size="1">
<?php

if (@$_REQUEST['showsource']) {
    
$filestoshow = array(basename(__FILE__), 'functions.inc.php');
    foreach (
$filestoshow as $key => $value) {
        echo 
htmlentities($value).':<blockquote style="border: 1px solid #000000; padding: 10px;">';
        
highlight_file($value);
        echo 
'</blockquote>';
    }
    exit;
}
echo 
'<a href="'.$_SERVER['PHP_SELF'].'?showsource=1" style="font-size: 8pt;">show source</a><hr noshade size="1">';

///////////////////////////////////////////////////////////////////////////
require_once('functions.inc.php');
///////////////////////////////////////////////////////////////////////////

if (!isset($_REQUEST['b1']) && !isset($_REQUEST['b2'])) {
    if (!@
$_REQUEST['exponent']) {
        echo 
'ERROR: must enter exponent<hr>';
    } elseif (@
$_REQUEST['guess'] == 'n') {
        
// guess for never-been-LL'd
        
$_REQUEST['b1'] = B1bound($_REQUEST['exponent'], false);
        
$_REQUEST['b2'] = B2bound($_REQUEST['exponent'], false);
    } elseif (@
$_REQUEST['guess'] == 'y') {
        
// guess for already-been-LL'd-once
        
$_REQUEST['b1'] = B1bound($_REQUEST['exponent'], true);
        
$_REQUEST['b2'] = B2bound($_REQUEST['exponent'], true);
    }
}

echo 
'Estimate B1 & B2: ';
echo 
'<a href="#" onClick="if (document.form1.exponent.value == \'\') { alert(\'Enter an exponent first\'); return false; } document.form1.submitbutton.visible = false; location = \''.$_SERVER['PHP_SELF'].'?guess=n&exponent=\' + escape(document.form1.exponent.value);">never-been-LL\'d</a> | ';
echo 
'<a href="#" onClick="if (document.form1.exponent.value == \'\') { alert(\'Enter an exponent first\'); return false; } document.form1.submitbutton.visible = false; location = \''.$_SERVER['PHP_SELF'].'?guess=y&exponent=\' + escape(document.form1.exponent.value);">"LL\'d-once-already"</a><br>';
echo 
'<table border="0">';
echo 
'<form method="post" name="form1">';
echo 
'<tr><td>Exponent:</td><td><input type="text" name="exponent" value="'.htmlentities(@$_REQUEST['exponent'], ENT_QUOTES).'"></td></tr>';
echo 
'<tr><td>B1:</td><td><input type="text" name="b1" value="'.htmlentities(number_format(@$_REQUEST['b1'], 0'.'''), ENT_QUOTES).'"></td></tr>';
echo 
'<tr><td>B2:</td><td><input type="text" name="b2" value="'.htmlentities(number_format(@$_REQUEST['b2'], 0'.'''), ENT_QUOTES).'"></td></tr>';
echo 
'<tr><td>Factored Bits:</td><td><select name="factorbits">';
echo 
'<option value="">-default-</option>';
for (
$i 60$i <= 80$i++) {
    echo 
'<option value="'.$i.'"'.(($i == @$_REQUEST['factorbits']) ? ' selected' '').'>'.$i.'</option>';
}
echo 
'</select></td></tr>';
echo 
'<tr><td colspan="2"><input type="submit" name="submitbutton" value="Calculate"> ';
echo 
'</form>';
echo 
'</table>';

if (@
$_REQUEST['exponent']) {
    echo 
'<hr>';
    echo 
'M'.$_REQUEST['exponent'].', factored to '.(@$_REQUEST['factorbits'] ? @$_REQUEST['factorbits'] : FactorBits($_REQUEST['exponent'])).' bits, with B1='.number_format(@$_REQUEST['b1'], 0).' and B2='.number_format(@$_REQUEST['b2'], 0).'<br>';
    echo 
'Probability = <b>'.number_format(100 FactorProbability($_REQUEST['exponent'], @$_REQUEST['b1'], @$_REQUEST['b2'], @$_REQUEST['factorbits']), 5).'%</b><br>';
    echo 
'Should take about '.number_format(CalcGHzDaysPminus1($_REQUEST['exponent'], @$_REQUEST['b1'], @$_REQUEST['b2']), 2).' GHz-days<br>';
}

?>
</body>
</html>
functions.inc.php:
<?php
require_once('db.inc.php');
error_reporting(E_ALL);
ini_set('display_errors''1');

define('STATS_HTML_CACHE_DIR'dirname(__FILE__).'/statsHTMLcache/');
///////////////////////////////////////////////////////////////////////////

function filesize_remote($remotefile$timeout=10) {
    static 
$filesize_cache = array();
    if (!@
$filesize_cache[$remotefile]) {
        
$filesize_cache[$remotefile] = false;
        
$url parse_url($remotefile);
        if (
$fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno$errstr$timeout)) {
            
fwrite($fp'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n");
            
fwrite($fp'Host: '.@$url['host']."\r\n");
            if (@
$url['user'] || @$url['pass']) {
                
fwrite($fp'Authorization: Basic '.base64_encode($url['user'].':'.$url['pass'])."\r\n");
            }
            
fwrite($fp"\r\n");
            
stream_set_timeout($fp$timeout);
            while (!
feof($fp)) {
                
$headerline fgets($fp4096);
                if (
eregi('^Content-Length: (.*)'$headerline$matches)) {
                    
$filesize_cache[$remotefile] = intval($matches[1]);
                    break;
                }
            }
            
fclose ($fp);
        }
    }
    return 
$filesize_cache[$remotefile];
}

function 
filedate_remote($remotefile$timeout=10) {
    static 
$filedate_cache = array();
    if (!@
$filedate_cache[$remotefile]) {
        
$filedate_cache[$remotefile] = false;
        
$url parse_url($remotefile);
        if (
$fp = @fsockopen($url['host'], ($url['port'] ? $url['port'] : 80), $errno$errstr$timeout)) {
            
fwrite($fp'HEAD '.@$url['path'].@$url['query'].' HTTP/1.0'."\r\n");
            
fwrite($fp'Host: '.@$url['host']."\r\n");
            if (@
$url['user'] || @$url['pass']) {
                
fwrite($fp'Authorization: Basic '.base64_encode($url['user'].':'.$url['pass'])."\r\n");
            }
            
fwrite($fp"\r\n");
            
stream_set_timeout($fp$timeout);
            while (!
feof($fp)) {
                
$headerline fgets($fp4096);
                if (
eregi('^Last-Modified: (.*)'$headerline$matches)) {
                    
$filedate_cache[$remotefile] = strtotime($matches[1]) - date('Z');
                    break;
                }
            }
            
fclose($fp);
        }
    }
    return 
$filedate_cache[$remotefile];
}

function 
P90years2GHzDays($P90years) {
    
// straight from George :)
    // http://www.mersenneforum.org/showpost.php?p=133207&postcount=3
    // One core of a C2D = 1.68 P4
    // A P4 = 3.44 PIIs
    // A PII = 1.12 Pentium
    // Thus, a P-90 CPU year = 365 days * 1 C2GHD * (90MHz / 1000MHz) / 1.68 / 3.44 / 1.12 = 5.075 C2GHDs
    
return $P90years 5.075;
}

function 
GHzDays2P90years($GHzDays) {
    
// straight from George :)
    // http://www.mersenneforum.org/showpost.php?p=133207&postcount=3
    // One core of a C2D = 1.68 P4
    // A P4 = 3.44 PIIs
    // A PII = 1.12 Pentium
    // Thus, a P-90 CPU year = 365 days * 1 C2GHD * (90MHz / 1000MHz) / 1.68 / 3.44 / 1.12 = 5.075 C2GHDs
    
return $GHzDays 5.075;
}

function 
CalcGHzDaysPminus1($exponent$b1=false$b2=false) {
    if (
$b1 === false) {
        
//$alreadyLLd = true; // most Mersenne-aries work will be done with Pfactor=exp,bits,1
        
$alreadyLLd false;
        
$b1 B1bound($exponent$alreadyLLd);
    }
    if (
$b2 == false) {
        
$b2 $b1 25// rough approximation of default auto-selection
    
} elseif ($b2 $b1) {
        
$b2 $b1;
    }
    require_once(
'cpucredit.inc.php');
    return 
credit_cpu_PM1_factoring($exponent0$b1$b2);
}

function 
CalcP90yearsPminus1($exponent$b1=false$b2=false) {
    return 
GHzDays2P90years(CalcGHzDaysPminus1($exponent$b1$b2));
}

function 
CalcGHzDaysECM($exponent$curves_run$b1$b2) {
    require_once(
'cpucredit.inc.php');
    return 
credit_cpu_ECM($exponent0$curves_run$b1$b2);
}

function 
CalcP90yearsECM($exponent$curves_run$b1$b2) {
    return 
GHzDays2P90years(CalcGHzDaysECM($exponent$curves_run$b1$b2));
}

function 
CalcGHzDaysLL($exponent) {
    require_once(
'cpucredit.inc.php');
    return 
credit_cpu_LL($exponent0);
}

function 
CalcP90yearsLL($exponent) {
    return 
GHzDays2P90years(CalcGHzDaysLL($exponent));
}

function 
BitsInFactor($factor$intversion=false) {
    return (
$intversion ceil(log($factor2)) : log($factor2));
}

function 
ExponentFactorThreshold() {
    static 
$ExponentThreshold = array(

        
// These breakeven points we're calculated on a 2.0 GHz P4 Northwood:
        
516000000 => 80,
        
420400000 => 79,
        
337400000 => 78,
        
264600000 => 77,
        
227300000 => 76,
        
186400000 => 75,
        
147500000 => 74,
        
115300000 => 73,
         
96830000 => 72,
         
75670000 => 71,
         
58520000 => 70,
         
47450000 => 69,
         
37800000 => 68,
         
29690000 => 67,
         
23390000 => 66,

         
// These breakevens we're calculated a long time ago on unknown hardware:
         
13380000 => 65,
          
8250000 => 64,
          
6515000 => 63,
          
5160000 => 62,
          
3960000 => 61,
          
2950000 => 60,
          
2360000 => 59,
          
1930000 => 58,
          
1480000 => 57,
          
1000000 => 56,
                
=> 40,

        
// OLD values from the before-time
        //79300000=>72, // Prime95 can't currently handle exponents bigger than this...
        //71000000=>72,
        //57020000=>71,
        //44150000=>70,
        //35100000=>69,
        //28130000=>68,
        //21590000=>67,
        //17850000=>66,
    
);
    return 
$ExponentThreshold;
}

function 
FactorBits($exponent) {
    
$ExponentThreshold ExponentFactorThreshold();
    
$factorbits 0;
    foreach (
$ExponentThreshold as $limit => $bits) {
        if (
$exponent >= $limit) {
            
$factorbits $bits;
            break;
        }
    }
    return 
$factorbits;
}

function 
CalcGHzDaysTrialFactor($exponent$frombits=0$limit=false$factor='') {
    if (
$limit === false) {
        
// how far will Prime95 factor this exponent by default
        
$limit FactorBits($exponent);
    }
    if (!
$frombits && !$factor) {
        
// this is a hack for pre-v25 which didn't always report starting point of TF
        // most trial factoring is done from <whatever-the-current-level-is> to
        // the reported bit depth, but it doesn't report the current level. So guess :)
        
if ($limit <= 58) {
            
$frombits 32;         // anything factored to low bit depths started from nothing, or close-to-nothing (CPU time to factor from 2^0 to 2^32 is insignificant)
        
} else {
            
$frombits $limit 1// assume most submitted results will be LMH results increasing factor depth by 1 level
        
}
    }
    require_once(
'cpucredit.inc.php');
    if (
$factor) {
        
$frombits floor(log($factor2));
        return 
credit_cpu_TF_factor_p95($exponent$factor$frombits);
    } else {
        return 
credit_cpu_TF_no_factor($exponent$frombits$limit);
    }
}

function 
CalcP90yearsTrialFactor($exponent$frombits=0$limit=false$factor='') {
    return 
GHzDays2P90years(CalcGHzDaysTrialFactor($exponent$frombits$limit$factor));
}

function 
B1bound($exponent$alreadyLLd=false) {
    
// formulae taken from Excel graph of actual values from Prime95
    // it's a little off around FFT size changes, but close enough
    
$exp $exponent 1000000;
    if (
$alreadyLLd) {
        
//$B1 = (-11 * $exp * $exp) + (5800 * $exp);   // projection data up to 79M
        
$B1 = (-1.3309 $exp $exp) + (5232 $exp); // updated formula from 2008-12-13 with projection data up to 500M
    
} else {
        
//$B1 = (-24.529 * $exp * $exp) + (12434 * $exp); // projection data up to 79M
        
$B1 = (-3.2698 $exp $exp) + (11182 $exp);   // updated formula from 2008-12-13 with projection data up to 500M
    
}
    return 
round($B1 5000) * 5000;
}

function 
B2bound($exponent$alreadyLLd=false) {
    return 
B1bound($exponent$alreadyLLd) * ($alreadyLLd 20 25);
}


function 
DickmansFunction($x) {
    static 
$savedF = array(
        
0000003.3513e-2155.63754e-2084.00865e-201,
        
1.65407e-1944.53598e-1888.93587e-1821.33115e-175,
        
1.55557e-1691.46609e-1631.13896e-1577.42296e-152,
        
3.80812e-1461.56963e-1405.32886e-1351.51923e-129,
        
3.69424e-1247.76066e-1191.42371e-1132.30187e-108,
        
3.30619e-1034.24793e-0984.80671e-0934.78516e-088,
        
4.22768e-0833.33979e-0782.37455e-0731.52822e-068,
        
8.94846e-0644.78909e-0594.65696e-0574.49802e-0554.31695e-053,
        
4.07311e-0513.81596e-0493.61043e-0471.73046e-0458.26375e-044,
        
3.9325e-0421.86471e-0408.8102e-0394.14402e-0371.99497e-035,
        
1.83001e-0341.59023e-0331.45505e-0321.24603e-0311.15674e-030,
        
9.70832e-0309.23876e-0294.20763e-0284.24611e-0271.61371e-026,
        
6.59556e-0263.17069e-0251.12205e-0244.65874e-0242.01267e-023,
        
6.2941e-0233.02604e-0227.84622e-0222.3526e-0216.7049e-021,
        
1.88634e-0204.59378e-0201.37233e-0194.00682e-0198.34209e-019,
        
2.21612e-0184.84252e-0181.02457e-0172.03289e-0174.07704e-017,
        
1.33778e-0162.4263e-0164.14981e-0167.0383e-0161.20511e-015,
        
3.85644e-0156.52861e-0151.06563e-0141.67897e-0142.79916e-014,
        
4.54319e-0149.83296e-0141.66278e-0132.61858e-0134.03872e-013,
        
5.98967e-0131.09674e-0121.70553e-0122.56573e-0123.72723e-012,
        
6.14029e-0129.33636e-0121.36469e-0111.89881e-0112.68391e-011,
        
4.12016e-0115.94394e-0118.43746e-0111.12903e-0101.66987e-010,
        
2.36959e-0103.11726e-0104.28713e-0105.90781e-0107.79892e-010,
        
1.05264e-0091.4016e-0091.87506e-0092.42521e-0093.14508e-009,
        
4.38605e-0095.43307e-0096.96737e-0098.84136e-0091.16286e-008,
        
1.42343e-0081.79697e-0082.30867e-0082.88832e-0083.52583e-008,
        
4.31032e-0085.46444e-0086.66625e-0088.06132e-0081.00085e-007,
        
1.20952e-0071.4816e-0071.80608e-0072.13125e-0072.5324e-007,
        
3.094e-0073.64545e-0074.31692e-0075.19078e-0076.03409e-007,
        
7.21811e-0078.53856e-0079.71749e-0071.13949e-0061.37042e-006,
        
1.53831e-0061.79066e-0062.15143e-0062.40216e-0062.76872e-006,
        
3.20825e-0063.61263e-0064.21315e-0064.76404e-0065.43261e-006,
        
6.2041e-0066.96243e-0067.94979e-0068.89079e-0061.01387e-005,
        
1.13376e-0051.2901e-0051.44183e-0051.59912e-0051.79752e-005,
        
1.99171e-0052.22665e-0052.47802e-0052.7678e-0053.0492e-005,
        
3.34189e-0053.71902e-0054.12605e-0054.54706e-0054.98411e-005,
        
5.48979e-0056.06015e-0056.61278e-0057.22258e-0057.97193e-005,
        
8.66574e-0059.48075e-0050.000103210.0001124790.000121776,
        
0.0001333440.0001440230.0001566670.0001683180.000183192,
        
0.0001965270.000213950.0002283890.0002492230.000264372,
        
0.0002893840.0003057070.0003339920.0003532870.000379868,
        
0.0004082740.000436380.0004653190.0004965040.000530376,
        
0.0005660080.0006026210.0006422860.0006845430.000723853,
        
0.0007726550.0008194180.0008685330.0009203990.000975529,
        
0.001031880.001094780.001157770.001220870.00128857,
        
0.001362880.001435570.001517140.001597470.00167572,
        
0.001765560.001861990.001950630.002052390.00216102,
        
0.002256980.002369620.002491450.002596360.00272455,
        
0.002870060.002975450.003123460.00326340.00340298,
        
0.003558270.003711950.003872880.004047250.00420016,
        
0.004397460.004563320.004759360.004957020.00514683,
        
0.005352840.005579040.005780840.006010280.00623082,
        
0.006477650.006734990.006965530.007225290.00748878,
        
0.007755370.008032710.008321990.008616120.00889863,
        
0.009198760.009533430.009854650.01019930.01050420.0108325,
        
0.01120190.01159010.01192950.01230090.01271910.0130652,
        
0.01348550.01391870.01429290.01475410.01513540.0156087,
        
0.01605720.01653820.01696690.01746930.01794600.0184202,
        
0.01895550.01943360.02001070.02048630.02102420.0216053,
        
0.02213610.02268580.02326930.02390270.02447790.025081,
        
0.02571690.02630590.02692130.02755330.02820650.0289028,
        
0.02956700.03022680.03091930.03166190.03231470.0330398,
        
0.03381240.03452670.03530380.03609470.03682880.0376202,
        
0.03837840.03918940.03996840.04081480.04164030.042545,
        
0.04336620.04424980.04510030.04603500.04688010.0478059,
        
0.04874420.04966470.05057520.05151230.05247920.0534474,
        
0.05446820.05545790.05650240.05746190.05847570.0595123,
        
0.06059880.06158740.06271900.06378760.06488300.0659551,
        
0.06705670.06812560.06927640.07045840.07153990.0727237,
        
0.07388030.07503770.07622750.07738550.07859340.0797802,
        
0.08100610.08222050.08348270.08471400.08587340.0871999,
        
0.08841370.08969480.09098200.09227970.09363500.0948243,
        
0.09612830.09747180.09882910.10009700.10143300.102847,
        
0.1042220.1054920.1068850.1083300.1096720.1110480.112438,
        
0.1138570.1153110.1167300.1181330.1195190.1209900.122452,
        
0.1239050.1254450.1268520.1283260.1297930.1312770.132817,
        
0.1343050.1357720.1372840.1388820.1403720.1419200.143445,
        
0.1449400.1465150.1481450.1496530.1511990.1528790.154368,
        
0.1559580.1576740.1592110.1607870.1624100.1640430.165693,
        
0.1672810.1689560.1705890.1722520.1738840.1755750.177208,
        
0.1788730.1805990.1822400.1839750.1856540.1873630.189106,
        
0.1907290.1925200.1941580.1958790.1976970.1993910.201164,
        
0.2028790.2046020.2064130.2081800.2099110.2117530.213484,
        
0.2152630.2170500.2188690.2206770.2223840.2242530.226071,
        
0.2278860.2297260.2315290.2333730.2352340.2370810.238853,
        
0.2407350.2426060.2444650.2463710.2482180.2501350.251944,
        
0.2538360.2557080.2575780.2595680.2614240.2633080.265313,
        
0.2671600.2690730.2710460.2729210.2748410.2768190.278735,
        
0.2806160.2826530.2846130.2865580.2884780.2904720.292474,
        
0.2944590.2963790.2983820.3003570.3023780.3043400.306853
    
);

    if (
$x >= 1.0) {
        return 
1;
    } elseif (
$x >= 0.5) {
        return 
log($x);
    }
    
$i floor($x 1000);
    if (!isset(
$savedF[$i]) || !isset($savedF[$i 1])) {
        return 
false;
    }
    return (
$savedF[$i] + (($x 1000.0) - $i) * ($savedF[$i 1] - $savedF[$i]));
}

function 
FactorProbability($exponent$B1$B2$HFF=false) {
    if (
$B1 30) {
        return 
0;
    }
    
$B2 max($B2$B1);
    if (!
$HFF) {
        
$HFF FactorBits($exponent);
    }
    
$ProbabiltyThreshold 0.001;

    
$LogK log(1.5) + (($HFF 1) * log(2)) - log($exponent);
    
$LogTemp $LogK log($B1 $B2) + log(2);

    
$FactorProbability 0;
    
$H $HFF;
    while (
true) {
        
$Prob1 DickmansFunction(log($B1) / $LogK);
        
$Aux   DickmansFunction(log($B2) / $LogTemp);

        
$Prob2 $Prob1 + (DickmansFunction(log($B2) / $LogK) - $Prob1);
        if (
$Aux != 0) {
            
$Prob2 *= DickmansFunction(log($B1) / $LogTemp) / $Aux;
        }

        
$H++;
        
$FactorProbability += $Prob2 $H;

        if (
$Prob2 $ProbabiltyThreshold) {
            break;
        }
        
$LogK    += log(2);
        
$LogTemp += log(2);
    }

    
$FudgeFactor 1.16// I don't know why, but my numbers are off by approx this much from Prime95
    
$FactorProbability *= $FudgeFactor;

    return 
$FactorProbability;
}

function 
AdminErrorEmail($file$line$subject$body) {
    
$body .= "\n\n".str_repeat('-'40)."\n";
    
$body .= '$_REQUEST:'."\n".print_r($_REQUESTtrue)."\n".str_repeat('-'40)."\n";
    
$body .= '$_GET:'    ."\n".print_r($_GET,     true)."\n".str_repeat('-'40)."\n";
    
$body .= '$_POST:'   ."\n".print_r($_POST,    true)."\n".str_repeat('-'40)."\n";
    
$body .= '$_COOKIE:' ."\n".print_r($_COOKIE,  true)."\n".str_repeat('-'40)."\n";
    
$body .= '$_SERVER:' ."\n".print_r($_SERVER,  true)."\n".str_repeat('-'40)."\n";
    
$headers  '';
    
$headers .= 'From: mersenne-aries.sili.net <'.EMAIL_JAMES.'>'."\r\n";
    
$headers .= 'Reply-To: '.EMAIL_JAMES."\r\n";
    return 
mail(EMAIL_JAMES$subject'==['.basename($file).'::'.$line."]==\n\n".$body$headers);
}

function 
SafeSQLquery($SQLquery$linenum='') {
    
$result = @mysql_query($SQLquery);
    
$error mysql_error();
    if (
$error) {
        if (@
$_SERVER['HTTP_HOST'] == 'www.example.com') {
            echo 
'<hr>'.$error.'<hr>';
        } elseif (
eregi('^Duplicate entry'$error)) {
            
// ignore
        
} else {
            
AdminErrorEmail(__FILE____LINE__'Mersenne-Aires SQL error'$error."\n\n".'On line '.$linenum."\n".'While processing query:'."\n".$SQLquery);
        }
    }
    return 
$result;
}

function 
getUserCompID($usernamestring$compidstring) {
    static 
$UserCompIDlookup = array();
    if (!isset(
$UserCompIDlookup[$usernamestring][$compidstring])) {
        
$SQLquery  'SELECT `usercompid`';
        
$SQLquery .= ' FROM `usercomp`';
        
$SQLquery .= ' WHERE (`userid` = "'.mysql_escape_string($usernamestring).'")';
        
$SQLquery .= ' AND (`compid` = "'.mysql_escape_string($compidstring).'")';
        
$result mysql_query($SQLquery);
        if (
$row mysql_fetch_assoc($result)) {
            
$UserCompIDlookup[$usernamestring][$compidstring] = $row['usercompid'];
        } else {
            
$SQLquery  'INSERT INTO `usercomp` (`userid`, `compid`) VALUES (';
            
$SQLquery .= '"'.mysql_escape_string($usernamestring).'", ';
            
$SQLquery .= '"'.mysql_escape_string($compidstring).'")';
            
mysql_query($SQLquery);
            
$UserCompIDlookup[$usernamestring][$compidstring] = mysql_insert_id();
        }
    }
    if (!
$UserCompIDlookup[$usernamestring][$compidstring]) {
        
AdminErrorEmail(__FILE____LINE__'Mersenne-aries: failed to getUserCompID('.$usernamestring.', '.$compidstring.')'$SQLquery."\n\n".mysql_error());
        echo 
'An error has occurred, sorry. The site administrator has been notified and will fix it shortly.';
        exit;
    }
    return 
$UserCompIDlookup[$usernamestring][$compidstring];
}

function 
getCompName($usercompid) {
    static 
$CompNameLookup = array();
    if (!isset(
$CompNameLookup[$usercompid])) {
        
$CompNameLookup[$usercompid] = '';

        
$SQLquery  'SELECT `compid`';
        
$SQLquery .= ' FROM `usercomp`';
        
$SQLquery .= ' WHERE (`usercompid` = "'.mysql_escape_string($usercompid).'")';
        
$result mysql_query($SQLquery);
        if (
$row mysql_fetch_assoc($result)) {
            
$CompNameLookup[$usercompid] = $row['compid'];
        }
    }
    return 
$CompNameLookup[$usercompid];
}

function 
EmailAttachment($from$to$subject$textbody, &$attachmentdata$attachmentfilename$base64encode=true) {
    
$boundary '_NextPart_'.time().'_'.md5($attachmentdata).'_';

    
$textheaders  '--'.$boundary."\n";
    
$textheaders .= 'Content-Type: text/plain; format=flowed; charset="iso-8859-1"'."\n";
    
$textheaders .= 'Content-Transfer-Encoding: 7bit'."\n\n";

    
$attachmentheaders  '--'.$boundary."\n";
    if (
$base64encode) {
        
$attachmentheaders .= 'Content-Type: application/octet-stream; name="'.$attachmentfilename.'"'."\n";
        
$attachmentheaders .= 'Content-Transfer-Encoding: base64'."\n";
    } else {
        
$attachmentheaders .= 'Content-Type: text/plain; name="'.$attachmentfilename.'"'."\n";
        
$attachmentheaders .= 'Content-Transfer-Encoding: 7bit'."\n";
    }
    
$attachmentheaders .= 'Content-Disposition: attachment; filename="'.$attachmentfilename.'"'."\n\n";


    
$headers[] = 'From: '.$from;
    
$headers[] = 'Content-Type: multipart/mixed; boundary="'.$boundary.'"';

    return @
mail($to$subject$textheaders.ereg_replace("[\x80-\xFF]"'?'$textbody)."\n\n".$attachmentheaders.($base64encode wordwrap(base64_encode($attachmentdata), 76"\n"true) : $attachmentdata)."\n\n".'--'.$boundary."--\n\n"implode("\r\n"$headers));
}

function 
caseinsensitivesortsub($a$b) {
    
$a strtolower($a);
    
$b strtolower($b);
    if (
$a == $b) {
       return 
0;
    } elseif (
$a $b) {
        return 
1;
    }
    return -
1;
}

function 
BarGraph($width$height=10$color='0033CC'$title='') {
    return 
'<img src="index.php?px='.$color.'" height="'.max(1round($height)).'" width="'.max(1round($width)).'" border="0" title="'.htmlentities($title).'">';
}

function 
GetPct($numerator$denominator) {
    if (
$denominator == 0) {
        return 
0;
    }
    return ((
$numerator $denominator) * 100);
}

function 
NonZeroMin() {
    
$arg_list func_get_args();
    
$NonZeroValues = array();
    if ((
func_num_args() == 1) && is_array($arg_list[0])) {
        
$values_to_check $arg_list[0];
    } else {
        
$values_to_check $arg_list;
    }
    foreach (
$values_to_check as $key => $value) {
        if (
$value) {
            
$NonZeroValues[] = $value;
        }
    }
    if (
count($NonZeroValues) == 0) {
        return 
false;
    }
    return 
min($NonZeroValues);
}

function 
Range2bgcolor($value$maxvalue$emptyvalue=null) {
    if ((
strlen($value) == 0) && !is_null($emptyvalue)) {
        return 
$emptyvalue;
    }
    
$value min(max(0$value), $maxvalue);
    
$scale round(($value $maxvalue) * 255);
    
$r str_pad(dechex(                                      $scale), 2'0'STR_PAD_LEFT);
    
$g str_pad(dechex(                                255 $scale), 2'0'STR_PAD_LEFT);
    
$b str_pad(dechex(round(255 * (abs((128 $scale) / 128)))), 2'0'STR_PAD_LEFT);
    return 
$r.$g.$b;
}

function 
NiceTimeFormatting($seconds$precision=1$returnparts=false) {
    
$sign = (($seconds 0) ? -1);
    
$seconds abs($seconds);
    do {
        if (
$seconds 100) {
            
$value number_format($seconds0);
            
$unit  'second'.(($seconds 1) ? 's' '');
            break;
        }
        
$minutes $seconds 60;
        if (
$minutes 60) {
            
$value number_format($minutes$precision);
            
$unit  'minutes';
            break;
        }
        
$hours $seconds 3600;
        if (
$hours 24) {
            
$value number_format($hours$precision);
            
$unit  'hours';
            break;
        }
        
$days $seconds 86400;
        if (
$days 60) {
            
$value number_format($days$precision);
            
$unit  'days';
            break;
        }
        
$months $seconds / (30 86400);
        if (
$months 12) {
            
$value number_format($months$precision);
            
$unit  'months';
            break;
        }
        
$years $seconds / (365 86400);
        if (
true) {
            
$value number_format($years$precision);
            
$unit  'years';
            break;
        }
    } while (
false);
    
$value *= $sign;
    if (
$returnparts) {
        return array(
$value$unit);
    }
    return 
$value.' '.$unit;
}

?>