CS-220 Spring 2016 Lab 2 Factorial Limits
In mathematics, we learned about the concept of factorial numbers, such a 5! = 5 * 4 * 3 * 2 * 1 = 120.
A factorial is most easily described by a function that invokes itself in it's own definition - known as a recursive function. The recursive definition for a factorial is as follows:
factorial(1) = 1
factorial(n) = n * factorial(n-1)
Factorials are used in many applications, such as combinatorics, where the number of permutations of n objects is fact(n), in calculus where the nth derivative of x**n is factorial(n), and in many probability theory applications. Clearly factorials grow very quickly. Hence, they are good tests for limits on the size of numbers.
Starting with Signed Character numbers
Extending to Unsigned Character Numbers
- Edit factc.c so that the factorial command takes an unsigned character argument and returns an unsigned character result. For more information about C data types, check out https://en.wikipedia.org/wiki/C_data_types.
- Debug the edited code.
- Again, keep track of the largest number for which you can get a correct answer. Is this different than for the signed case above? Why?
Extending to larger numbers
- Go through the same process as above, but this time, make factorial take a short int as an argument and return a short int as a result.
- What is the largest number for short int handles? Why?
- Try again with unsigned short ints. Does this change anything? Why or why not?
- Try some other integer data types, such as ints, unsigned ints, long ints, unsigned long ints.
Warning... to make long ints work, you will have to change the declarations of n and fn in the "main" function, and you will have to change the format specifications in the printf call from %d to %ld.
Working with floating point factorials
- Change factorial to take a float argument and return a float result.
- Warning... you will have to change the declaration of n and fn in the main function to float, and you will have to change the printf format specifications to %f.
- Warning... floating point numbers can return the value inf to indicate "infinity". This causes the for loop in the main function to go on forever. To get around this problem, add #include <math.h> at the top of your code, and change the condition in the for loop from fn>0 to fn > 0 && fn != INFINITY.
- Test you changes.
- What is the largest number in this case for which factorial returns an exactly correct answer (with no approximation)?
- How can you determine whether the result is exactly correct? Hint... factorial(5) contains factors of 2x5=10. Every factorial greater than 5 must have these factors in it as well.
- Does this change if factorial takes and returns a double result?
- Hint... for double, what you want to see may scroll off the screen before you have a chance to look at it. One way of fixing this problem is to use the command make test | head -40. This command will print the first 40 lines of output from make test, and then print a "Broken pipe" messasge.
Download and edit the following file: lab2_report.txt. Then submit your editted file on Blackboard in the Lab 2 submission area.