BC number theory programs


BC (version 1.06) (see the manual) is a programming language that supports arbitrary precision integer arithmetic calculations with interactive execution of statements. It is an idiosyncratic programming language which closely resembles parts of the C language. While it is somewhat slow for some purposes (all code is executed as it is read), the author has found bc very useful in teaching and research. The code is very easy to read and students can see directly how an algorithm has been implemented. It is also useful as a template for harder-to-write, but faster C code.
bc-1.06 is a superior version of the basic bc program which comes with the UNIX operating system.

To run a bc program such as gcd below, type bc gcd. This loads the bc program and the bc program gcd.

As a calculator, bc has a number of standard operations:

5+3, 5*3, 5/3, (=integer part), 5%3, (=least remainder), 5^3, sqrt(n).

global array variables can be entered: m[0]=5;m[1]=3;m[2]=7

Typing z=lcma(m[ ],3) and then z, prints and stores z, the lcm of 5, 3 and 7.

Consult the bc manual for more information.


Bugs and idiosyncrasies:
  1. When using the if-else construction, insert a "\", as follows:
              if(expression){statement1}\
              else{ statement2}
    
    Alternatively, as pointed out by
    Anton Stiglic, use the construction
              if(expression){
                   statement1
              }else{
                   statement2
              }
    
  2. There was a bug in BC 1.06 under some Solaris platforms, which causes my program squareroot below to give a bus error, though not under linux.
  3. There can be a problem with number of auto variables allowed in a bc function in BC 1.06. (This arose recently on my Imac, when after updating the OS to Snow Leopard, I was getting an Abort Trap error message.) The auto variables appear to be accounted for internally together with all other variables, so the only limitation is the total number of names in the whole program. This is shown by the command limits which is 32767 on some systems. I had no problem with BC 1.06.95.
  4. The continue statement only works for for loops.

Email
http://www.numbertheory.org/keith.html

Last modified 19th October 2014