The first objectMore detailsFactorial numbers

Factorial numbers

In order to work with SYMMETRICA you have to write a main routine of your own choice. If you are familiar with object oriented programming and with the programming language C, you may very well skip this section. But if this is not true, you better follow the next advices carefully and step by step.

All you need to know at the very beginning is that the main routine we are going to write is of the forn

main()
{
\ldots
}
Inside the curly brackets we shall write the program that hopefully will do what we want. But first consider a very easy example: the evaluation of n!. In order to do this we recall that this number can be evaluated in a recursive way. Here is, first of all, how this function can be defined by using the programming language C, but note that this is not the complete program yet which allows to calculate some values (in fact we assume that you either know C already, or that at least you have a book on this programming language at hand, say the book by Kernighan/Ritchie):
int  fak(n)
int n;
{
if(n==0) return(1);
return(fak(n-1)*n);
}
Now we would like to evaluate the numbers fak(m), for m£10, say. We therefore complete the rows given above to a corresponding program, embraced by main(){...}, and assisted by a first line that includes the usual standard input and output routines. This altogether looks as follows:
#include<stdio.h>
main()
{
int  m,n;n=10;
for(m=1;m<=n;++m){printf("%d,",fak(m));}
}
int  fak(n)
int  n;
{ 
if(n==0)return(1);
return(fak(n-1)*n);
}
Put this tiny little program (it is already at hand, namely in the example file ex7.c) into the same directory as the other files of SYMMETRICA and give it the name
test.c
This will be the file where you write your programs. You should now immediately try if it works. Hence type (if you use a UNIX-machine)
cc test.c
or the appropriate command, depending on your personal computer. Now your C-compiler translates the program, and after it has returned the prompt (hopefully), you give it the order to carry it out by typing (this again is machine dependent!)
a.out
After a little while, your computer will return the following sequence of natural numbers: 1,2,6,24,120,720,5040,40320,362880,3628800, which shows that everything is quite all right, so far.

Now we want to make a harder test, by replacing now 10 by 20 (in test.c, of course), obtaining (after compilation) the following output: 1,2,6,24,120,720,5040,40320,362880,3628800,39916800, 479001600,1932053504,1278945280,2004310016,2004189184, -288522240,-898433024,109641728,-2102132736. At least the negative entries are mistakes, caused by overflow. Thus we have to explain that and how longintegers are managed in SYMMETRICA automatically. This will lead us to the introduction of object oriented methods of programming: As soon as we have changed the number fak(n) into an object obfak(n), say, then everything is fine, in this case SYMMETRICA will automatically switch to longinteger, and no overflow will occur!


harald.fripertinger@kfunigraz.ac.at,
last changed: November 19, 2001

The first objectMore detailsFactorial numbers