bc 1.06

Version of implementation GNU bc of programming language bc

A stable version of GNU bc, released on 18 March, 2001. As usual with bc, it has only command line interface.

Examples:

Hello, World! - bc (242):

print "Hello, World!\n";

Factorial - bc (243):

This example uses recursive factorial definition.

define factorial(n) {
    if (n == 0) return(1);
    return(n * factorial(n - 1));
}

for (n = 0; n <= 16; n++) {
    print n; "! = "; factorial(n);
}

Fibonacci numbers - bc (244):

This example uses recursive definition of Fibonacci numbers. Note that printing “…” is put into the loop, since both code and output are shown in the same console, and printing “…” after the loop will result in the following output:

for (n = 1; n <= 16; n++) {
print fibonacci(n); ", "
}
1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, print "..."
...

define fibonacci(n) {
    if (n <= 2) return(1);
    return(fibonacci(n-1)+fibonacci(n-2));
}

for (n = 1; n <= 16; n++) {
    print fibonacci(n); ", "
    if (n==16) print "..."
}

Fibonacci numbers - bc (245):

This example uses Binet’s formula. Note that bc is an arbitrary-precision calculator, so after floating-point calculations the numbers have to be rounded to integers. This has to be done manually, since bc doesn’t provide built-in rounding.

for (n = 1; n <= 16; n++) {
    scale = 10
    x = (((1 + sqrt(5)) * .5) ^ n - ((1 - sqrt(5)) * .5) ^ n) / sqrt(5)
    scale = 0
    print (x+0.5)/1; ", "
    if (n==16) print "..."
}