<?php
/*
 * Created on Jul 17, 2006
 *
 * In what follows we open the `abc` database and compute the quality for all cases
 * where the quality is < 1.  (These being cases where it was not previously computed
 * since the 'default' value for quality is 0.)  Included are two functions that 
 * combined compute the radical of a number.  They in turn are dependent on the 
 * existence of the `radical` database and the `primes` database.
 * 
 * This file is mean to be called from a form in quality_update.php.  This will 
 * allow for "pressing a button" to start the process and a place to view the 
 * stats from the process. Note we open the database at the start because
 * it is needed multiple places.  Also note that we have to use mathadmin 
 * because this page UPDATEs the database `abc`.  For this reason, this 
 * webpage is NOT for external consumption.
 */


/*Open the database for the Mathematics database*/
	include "mysql.privconnect.php";

/*  
*	The following function returns the radical of the input value given the 
* 	implicit assumption that the input value is less than or equal to the 
*   maximum nvalue in the table radical.  Note it also works on the assumption 
*   that the database containing radical values contains all x where x/rad x >10.
*/ 
function small_radical($nvalue)
{

	$nquery = "SELECT * FROM `radical` WHERE nvalue = '$nvalue' ";
	$nresult = mysql_query($nquery);
			
	if (mysql_num_rows($nresult)>0)
	{		
		while ($row = mysql_fetch_array($nresult,MYSQL_ASSOC))
 		{
 			$nval = $row['nvalue'];
 			$nrad = $row['nradical'];	
 		}
 		return $nrad;	
	}
	else 
	{
		$testprimes = array("2", "3", "5", "7");
		$nradical = $nvalue;
		foreach($testprimes AS $prime) 
		{
			$psquare = bcmul($prime, $prime);
			while (bcmod($nradical, $psquare) == 0)
				$nradical = bcdiv($nradical, $prime); 
		}	
		return $nradical;
	}			
}	


/*  
 *  Here we are collecting the maximum value currently stored in the radical
 *  database.
 */
	$maxquery = "SELECT max(nvalue) FROM `radical`";
	$maxresult = mysql_query($maxquery);
	$dbmax = mysql_result($maxresult, 0);

/*
 *  The following radical function depends on the existence of a database 
 *  containing all of the prime numbers <= sqrt($nvalue) for accuracy.  It 
 *  also requires that small_radical be available and that $dbmax above is 
 *  computed---since it is used as a global in the function.
 */

function radical($nvalue)
{
	global $dbmax;
	if (bccomp($nvalue, $dbmax) < 1)
		return small_radical($nvalue);
	else
	{
		$primeBound = bcsqrt($nvalue);
		$pquery = "SELECT * FROM `primes` WHERE primenumber <= '$primeBound' ";
		$presult = mysql_query($pquery);
		$nradical = $nvalue;
		while (($row = mysql_fetch_array($presult,MYSQL_ASSOC)) && (bccomp($nradical, $dbmax) > 0) && (bccomp($row['primenumber'],$nradical) < 1))
		{		
			$prime = $row['primenumber'];			
			$primeSquare = bcmul($prime, $prime);
			while (bcmod($nradical, $primeSquare) == 0)
				$nradical = bcdiv($nradical, $prime); 
		}
		if (bccomp($nradical, $dbmax) < 1)
			return small_radical($nradical);
		else
			return $nradical;	
	}		
}	

/*
 *  Now we will go ahead and add the part that will compute the quality of a 
 *  given abc triple.  Note this will only run if post has been set....
 */


	$cquery = "SELECT * from `abc` WHERE quality < 1 ";
	$cresult = mysql_query($cquery);

	$counter = 0;
	$fail = 0;
	
	while ($row = mysql_fetch_array($cresult,MYSQL_ASSOC))
 	{
 		$aval = $row['avalue'];
 		$bval = $row['bvalue'];
 		$cval = $row['cvalue'];
 		
 		$rada = radical($aval);
 		$radb = radical($bval);
 		$radc = radical($cval);
 		
 		$radproduct = bcmul($rada,bcmul($radb,$radc));
 		
 		$quality = log($cval)/log($radproduct);
 		
 		$uquery = "UPDATE `abc` SET quality = '$quality'  WHERE cvalue = '$cval' AND avalue = '$aval' ";
 		$uresult = mysql_query($uquery);
 		
 		if ($uresult)
 			$counter = $counter +1;
 		else
 			$fail = $fail +1;	
 		
 		echo "($aval, $bval, $cval), &nbsp $quality <br />";
 	}
 	echo "$counter records were updated with $fail records failing to update. <br />";

	mysql_close();
?>


	