Click here to Skip to main content
14,924,531 members

Welcome to the Lounge

   

For discussing anything related to a software developer's life but is not for programming questions. Got a programming question?

The Lounge is rated Safe For Work. If you're about to post something inappropriate for a shared office environment, then don't post it. No ads, no abuse, and no programming questions. Trolling, (political, climate, religious or whatever) will result in your account being removed.
 
JokeRe: Factorials (C#) [modified] Pin
Xiangyang Liu 刘向阳18-Sep-08 6:31
MemberXiangyang Liu 刘向阳18-Sep-08 6:31 
GeneralRe: Factorials (C#) Pin
WilliamFalconerUK18-Sep-08 11:06
professionalWilliamFalconerUK18-Sep-08 11:06 
GeneralRe: Factorials (C#) Pin
Nemanja Trifunovic18-Sep-08 5:00
MemberNemanja Trifunovic18-Sep-08 5:00 
GeneralRe: Factorials (C#) Pin
Xiangyang Liu 刘向阳18-Sep-08 5:08
MemberXiangyang Liu 刘向阳18-Sep-08 5:08 
GeneralRe: Factorials (C#) Pin
PIEBALDconsult18-Sep-08 5:27
professionalPIEBALDconsult18-Sep-08 5:27 
GeneralRe: Factorials (C#) Pin
Dan Neely18-Sep-08 5:33
MemberDan Neely18-Sep-08 5:33 
GeneralRe: Factorials (C#) Pin
PIEBALDconsult18-Sep-08 7:06
professionalPIEBALDconsult18-Sep-08 7:06 
GeneralRe: Factorials (C#) Pin
Robert.C.Cartaino18-Sep-08 7:34
MemberRobert.C.Cartaino18-Sep-08 7:34 
Marc Clifton wrote:
I was annoyed to find that the Math class doesn't include a Factorial method


Too bad C# doesn't support extension methods for static classes. You could have written something like:
public static class MyExtensions
{
    public static ulong factorial(this System.Math, int factor) { ... } ;  // THIS WONT WORK
}

and called : ulong factorial = Math.factorial(10); But... nope.

I guess you could do this:
namespace MarcsCoolStuff
{
    public static class Math
    {
        public static ulong factorial(int factor)
        {
            ulong[] factorials = { 1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 
                                    39916800, 479001600, 6227020800, 87178291200, 1307674368000, 
                                    20922789888000, 355687428096000, 6402373705728000, 
                                    121645100408832000, 2432902008176640000, 14197454024290336768 };

            return factorials[factor];
        }
    }
}
Wrap the array lookup around a try/catch IndexOutOfRange exception if you want bounds checking.

And now you can call:
ulong factorial = MarcsCoolStuff.Math.factorial(10); Wink | ;) Nah, don't do that. It's too obtuse to "fake" a Math class. It's screaming fast though.
GeneralRe: Factorials (C#) Pin
Single Step Debugger18-Sep-08 8:18
MemberSingle Step Debugger18-Sep-08 8:18 
AnswerRe: Factorials (C#) Pin
Ri Qen-Sin18-Sep-08 9:52
MemberRi Qen-Sin18-Sep-08 9:52 
GeneralRe: Factorials (C#) Pin
Robert.C.Cartaino18-Sep-08 11:12
MemberRobert.C.Cartaino18-Sep-08 11:12 
GeneralRe: Factorials (C#) Pin
Ri Qen-Sin18-Sep-08 11:39
MemberRi Qen-Sin18-Sep-08 11:39 
GeneralRe: Factorials (C#) Pin
Marc Clifton18-Sep-08 11:23
mvaMarc Clifton18-Sep-08 11:23 
GeneralRe: Factorials (C#) Pin
Ri Qen-Sin18-Sep-08 11:46
MemberRi Qen-Sin18-Sep-08 11:46 
GeneralRe: Factorials (C#) Pin
Stuart Dootson22-Sep-08 2:02
professionalStuart Dootson22-Sep-08 2:02 
QuestionGenius or barking? Pin
Nicholas Butler18-Sep-08 4:19
sitebuilderNicholas Butler18-Sep-08 4:19 
AnswerRe: Genius or barking? Pin
Marc Clifton18-Sep-08 4:29
mvaMarc Clifton18-Sep-08 4:29 
GeneralRe: Genius or barking? Pin
Nicholas Butler18-Sep-08 4:42
sitebuilderNicholas Butler18-Sep-08 4:42 
GeneralRe: Genius or barking? Pin
James R. Twine18-Sep-08 4:31
MemberJames R. Twine18-Sep-08 4:31 
GeneralRe: Genius or barking? Pin
Nicholas Butler18-Sep-08 4:44
sitebuilderNicholas Butler18-Sep-08 4:44 
GeneralRe: Genius or barking? Pin
John M. Drescher18-Sep-08 4:51
MemberJohn M. Drescher18-Sep-08 4:51 
AnswerRe: Genius or barking? Pin
Brady Kelly18-Sep-08 4:36
MemberBrady Kelly18-Sep-08 4:36 
GeneralRe: Genius or barking? Pin
Nicholas Butler18-Sep-08 4:47
sitebuilderNicholas Butler18-Sep-08 4:47 
AnswerRe: Genius or barking? Pin
Graham Bradshaw18-Sep-08 4:39
MemberGraham Bradshaw18-Sep-08 4:39 
GeneralRe: Genius or barking? Pin
Nicholas Butler18-Sep-08 4:50
sitebuilderNicholas Butler18-Sep-08 4:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.