Hi
I'm asked to write two functions to find the factorial one using recursion and the other using loops.
the range of numbers which if have to output their factorials is 1 to 500(N)
after some time the computer won't be able to find the factorial .I have to output the
last value of N .

is there's anyway to output anything when the computer stops (when it ran's out of stacks )
??

>>is there's anyway to output anything when the computer stops (when it ran's out of stacks )

Nope -- your program is trashed at that point. Post the function you are attempting to write, and tell us the compiler and operting system.

If you ran out of stack memory, that is probably with your recursive implementation. The rule is that if you write a recursive function for which you expect very deep recursion (like 500 recursive calls), then you should really minimize the number of local variables in the recursive function definition. Also, using the highest level of optimization as a compiler option ("-O3" for gcc or MinGW) can help as well. But first, look at the algorithm itself, please post what you have if you want any more specific help/hints from us.

commented: Thanks for help +1

>>Nope -- your program is trashed at that point. Post the function you are attempting to write, and tell us the compiler and operting system
I know that what i want is to output the last of of N when it's trashed

The point from my code is show that sometimes loop are much better than recursion .
I'm using Microsoft visual c++ 2005

>> please post what you have if you want any more specific help/hints from us.

here is the assignment
In this assignment, you will learn the main difference between recursion and regular iteration.
You are required to write a C++ program which computes the factorial function (N!). You are required to use two methods: recursion and iteration.
Define class factorial; and within factorial class define two functions: recursion() and iteration(). Function recursion() uses the recursive method to compute N!, and function iteration() uses the normal loop structure to compute N!.
For each function record the start time and end time for each function.
For time recording use the following class

#include <time.h>

You may use the following for defining start and end time in your programs

time_t start_t, end_t;  // this defines time in seconds
clock_t start_tm, end_tm; //this defines time in milleseconds

You may also use the function difftime (end_t, start_t) to define the difference between end and start times.

Repeat the execution of the functions many times; you may use the following loop to call the functions

For (i=10; i<500; i++){
	Factorial.recursion(i);
	Factorial.iteration(i);
}

Where Factorial.recursion(i) and Factorial.iteration(i) are functions in the class Factorial.
Generate the output to a file using the following format
N N! Recursion_Time Iteration_Time
10
11
12


500

You will notice that the computer will not be able to generate the factorial value when N grows large. Find the maximum N for which the factorial is generated.

Output the value of N (which is the i loop counter) before each iteration of the loop in main(). That way when the program crashes due to too big N you will know its value.

can't say for the recursive version but for the iterative one you could put an if statement in the loop to see if the MAX value(for data type) divided by the next term to be multiplied is less than your running total then this would overflow the range of your data type, so break the program there and output the last value.:)

i.e

if(MAX/N<total){break;}

cout<<N;

Thanks guys for your help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.