C Board  

Go Back   C Board > Community Boards > Contests Board

Closed Thread
 
LinkBack Thread Tools Display Modes
Old 05-25-2008, 03:55 PM   #1
Chinese pâté
 
foxman's Avatar
 
Join Date: Jul 2007
Location: Canada
Posts: 406
Factorial

Here's a contest I think most people can give a try. The goal is to implement a program in C or C++ that compute precisely the factorial of N (N being an non-negative integer) as fast as possible.

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);
}
Of course, this code when compiled on a "normal" machine will yields incorrect result if N is larger than 12. So this doesn't interest us.

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:
  • Intel Core2 Duo (64-bit instruction set, SSE4.1 support, dual core, 6 MB of L2 cache)
  • 4 GB of RAM

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.
  • You can't use mathematics libraries, except if you are the author or if it's the standard C/C++ math library. In fact, if someone want to use an "advanced" mathematic library, he can but his program won't be taken into account when it will be time to declare the winner of the contest.
  • You can't use pre-calculated factorial, except for 1! and 0!. This is simply not interesting.

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  
Old 05-25-2008, 06:55 PM   #2
Woof, woof!
 
zacs7's Avatar
 
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?
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline  
Old 05-25-2008, 07:17 PM   #3
Chinese pâté
 
foxman's Avatar
 
Join Date: Jul 2007
Location: Canada
Posts: 406
Quote:
1. Did you want us to support any +ve value of N? Such as, I don't know -- 955858! ?
Yes. The input number will be between 0 and 2^32 - 1. But since I don't want to leave my computer running for days for every program to terminate, consider it will be somewhere between 100 000 and maybe 1 000 000. But consider 2^32 - 1 being the "hard limit".

Quote:
How do we actually submit our solutions? A PM? Email?
I would say by PM, but i did not even check if it was actually possible to send attachment via PM. I'll give an email address if it's not possible. EDIT: Ok, it doesn't look possible to send attachments via PM, so you'll have to send them via email. If you go see my profile, there's an email address you can use.

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  
Old 05-25-2008, 07:45 PM   #4
(?<!re)tired
 
Mario F.'s Avatar
 
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  
Old 05-25-2008, 08:15 PM   #5
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
I think that's part of the problem

Anyone can calculate 1 to 12! Even the solution is given in the original post.
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline  
Old 05-25-2008, 08:36 PM   #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:
You can't use mathematics libraries, except if you are the author or if it's the standard C/C++ math library. In fact, if someone want to use an "advanced" mathematic library, he can but his program won't be taken into account when it will be time to declare the winner of the contest.
But we won't see them if we're not at least allowed to use a bignum library.
whiteflags is offline  
Old 05-25-2008, 09:09 PM   #7
Chinese pâté
 
foxman's Avatar
 
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  
Old 05-25-2008, 10:40 PM   #8
Woof, woof!
 
zacs7's Avatar
 
Join Date: Mar 2007
Location: Australia
Posts: 3,139
> Is it clearer ?
Yes thanks
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim
zacs7 is offline  
Old 05-27-2008, 12:44 PM   #9
and the hat of marbles
 
Sang-drax's Avatar
 
Join Date: May 2002
Location: Göteborg, Sweden
Posts: 2,038
Quote:
Originally Posted by Mario F. View Post
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?
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  
Old 05-27-2008, 03:20 PM   #10
and the hat of marbles
 
Sang-drax's Avatar
 
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.
Attached Images
File Type: pdf SangDrax.zip.pdf (1.5 KB, 168 views)
__________________
Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling
Sang-drax is offline  
Old 05-27-2008, 05:01 PM   #11
Chinese pâté
 
foxman's Avatar
 
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  
Old 05-27-2008, 08:29 PM   #12
Rampaging 35 Stone Welsh
 
abachler's Avatar
 
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  
Old 05-28-2008, 01:17 AM   #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  
Old 05-28-2008, 05:38 AM   #14
and the hat of marbles
 
Sang-drax's Avatar
 
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  
Old 05-28-2008, 06:45 AM   #15
Woof, woof!
 
zacs7's Avatar
 
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`
Not sure what else I could optimize, because I actaully haven't read it

Just make sure you extract Sang-drax's first

On a serious note, It's coming!
__________________
"I.T. gets the chicky-babes" - M. Kelly
bakefile | vim

Last edited by zacs7; 05-28-2008 at 07:43 AM.
zacs7 is offline  
Closed Thread

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 01:26 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22