none
calculate factorial via powershell scripting RRS feed

  • Question

  • hello guys i written script until calculate the factorial via powershell but i do not know what does not work !

    function factorial( [int] $n ) 

        $result = 1; 

        if ( $n -gt 1 ) 
        { 
            $result = $n * ( factorial ( $n - 1 ) ) 
        } 

        $result 
    }

    this function can not work with big number like that 999 how can i solve that problem?

    Friday, October 24, 2014 5:24 PM

All replies

  • The problem is that [int] is a 32-bit integer, where the maximum allowed is 2^31 - 1, or 2,147,483,647. 999! is much, much larger than that. You can handle larger integers if you use another datatype, but even [int64] only allows values up to 2^64 - 1. You might need to settle for [double] and not deal with exact answers.

    Richard Mueller - MVP Directory Services

    Friday, October 24, 2014 6:01 PM
  • i use [double] but again get

    The script failed due to call depth overflow.  The call depth reached 1001 and the maximum is 1000.

    please help me

    Friday, October 24, 2014 6:39 PM
  • I get similar results, with a slightly different function. I get results up to at least 170. From 180 through at least 990 I get "Infinity" for the answer. At 999 I get your error, which I believe cannot be avoided. Even if you avoided the call depth error, PowerShell seems to refuse to deal with such large numbers, and simply outputs "Infinity".

    This article goes into factorials in depth:http://en.wikipedia.org/wiki/Factorial

    It includes a table for very large values, plus a formula for approximating factorials of large numbers. You might be able to code the approximation formula.


    Richard Mueller - MVP Directory Services


    Friday, October 24, 2014 7:41 PM
  • I know this is a old post. Thought it may be useful for others who will be searching for number greater than 999.

    Use [bigint] type to run more than 999. I tried till 3500 it is working fine.  Here is the output.

    PS C:\Windows\System32\WindowsPowerShell\v1.0>[bigint]$number = Read-Host "Enter number to find factorial"

    $out = 1
    function recursion
    {
        param($numb)
        if($numb -ne 0)
        {
         $script:out = $script:out * ($numb)
         recursion ($numb-1)
        }
    }
    recursion $number
    Write-Host $out
    Enter number to find factorial: 3500
    23911281994776495250953874936936411845162297244287515703509898695877400885693800916446438447
    80877127638851368363249959030672600029662323803179235690144720473782421728959016757843397426
    65174285098178725230385718873410201580175259254117313278842360350645709184329876569408534087
    24715750617566222732303226840154263419251746785483375910828698228273867421800616453472208050
    18887013067452711566159649011196906135496774127338547316502122402915302308689630520625373225
    43685590156805381172101989286235672093708387076881037922024043169353754750563162564975919989
    15670575221653768641341988647832833422558267735251734876259222362623428353321763869751025989
    11222889453333476253232225457489575929066149668092417626332250361293300675444284091306142797
    79458358460214091774376376215664910913970794811067270469967652614715681435650602502715659854
    78467408060738749603499543276210294322568928814104750760356027462067127957125783828128607460
    24020419066669058161590216705031892742978089491474396176233660140815014930699281338295767876
    48903343040313232509102393480437849932578077767394100942315149628506552818743799625921142442
    36212454735141403761575147177973665222320533583110188483895346127040671447976134376182952941
    57170673549825721531620564993323514965349193954562608624892205563919295377997484911375309029
    93971775367411959193353227311189697831964494769449670843907802573074824576081608675610481199
    28258848757855935054032296449440466307003069250618839454971190478611042399249771151419405056
    80622542726832716460518527602424490794684263948329962124623176815620663827419914986260001863
    83919188586337690398358410097963645981845943904103085919424048994299893759272204944265425380
    20871211827468703786326042597080829034922560210934976532242158752434464727607581939526240824
    72916199344001067497532044965332979826710064748865125090583936196759638131447706003337602585
    54927858026930214303998018311920138214403116873328516243009101282709378168128763134083498933
    24813500829202069714257012025846349613953091185859790200599838829359871390725624512049464724
    97911947679500020690837173372035176114973676898381235806467425892146886232620866571953146949
    56264173960913196126575347297111534900392235253780359357395723416960087636892732601653435913
    58965568166113125754231480256332052192612463101666001667063180576523033978311334630306107942
    16234859339498075975507172472835763540291690364457066613683639788914540937888617548773882856
    43999732690060073196923109774931897808506499739385218018130806086283707110373583923268832731
    84226838733565736598364575843519371661324470808631963233147343967638922724657862137492051663
    81972760251860702904315478193785982064573811799185136025917530285717508127686618962585738780
    97533672909600504432905074504032787488476557745376205129311939566789495198515711967195965721
    09668872213922392626240915737294517939135380196086957047367727951924884196622610764076590828
    93667572369309768160721691589147633135660258414395652757295687443086660683753647650261345578
    28994497475487073211245024320481189431901731952348993832023514078306594181237727326990681611
    78723033469177307108214013041886483740737748256374828270107706820545570074247567383224779406
    05664739805746499320063000259139428488332551038060072587926646803218818241912926332236787212
    31203118509851044596098638994797499302637918284164533948517228669747178634804889803109452954
    40205250457309949945059429502192185147414309805618229249218602747970403845293464902245965431
    41876536485340373970576875397037367929297860267130045054550908269221028647093904967111353552
    75763314468477762036156570779430502809114971166283166255476621707864979422497249592131377432
    89189518444858100954674752795099217597942686367848085942799762416545478603680388100585997596
    02772355795471416749466918305450888263885838365818728938971785207844494510784969157070785624
    04155115356983243195289440474255734778268119173598515000453185167432649333662482010693585626
    94927730153104590005783265974736027539524239970984586348865411841017709047307688406747773603
    03147757662429574556487269820669314042369666580611555342672939380529113677820625687955377718
    63706492864505774181666322768230930961097985043810236164862377493892870646480514311677190684
    58229256925088206140599869704629800058198308900540305496582182138589068780093503970009954671
    96807663119679990760807341220495858371367788833590987100068744972888811656275790706126681238
    51619081732181725010529826662967133514067477484529702396859645143368651607811562489666874253
    60867836696726491145779413128492191155176327027870898838754875974672297789103514986986203594
    48196045833193385906893042355506871356517070229255894079409219331853903204438903945368733655
    75441692374169030955598075134280924752047390130262218792865986787433083401814583070256474678
    59582008191489333185639501324373882357819272470865744596067049771601021503786720206182267456
    80380691917832818325859966559092834533768939749410420948950970404437315948865044913217734479
    37548353360505545870347413798645587842403533283813838758293873496939938506342333359442707060
    52385093883859116783817374295235449344020085385906621277579023161801767704172615928601006965
    88285131683715073426067640557461420443051354642024397093833619330015074048892787154439076232
    48244509418443340817122102107981732361049513848676566490157086383252387160938588261038378478
    82884037423888057272955111393257727696163083430796722561513575655639061080238291215312141746
    15371180550548871727296634839749685243031341372501614290347242382783968450382492600640874618
    28722458568657299051099223293057334579816844913362114526542406750021892536789804227088391918
    86097822292449095183081600514249966844785770894829897233193731863715313635244463617310683484
    52895763660637873048451015377992723927520343258090535945615872706960519997432959945907174780
    67064983317097621660815078290181480733944366653976513505868490837972584249407882115897891032
    43206805469253011148895531521527508514894990519881278866213922737179696199148317017538873511
    77156185165165930821879992021591432954866484153180873813915838362100961934836442649720738026
    42449081973652790693801947084344690780924113641598941451936652492127257559144771554768141186
    83107811143924448525651991428444526420587659365484853587718945484704281327630091112686057651
    35601412736434217858889254579906126526033405354832257376224050511258494774074116697352977531
    93234545729091860567086786573744501469111572460286652771415569729547182244698334244680621601
    32531901577493295508019216234278326133754704376322238601242325592933000213787820774336577378
    84974656022110497856475881870728930051143683145574124575814383130071981073125778635419867841
    91904847014177558473614040979356249382088650968011260969484062430984185406079913654106954786
    48641286752454474582522209207420413470202444403286606109599297513270935843391521536232095397
    21888728676384945204607848101214528672409259721735182574399375651799245838024709652034640805
    75142650947642992612026722166097533056008189803689908958725332734834949578348014572692216087
    29454535501382350466583599702324037854290438128466321579451763888751214647171441832213295657
    79242880685108928289477217842036572230445744358443636312243480122508616169623189118122095537
    63853105735751158674398631000596766494614395245783222031994594903050745264680628057839687568
    33583496465156358930543059993002427397599110904564931368195145764238476534759854952835958510
    84657312538869849119967793461685550803224873808698413270775681309421891860966539651197396822
    20260919397021772933680534621590867892581862293947274033699254426726016983326738074894903751
    97684155554395562174905294382684540581680965506629642129532486400588578510250741128802430993
    19881925439499271057505280413435027374444533440384807063933599344955836065129030960510161625
    43738370786063460371810870458399102944630495841885162340149567983496716280826231245820184451
    95073954648604031593132641317715550061354754226951813390052258701142524852375359916031034640
    32974484152029112392645141273698850320644353584905526041311922803812665994007752050489875967
    35950640955949522827376905212190873343108955722824782447712471476206129623616506128613655178
    25951200526404998213050711638800851570800227832896071494450200220916197966116977768312689425
    40842789435153518337035568027517347635977352912387690294233072175913678641394078048836707388
    00625946816224609689541061964850167226897841665070631907924103102327396350567375344473787582
    91376190703271268199073430990086702954914683152116301846898979173403698681438349196509195837
    39251722556623693405444926963457286586202701447990199298986782831263237454665855937718187994
    28070720794962529625393795364657285135684437111767987824444419767516696440398019369298752547
    17425949257499630929286649445743088757848718242957171394806201239290586943146322838272823402
    08660010730870957620226634719552576523698172609780258658321682043949788034025544074146279381
    31400775152143768058466926711862554768917309824529795156397295222164680108509496575487865267
    04027956140239843498911124519241825799056808696104031239681156552211727473883309651912562881
    16039919515809095678927268466115778322510839531693056263919689537976621170035270852684922989
    23224247090505794634225735782125010060004759361701969808941052020910063750957322352346266224
    85510952888131860442549700183802899070564374852417474369195792308330721394429664877977053925
    70408159011622401003468271837707833596383260193991061349668745998676221510962670327577239264
    13308185422394911811043779840486872209104367379031203030321054677803884187910391508936241709
    82298799861737829723946841376538902614457837129034898871459917451929031696200943814272341034
    14636280374409193265993663672312077338878396721236152097768923701980909822681050038534644074
    03440494204842308798794162940200284020763082844627633978050913393894146367028891547616668181
    80404625753936826566487930619448338295957323406599918665853414792845591630827416706244434396
    28279278802946566337744430744171327084095160990442684729240208933669958790464823654294511465
    76976101697910754243036621294576276401246100755970789339675400189104501832268530841627265547
    19457694233235097077385784915121424282815172381131948990830603189777684496384000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
    0000000000000000000000000000000

    Thursday, July 26, 2018 3:23 PM