Make sure you wrap your code in [php][/php] or [code][/code] tags. Also, a more suitable subject title would be better.
So what is the actual problem you are having? What algorithm do you intend to use for the calculations?
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php