|
From: <tel...@ya...> - 2003-02-15 14:18:27
|
the thing with factorials is, that it's recursively horrible since the numbers get very big very quicky, ie the space allocation required will be massive. before you even hit 13! or factorial(13) you exceed the limit for the max value for integers. (2,147,483,647) ( http://www.newdream.net/~sage/old/numbers/fact.htm ) of course you could then use floats or double data types etc, but the thing is, iteratively, the space is set aside perhaps once for the factorial, one counter, and once for the answer. e.g. int factorial; int answer=1; int i=0; /*assign factorials, cater for 0!=1 exception */ while (i<factorial) { answer=answer*(factorial-i); i++; } /* then cout or printf or fstream of whatever u want to do with it.. */ recursively however, as mentioned, it works through stack, thing is, each stack level is sorta like the 'run time embodiement' of the called function. sorry, not too good @ this yet... :( basic thing i'm getting @ is, each time the recursed function is called, then the data is passed as it would to any other function, and passing all that data around is obviously gonna be a detriment to performance as has been mentioned. there are things that work better recursively, one example is euclid's agorithm. google search it if u have to, altho i assume most of you would know what it is. last thing, i cant think of an example, but i think any recursion can be handled with a loop, but not all loops can be done via recursion... i dunno, how was i?? please lemme know if i dont know jack :P http://mobile.yahoo.com.au - Yahoo! Mobile - Exchange IMs with Messenger friends on your Telstra or Vodafone mobile phone. |