BCMath programming hints for number theorists

Introduction

Here are some hints arising from my experience with programming number theory algorithms in BCMath.
The discussion is aimed at BC programmers who wish to convert their BC programs into BCMath, thereby making their BC programs more accessible.
My BC programs are located via http://www.numbertheory.org/gnubc/gnubc.html
Only a modicum of PHP programming knowledge is required and I found the book PHP for the world wide web by Larry Uhlman, a good introduction to PHP.
Matt Zandstra's book SAMS Teach Yourself PHP4 in 24 Hours is also quite readable.

By way of illustration, let's take my BC program base.
The BCMath conversion of base consists of four files: base.html, base.php, check_input.php and base_.php:

The underlying calculations are performed on a server somewhere.
Naturally there is a limitation on the running-time of these programs. BC programs do not run anywhere near as fast as compiled C programs, but nevertheless are powerful and fun for number theorists to write and experiment with.

Description of the files

(a) base.html

Here is its body:

<h3 ALIGN="CENTER">Finding the base b expansion of a positive integer n</h3>

<FORM ACTION="base.php" METHOD="POST">
Enter b (> 1):<INPUT TYPE=TEXT NAME="Bvalue" SIZE=10><br>
Enter a non-negative integer n:<INPUT TYPE=TEXT NAME="Nvalue" SIZE=50><br>
<INPUT TYPE=SUBMIT NAME="SUBMIT" VALUE="GO">
<INPUT TYPE=RESET NAME="RESET" VALUE="RESET">
</FORM>


Integers b and n are slotted into variables Bvalue and Nvalue, respectively, for processing in base.php.

(b) base.php


<html>
<?php
#PHP file base.php
include("base_.php");
include("check_input.php");
$Bvalue=trim($Bvalue);
$Nvalue=trim($Nvalue);

$check1=check_decimal($Bvalue);
$check2=check_decimal($Nvalue);

	if ($check1==0 || $check2==0){
		print "<p>\n";
	}
	else if(bccomp($Bvalue,"1")<=0){
		print "b <= 1<br>\n";	
	}
	else if(bccomp($Nvalue,"0")<0){
		print "n < 0<br>\n";	
	}
	else {
		base($Bvalue,$Nvalue);
		for($j=$length;bccomp($j,"1")>=0;$j=bcsub($j,"1")){
			if(bccomp($j,"1")>0){
				print "$digits[$j]b<sup>$j</sup> + ";
			}else{
				print "$digits[$j]b +";
			}
		}
		print "$digits[0]<br>\n";
		print "is the base b=$Bvalue expansion of $Nvalue<br>\n";
	}
	print "<p>\n";
	print "<a href=\"./base.html\">Return to main page</a><br>\n";
?>
</html>

The file check_input.php" contains the very important check_decimal program, which is not included here for security reasons.

(d) base_.php

<?php
#PHP file base_.php
function base($b,$n){
global $length;
global $digits;
	$i=0;
	$x=$n;
	while(bccomp($n,$b)>=0){
		$q=bcdiv($n,$b);
		$temp=bcmul($q,$b);
		$t=bcsub($n,$temp);
		$digits[$i]=$t;
		$n=$q;
		$i=bcadd($i,"1");
	}
	$digits[$i]=$n;
	$length=$i;
	return;
}
?>

Notice the declaration of global variable length and global array digits at the top of the file. These variables will be accessible in base.php.

Some remarks about BCMath

There is little online information about the use of the BCMath functions and just what features of BC programming go over to BCMath. Here are some of my observations:

Inputting an array of integers

The file lcm.html takes as input an array of positive integers $Mvalue. These are processed in lcm.php as follows:
<html>
#PHP file lcm.php
<?php
include("lcm_.php");

$Mvalue=trim($Mvalue);
include("check_input.php");
$a=preg_split('[ ]+',$Mvalue);
$t=count($a);
if(bccomp($t,"1")<=0){
	print "number of entries is less than or equal to 1<br>\n";
}
else{
	$flag="0";
	for($i="0";bccomp($i,$t)<0;i=bcadd($i,"1")){
		$check=check_decimal($a[$i]);
		if ($check==0){
			print "<p>\n";
			$flag="1";
			break;
		}
		$u=bccomp($a[$i],"0");
		if($u<=0){
			print "a[$i] <= 0<br>\n";
			$flag="1";
			break;
		}
	}
	if($flag==0){
		$lcm=lcma($a,$t);
		print "lcm(";
		$j=bcsub($t,"1");
		for($i="0";bccomp($i,$j)<0;$i=bcadd($i,"1")){
			print "$a[$i], ";
		}
		print "$a[$i]) = $lcm";
	}
}
print "<p>\n";
print "<a href=\"./lcm.html\">Return to main page</a><br>\n";
?>

Listing the members of a cartesian product of integers

See recursive construction of the cartesian product at
http://stackoverflow.com/questions/2516599/php-2d-array-output-all-combinations

preg_split() converts the string $Mvalue of integers which were entered separated by (any number of) spaces, into a normal array $a of $t=$count($a) numbers $a[0],...,$a[$n], where $n=bcsub($t,"1").

Debugging your BCMath script

Just as in debugging a Latex document, debugging a BCMath script can be challenging.

Please email any comments or suggestions to Keith Matthews
Last modified 16th March 2017

Return to main page