### 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:

• base.html contains boxes in which integers b and n are entered by the user, from the browser.
• In base.php these integers are trimmed of whitespace for and aft, using trim() and then checked with check_decimal(), to see that they are genuine integers and not strings containing rubbish characters.
• Then b is checked to see that b > 1 holds, while n is checked to see that n is non-negative.
• Then a function call is made to base(b,n) which resides in base_.php. This will result in html output to the browser to display the results of the calculations.
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.

### (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";
?>
</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;
}
\$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.

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:
• Warning: Instead of the following versions of the if statement:

if(\$a<\$b),if(\$a==\$b), if(\$a>\$b), if(\$a!=\$b)

one should use

if(bccomp(\$a,\$b)<0), if(bccomp(\$a,\$b)==0), if(bccomp(\$a,\$b)>0), if(bccomp(\$a,\$b))

to be on the safe side.
There seems to be no problem for \$a and \$b with up to 16 decimals. However it seems that on my system, integers lie in the range -2147483647≤ n ≤ 2147483647=231-1. See the PHP manual and the entry for var_dump and consider the following two php programs:
Example 1.
```<?php
\$large_number1 =  2147483648;
\$large_number2 =  2147483647;
\$large_number3 =  -2147483647;
\$large_number4 =  -2147483648;
var_dump(\$large_number1);
echo "<br>\n";
var_dump(\$large_number2);
echo "<br>\n";
var_dump(\$large_number3);
echo "<br>\n";
var_dump(\$large_number4);
echo "<br>\n";
?>
```
The output is
```float(2147483648)
int(2147483647)
int(-2147483647)
float(-2147483648)
```
Example 2.
```<?php
\$test1="10000000000000000";
\$test2="10000000000000001";
if(\$test1>\$test2){
echo "10000000000000000 > 10000000000000001<br>\n";
}elseif(\$test1==\$test2){
echo "10000000000000000 = 10000000000000001<br>\n";
}else{
echo "10000000000000000 < 10000000000000001<br>\n";
}

\$t=bccomp(\$test1,\$test2);
if(\$t==1){
echo "10000000000000000 > 10000000000000001<br>\n";
}elseif(\$t==0){
echo "10000000000000000 = 10000000000000001<br>\n";
}else{
echo "10000000000000000 < 10000000000000001<br>\n";
}

\$t=strcmp(\$test1,\$test2);
if(\$t==1){
echo "10000000000000000 > 10000000000000001<br>\n";
}elseif(\$t==0){
echo "10000000000000000 = 10000000000000001<br>\n";
}else{
echo "10000000000000000 < 10000000000000001<br>\n";
}
?>
```
The output is
```10000000000000000 = 10000000000000001
10000000000000000 < 10000000000000001
10000000000000000 < 10000000000000001
```
• Warning: One should write

\$a="123456789123456";

\$a=123456789123456;

There seems to be no problem if \$a has less than 15 digits.
• \$i++ does not seem to work in the usual bc for loop

for(\$i="0";bccomp(\$i,\$n)<0;\$i++).

Instead this should be written as

Similarly, one cannot use i++ inside while.
• Incrementing with ++ inside a for or while loop, using a symbol other than i++, seems to work.

### 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";
\$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");
print "\$a[\$i], ";
}
print "\$a[\$i]) = \$lcm";
}
}
print "<p>\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").

Just as in debugging a Latex document, debugging a BCMath script can be challenging.
• Don't forget to end statements with a ";".
• One nasty, hard-to-locate error is \$a="1';. The error message might state that the error lies on the last line of the file.
• Writing \$a[i] instead of \$a[\$i], is easy to do.
• Forgetting to make corresponding global variable declarations in appropriate files is a trap for the forgetful programmer.
• John Campbell has pointed out that including a space as part of a print statement forces the browser to emit a new line, instead of continuing to send the output off the screen. I use this trick in my prime_generator program to get the primes to fit nicely on the screen.
• I use flush() after most print statements, to flush the output buffer to push all the output so far to the user's browser. But even then, I don't seem to always get the output pushed up the screen automatically as I would like, without use of the scrollbar.
• 13th December 2005. One nice feature of BCMath is the matrix facility. However printing a matrix needs care.
```     print "matrix[\$i][\$j]=\$matrix[\$i][\$j] "; /* does not work */

\$t=\$matrix[\$i][\$j];
print "matrix[\$i][\$j]=\$t "; /* works */
```
• On an IMAC, debugging your BCMath program is vastly helped by viewing MAMP logs in OSX.
• Using the argument &\$a in a function argument will change the value of \$a.
• Getting out of fullscreen in Firefox
• Creating a .png from a window in a pdf using screenshot in Preview in an Imac. Create the pdf using Latex. Open the pdf and view it in preview. Go to print and select screenshot and copy a section via the select option. Then save as a .png.
• This isn't BCMATH stuff, but I used the Unix command

sort -k2n -k1n ecubed_table > ecubed_table_sorted

to sort the list of the first 250,000 partial quotients of e3 into the list list, ordered with regard to increasing size.