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. Hopefully the bug will not be present when bc-1.07 is released.
  3. The maximum number of auto variables allowed in a bc function seems to be between 24 and 28 on some operating systems! This limitation will be removed in bc-1.07.
  4. The continue statement only works for for loops.

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

Last modified 12th July 2011