![]() |
| | #1 |
| Chinese pâté Join Date: Jul 2007 Location: Canada
Posts: 406
| Factorial For people who wouldn't know what's a factorial: http://en.wikipedia.org/wiki/Factorial But I guess most of us have all seen somewhere as an introduction to recursion this kind of code Code: unsigned int factorial(unsigned int N)
{
if (N == 0 || N == 1)
return 1;
return N * factorial(N - 1);
}
The factorial to compute will be given as a command line argument. The result has to be print to stdout. It can be either in decimal or in hexadecimal format. The machine specifications are:
The target compiler is GCC 4.2.3 on a 64-bits Linux system (I'm using Ubuntu 8.04 64-bits). Since i'm quite new to Linux-based operating systems, don't make your code overly complicated to compile or you'll have to explain me how to get it works . A shell script (sh or bash) or a makefile would be ideal...And here's some rules.
Note that every submission you submit (you can submit more than one if you want, I don't care) will be publicly available after, so everyone will be able to take a look at what people's did. I encourage people with less experience to participate. That's just going to be more fun. And it's not that hard to make a working implementation. Also, please put all your source code in a folder and include a README text file which would contains your cboard alias and directives on how to compile your solution. If you use libraries not easily found or not easily installable on a typical Linux distribution, gives indications where to get them. Also, specify in which base is the output. And zip the folder. The contest ends in 3 weeks, which means June 15th, 22:00 GMT. Have fun! (And sorry for all the English mistakes i may have made)
__________________ I hate real numbers. |
| foxman is offline |
| | #2 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| Cool! 2 questions, 1. Did you want us to support any +ve value of N? Such as, I don't know -- 955858! ? 2. How do we actually submit our solutions? A PM? Email? |
| zacs7 is offline |
| | #3 | ||
| Chinese pâté Join Date: Jul 2007 Location: Canada
Posts: 406
| Quote:
Quote:
I'm glad at least one people is interested for now. I'll have to begin on working on my solution... one day... it won't be mind blistering but it will touch to one things or two i don't usually. Won't be the fastest, but shouldn't be the slowest neither.
__________________ I hate real numbers. Last edited by foxman; 05-25-2008 at 07:26 PM. | ||
| foxman is offline |
| | #4 |
| (?<!re)tired Join Date: May 2006 Location: Portugal
Posts: 5,220
| Hmm... there's not many options I can think of to optimize a N factorial. There is only one mathematical approach I know of. But I'm not sure how many people can define it. I surely can't. Lookup tables is the usual way. Heck, even boost uses them for i <= max_factorial<T>::value. Also, an 2^32-1 input will overflow on the result unless provisions are also made for big numbers, even for 64bit computers, won't it? I think there's not much one can do, other than cutting on the end instruction set the best they can.
__________________ Originally Posted by brewbuck: Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster. |
| Mario F. is offline |
| | #6 | |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| Well there are actually optimizations for large N - possibilities are given in the wikipedia article (or a link or two deep, I don't remember). Quote:
| |
| whiteflags is offline |
| | #7 |
| Chinese pâté Join Date: Jul 2007 Location: Canada
Posts: 406
| What I mean is that it's against the rules to use something like, example, GMP, except if you are one of the author. Of course, if you want to be able to compute the factorial of a number larger than 12, you'll need some sort of data structure capable of manipulating "big integers". That's part of the challenge. Is it clearer ?
__________________ I hate real numbers. |
| foxman is offline |
| | #9 |
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| Then you have to be more clever than that, won't you?
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline |
| | #10 |
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| Here's my submission. It is password protected, btw, so no need to cheat.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline |
| | #11 |
| Chinese pâté Join Date: Jul 2007 Location: Canada
Posts: 406
| Alright, thanks for participating Sang-drax. Don't forget to publish the password the day the contest ends. I would like to add that when I say it has to be in C/C++, inline assembly is also accepted (of course), as long as it's incorporated through a C/C++ program and compile on gcc and run on the target architecture. And for those who would like this extra information: sizeof(int) = 4 sizeof(long) = 8
__________________ I hate real numbers. Last edited by laserlight; 05-28-2008 at 05:11 AM. |
| foxman is offline |
| | #12 |
| Rampaging 35 Stone Welsh Join Date: Apr 2007
Posts: 2,924
| ok, so an array of ~21 million bytes shoudl suffice to hold the largest value you specified (1 million), although 2^32-1 would require around 205 gigabytes of ram just for teh answer, so Im going to go ahead and just assume you wont give an input larger than 1 million. At that rate, assuming you can get the multiply for free adn just have to transfer the answer each iteration, adn only once, you are lookign at around 80+ minutes run time for 1000000!
__________________ He is free, you say. Ah! That is his misfortune… These men… [have] the most terrible, the most imperious of masters, that is, need. … They must therefore find someone to hire them, or die of hunger. Is that to be free? - Simon Linguet |
| abachler is offline |
| | #13 |
| Registered User Join Date: Apr 2006 Location: United States
Posts: 3,201
| I wasn't joking when I said that there were optimizations for large N, obviously. These are algorithm changes too, not just unrolling loops or something like that. |
| whiteflags is offline |
| | #14 |
| and the hat of marbles Join Date: May 2002 Location: Göteborg, Sweden
Posts: 2,038
| My submission contains no optimizations whatsoever.
__________________ Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling |
| Sang-drax is offline |
| | #15 |
| Woof, woof! Join Date: Mar 2007 Location: Australia
Posts: 3,139
| > My submission contains no optimizations whatsoever. In that case, Here is my submission: Code: g++ -O3 -o mysubmission `find / -name fac.cpp` ![]() Just make sure you extract Sang-drax's first ![]() On a serious note, It's coming! Last edited by zacs7; 05-28-2008 at 07:43 AM. |
| zacs7 is offline |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Segmentation fault using recursion to find factorial | kapok | C++ Programming | 4 | 02-23-2009 11:10 AM |
| Calculating Factorial | ice661666 | C++ Programming | 3 | 06-12-2008 04:58 AM |
| Recursion | Lionmane | C Programming | 11 | 06-04-2005 12:00 AM |
| Basic Factorial from 1-10 | AaA | C Programming | 20 | 05-28-2005 07:39 AM |
| factorial output | maloy | C Programming | 1 | 03-13-2002 03:28 PM |