Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
2.50/5 (5 votes)
See more:
hi How can we calculate factorial of thousand of number in C#

i use this code but if i input 2000 then no result is found.
please help me.

code:

C#
long x = Convert.ToInt32(TextBox1.Text);
long fact = 1;
if (x == 1)
 {
    Label1.Visible = true;
    Label1.Text = Convert.ToString(fact);
 }
 else
  {
    long i = 1;
    while (i <= x)
    {
        fact = fact * i;
        i++;
    }
    Label1.Visible = true;
    Label1.Text = Convert.ToString(fact);
   }
Posted
Updated 11-Apr-11 19:57pm
v3
Comments
Toli Cuturicu 12-Apr-11 4:28am    
Joke, right?

Look up the BigInteger class. It might take a while, but it will get an exact answer. I believe it is built into .NET 4.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 1:54am    
Why could it take for a while? 20000 iterations? No time! Programming it also takes no time.
See the result in my Answer...
--SA
Peter_in_2780 12-Apr-11 2:01am    
My 386DX almost melted! :) Agreed, coding takes no time if you've got VS installed...
Wild-Programmer 12-Apr-11 1:58am    
Thank you so much for sharing this info. I learnt new this about data types :)
My 5+.
Olivier Levrey 12-Apr-11 3:52am    
Good to know about BigInteger class. My 5. However, I don't really see when such a class could be usefull in real world.
Peter_in_2780 12-Apr-11 4:03am    
Heavily used in public key cryptography, which likes to deal in 200+ digit numbers. I'm pretty sure that was the motivation for the development of BigInteger libraries in various languages/frameworks.
You must be joking! I just calculated that factorial, it is expressed by the integer value with its decimal presentation which is 77388 decimal places long!

No CPU integer type can carry so much information.

If you can use Framework 4.0 you can use System.Numerics.BigInteger for such calculation:

C#
static BigInteger Factorial(int arg) {
    BigInteger value = 1;
    for (int index = 2; index <= arg; index++)
        value *= index;
    return value;
}


Let's test it and compare with maximum long:

C#
string factorial = string.Format("{0} decimal places", Factorial(20000)); 
Console.WriteLine(factorial.Length);
Console.WriteLine(long.MaxValue);


Output:
77338 decimal places
9223372036854775807


Sorry I don't want to print the integer value 77338 digits long.
No wonder you could not do it using long!

—SA
 
Share this answer
 
v3
Comments
Wild-Programmer 12-Apr-11 1:58am    
Thank you so much for sharing this info. I learnt new thing about data types :)

My 5+.
Sergey Alexandrovich Kryukov 12-Apr-11 2:05am    
You're very welcome and thank you for the voting.
--SA
Peter_in_2780 12-Apr-11 1:59am    
System.Double can't handle 10^77338, but you could use it to calculate the logarithm by Stirling's approximation.
Sergey Alexandrovich Kryukov 12-Apr-11 2:05am    
Opps! Thank you for the note, I'll fix it now.
--SA
Olivier Levrey 12-Apr-11 3:49am    
Good answer, my 5. I laughed too when I saw the question. Understanding underlying data types should be taught deeply prior to programming.
Mind blowing question. I never thought this out but this is true. Actually double is the largest data type in .NET but the factorial of 2000 will be such a huge value that computer is actually not able to hold the value.

Thats why even if I run your code with double data type, the answer that comes for value 2000 is Infinity.

Try my code below and see :)

C#
long x = Convert.ToInt32(2000);
double fact = 1;
if (x == 1)
{
}
else
{
    long i = 1;
    while (i <= x)
    {
        fact = fact * i;
        i++;
    }
    MessageBox.Show(Convert.ToString(fact));
}
 
Share this answer
 
Comments
Peter_in_2780 12-Apr-11 1:51am    
see my answer re BigInteger [edit] and SA's too [/edit]
Sergey Alexandrovich Kryukov 12-Apr-11 11:17am    
Amit, the code should be much shorter and simpler, see my samples.
--SA
Using String Multiply.
I've Done It In Powershell.
Calculate 2000! In Powershell[^]

function MUL {
    param ([string]$a ,[string]$b)
    [string]$res=""
    [bigint]$m=0
    $a.Split()|%{
    [bigint]$c=[bigint]$_*[bigint]$b+$m;
    [bigint]$d=$c%10
    $m=($c-$d)/10

    $res=$res+$d.ToString();
    }
    $res=$m.ToString()+$res;
    $res
}




VB
PS C:\> C:\BigNumberMultiply\MUL.ps1
2000!=3316275092450633241175393380576324038281117208105780394571935437060380779056008224002732308597325922554023529412258341092580848174152937961313866335263436889056340
5855616394060511725257187064785639354404540524395746703767410872297043468415834375243158087753364512748799543685924740803240894656150723325065279765575717967153671868935
9056112815871601717232657156110004214012420433842573712700175883547796899921283528996665853405579854903657366350133386550401172012152635488038268152152246920995206031564
4185654806759464970515522882052348999957264508140655366789695321014676226713320268315522051944944616182392752040265297226315025747520482960647509273941658562835317795744
8287631459645037399132733417726360885249009350662161014445970941270782131373256383157230201994991495831647094277447387032798554967429860883937632682415247883438746959582
9257740574539837501585815468136294217949972399813599481016556563876034227312912250384709872909626622461971076605931550201895135583165357871492290916779049702247094611937
6077851651106844322559056487362665303773846503907880495246007125494026145660722541363027549136715834060978310749452822174907813477096932415561113398280513586006905946199
6525731074117708151992256451677857145805660218565476095237746301667942248844448579834980154803262082989096585738175188861937669282827988845358463989659421395298446529109
2009103710046149449915828588050761867924946385180879874512891408019340074625920057098729578599643650655895612410231018690556060308783629110505601245908998383410799367902
0520768586691834779065585447001486926569246319333376124280974200671728463619392496986284687199934503938893672704871271727345617003548674775091029555239535479411074219133
0135681954109194146276641754216158762526285808980122244389024867718205495941575199170127176757178749586161966593187885514183578209260148207177733173539603430496908207058
9958701381980813035590160762908388574561288217698136182483576739218303118414719133986892842344000779246691209766731651433494437473235636572048844478331854941693030124531
6762327453678793228474738244850922831399525097325059791270310476836014811911022292533726976938236700575656124002905760438528529029376064795334581796661238396052625491071
8666386935476610845504619810208405063582767652658949239324951968595417167241932953068367349554400458635983816104305944982662753060542358075589410827888042782595108988063
5410567917950974017780688782869810219010900148352061688883720250310665922068601483649830532782088263536558043605686781284169217133047141176312175895777122637584753123517
2309905498292101346873042058980144180638753826641698977042377594062808772537022654265305808623793014226758211871435029186376363403001732518182620760397473695952026426323
6414544685111342720215045838385101013694131303485622191663162389263276581535501127630782505996915882453345743543786368317373067329658935519969445823687350883027865770087
9749889992343555566240682834763784685183844973648873952475103224222110561201295829657191368108693825475764118886879346725191246192151144738836269591643672490071653428228
1526612478004639225449451703637236279407577845420910483054616561906221742869816029733240465202019928138548826819510072828697010707375009276664875021747753727423515087482
4672027417003158112280589617812216074743794751095062093855667458125251837668215771280786149925587613235295042234638787895485088576446613629039412766597804420209228133798
7115900896264878942413210454925003566670632909441579372986743421470507213588932019580723064781498429522595589012754823971773325722910325760929790733299545056388362640474
6502450808094691160726320874941439730007041114185955302788273576548191820024496977611113463181952827615909641897909581173386272060889104329452449785351470141124421430554
8608963957837834732532359576329143892528839398625627324286277556314046383038916842163311344563630957196597846633855149231619633567535513840342580416291983782226690952177
0153175338730284610841886554138329171951332117895728541662084823682817932512931237521541926970269703299477643823386483008871530373405666383868294088487730721762268849023
0849346611942601802726138021080050782157410060548482013478595781027707077806555127725405016743323960662532164150048087724030476119290322101543853531386855384864255707907
9534117651957118868373988068389579274374968349814292329219630977709014393684365533335930782018131299345502420604456334057860696247196150560339489952332180043435996725662
3927196435402872055475012079854331970674797313126813523653744085662263206768837585132782896252333284341812977624697079543436003492343159239674763638912115285406657783646
2139112474470512552263427012395270181270454916480459322481088586746009523067931759677555810116799400052498063037631413444122690370349873557999160092592480750524855415682
6628176081544630830540667741263012444186420410837311909313000115447056027777372437806718889977085105672727678124719883285769584421758889516046786820481001004781646235822
0838532488134270834079868486632162720208823308727819085378845469131556021728873121907393965209260229101477527080930865364979858554010577450279289814603688431821508637246
2169678722821693473705992862771124476909209029883201668301702734202597656717098633112163495021712644268271196502640542282317596308744753018471940955242634114984695080733
9008000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
 
Share this answer
 
v2
Hi,

The zero was caused by the result after the 66!, you need to take into consideration the limitation of long that is up to 9,223,372,036,854,775,807 only. Try using using System.Numerics as it has a higher max value. On 200 factorial, we are talking of a very huge digits here. So huge your ancestors will be shocked.

Something like this.

C#
var limit = 200;
BigInteger fact = 1;

if (limit != 1) {
    var i = 1;
    while (i <= limit) {
        fact *= i;
        ++i;
    }
}

Console.WriteLine(fact);
 
Share this answer
 
v3
Comments
Sergey Alexandrovich Kryukov 12-Apr-11 11:18am    
The code must be much shorter and simpler, please see my sample. This code won't even compile, by the way.
--SA
Pong D. Panda 12-Apr-11 22:30pm    
It does compile. Do you compile it in your head? The code is based on the original poster's code. And since you were looking down on my code, I can share you a piece of advice that can optimize the code you gave. Instead of for (int index = 2; index <= arg; index++) , you can use for (int index = 2; index <= arg; ++index) . Its faster.
Sergey Alexandrovich Kryukov 12-Apr-11 22:51pm    
This will compile, but I was responding when it was an older revision with Convert (why on earth) and no BigInteger. I made a mistake even then, sorry. The code I criticized would not work for factorial of 20000, anyway. Even this code is excessive and limit, why while loop? It's simpler.

Well, thank you for the performance tip about ++index, but... why it's faster? Do you know?
I will need to check it up anyway.

Thank you.
--SA
Pong D. Panda 12-Apr-11 22:59pm    
Try hooking up a reflector and you will see the diff on ++i and i++ for more details, its something about the compiler optimization when you dont need to return the i on the loop. As I said before, my code was based on the poster's piece of code.
Sergey Alexandrovich Kryukov 12-Apr-11 23:56pm    
Sure. It looks you're right about ++ performance, thank you very much for this advice!
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900