tag:blogger.com,1999:blog-125341290284114978.post9141937561758503474..comments2023-04-23T00:05:10.829-07:00Comments on Guy Ellis' Tech Blog: Factorial Function in C#Guy Ellishttp://www.blogger.com/profile/02574435376236977220noreply@blogger.comBlogger8125tag:blogger.com,1999:blog-125341290284114978.post-3159650254424721992013-03-30T07:42:13.000-07:002013-03-30T07:42:13.000-07:00Fast and also not hard coded.
static decim...Fast and also not hard coded.<br /> static decimal[] mfactors = null;<br /> static double[] dfactors = null;<br /> static void init_mFactors()<br /> {<br /> mfactors = new decimal[27];<br /> mfactors[0] = 1;<br /> decimal f = 1;<br /> for (int i = 1; i < 27; i++)<br /> {<br /> f *= i;<br /> mfactors[i] = f;<br /> }<br /> }<br /> static void init_dFactors()<br /> {<br /> dfactors = new double[171];<br /> dfactors[0] = 1;<br /> double f = 1;<br /> for (int i = 1; i < 171; i++)<br /> {<br /> f *= i;<br /> dfactors[i] = f;<br /> }<br /> }<br /> public static decimal Factorial(int factor)<br /> {<br /> if (mfactors == null) init_mFactors();<br /> if (factor < 0 || factor > 26) throw new OverflowException("Arithmetic operation resulted in an overflow.");<br /> return mfactors[factor];<br /> }<br /> public static double Factorial(double factor)<br /> {<br /> if (mfactors == null) init_dFactors();<br /> if (factor < 0 || factor > 170) throw new OverflowException("Arithmetic operation resulted in an overflow.");<br /> return dfactors[(int)factor];<br /> }<br /> }Matthiasnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-15152351770172771982013-03-06T17:38:06.000-08:002013-03-06T17:38:06.000-08:00Ahhh, so obvious once it's pointed out. Very ...Ahhh, so obvious once it's pointed out. Very nice solution. <br />Trevor.trevornoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-20606545509864712982011-08-10T10:20:30.000-07:002011-08-10T10:20:30.000-07:00august: You are correct, that is a bug in the code...august: You are correct, that is a bug in the code. I'm going to leave it as is right now and not edit it (I may edit it later) as it seems to be a good twist to the question. From my experience, however, most candidates are not aware of the mathematical factorial equation and so that would slip them by. It slipped by me because I wasn't paying attention.<br />Also, I understand your notation I just wasn't expecting to see mathematical notation and so read that as if it was C#. My bad and thanks again for pointing that out.guy ellisnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-9623694313006114532011-08-10T08:46:36.000-07:002011-08-10T08:46:36.000-07:00One more thing. In my previous code 0!=1 should re...One more thing. In my previous code 0!=1 should read zero factorial is equal to one. This is not the same as 0 not equal to 1. ! is sign of factorial in mathaugust_monthnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-27155869783539790802011-08-10T08:41:58.000-07:002011-08-10T08:41:58.000-07:00It seems like factors64[0]=0. I think it should be...It seems like factors64[0]=0. I think it should be factors64[0]=1august_monthnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-38179014479441399412011-08-09T19:12:02.000-07:002011-08-09T19:12:02.000-07:00august: What part of the code sample assumes that ...august: What part of the code sample assumes that zero is equal to one?guy ellisnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-28262833606392113702011-08-09T16:07:30.000-07:002011-08-09T16:07:30.000-07:00As far as I remember 0!=1As far as I remember 0!=1august_monthnoreply@blogger.comtag:blogger.com,1999:blog-125341290284114978.post-29515598916035461262011-07-03T00:32:05.000-07:002011-07-03T00:32:05.000-07:00Amen Brother. I have been asked this question so ...Amen Brother. I have been asked this question so many times on job interviews. And for a double (floating point being so slow), the double max is 170 factorial. Thats an array of 170 doubles. If you just index it your like very ZEN with code optimization. That could really matter if the calculation is at the center of a very repetitive call to the factorial function.Steven Packhamnoreply@blogger.com