76
\$\begingroup\$

Create the shortest program or function that finds the factorial of a non-negative integer.

The factorial, represented with ! is defined as such

$$n!:=\begin{cases}1 & n=0\\n\cdot(n-1)!&n>0\end{cases}$$

In plain English the factorial of 0 is 1 and the factorial of n, where n is larger than 0 is n times the factorial of one less than n.

Your code should perform input and output using a standard methods.

Requirements:

  • Does not use any built-in libraries that can calculate the factorial (this includes any form of eval)
  • Can calculate factorials for numbers up to 125
  • Can calculate the factorial for the number 0 (equal to 1)
  • Completes in under a minute for numbers up to 125

The shortest submission wins, in the case of a tie the answer with the most votes at the time wins.

\$\endgroup\$
  • 12
    \$\begingroup\$ How many of the given answers can actually compute up to 125! without integer overflow? Wasn't that one of the requirements? Are results as exponential approximations acceptable (ie 125 ! = 1.88267718 × 10^209)? \$\endgroup\$ – Ami Feb 6 '11 at 22:43
  • 6
    \$\begingroup\$ @SHiNKiROU, even golfscript can manage 125! less than 1/10th of a second and it's and interpreted interpreted language! \$\endgroup\$ – gnibbler Feb 8 '11 at 3:21
  • 5
    \$\begingroup\$ @ugoren the two-character solution to the other question uses a built-in factorial function. That's not allowed in this version of the challenge. \$\endgroup\$ – Michael Stern Jan 7 '14 at 3:18
  • 4
    \$\begingroup\$ Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? \$\endgroup\$ – sergiol Aug 24 '17 at 18:05
  • 4
    \$\begingroup\$ @sergiol Incredibly that hasn't been an issue in the last 2 years, I suspect most languages can get it done in under a minute. \$\endgroup\$ – Kevin Brown Aug 24 '17 at 21:20

181 Answers 181

1
3 4 5 6
7
0
\$\begingroup\$

C (gcc), 33 bytes

double f(n){return n?n*f(n-1):1;}

Output:

1
1
2
6
24
120
720
5040
40320
362880
3628800
39916800
479001600
6227020800
87178291200
1307674368000
20922789888000
355687428096000
6402373705728000
121645100408832000
2432902008176640000
51090942171709440000
1124000727777607680000
25852016738884978212864
620448401733239409999872
15511210043330986055303168
403291461126605650322784256
10888869450418351940239884288
304888344611713836734530715648
8841761993739700772720181510144
265252859812191032188804700045312
8222838654177922430198509928972288
263130836933693517766352317727113216
8683317618811885938715673895318323200
295232799039604119555149671006000381952
10333147966386144222209170348167175077888
371993326789901177492420297158468206329856
13763753091226343102992036262845720547033088
523022617466601037913697377988137380787257344
20397882081197441587828472941238084160318341120
815915283247897683795548521301193790359984930816
33452526613163802763987613764361857922667238129664
1405006117752879788779635797590784832178972610527232
60415263063373834074440829285578945930237590418489344
2658271574788448529134213028096241889243150262529425408
119622220865480188574992723157469373503186265858579103744
5502622159812088456668950435842974564586819473162983440384
258623241511168177673491006652997026552325199826237836492800
12413915592536072528327568319343857274511609591659416151654400
608281864034267522488601608116731623168777542102418391010639872
30414093201713375576366966406747986832057064836514787179557289984
1551118753287382189470754582685817365323346291853046617899802820608
80658175170943876845634591553351679477960544579306048386139594686464
4274883284060025484791254765342395718256495012315011061486797910441984
230843697339241379243718839060267085502544784965628964557765331531071488
12696403353658276446882823840816011312245221598828319560272916152712167424
710998587804863481025438135085696633485732409534385895375283304551881375744
40526919504877220527556156789809444757511993541235911846782577699372834750464
2350561331282878906297796280456247634956966273955390268712005058924708557225984
138683118545689864933221185143853352853533809868133429504739525869642019130834944
8320987112741391580056396102959641077457945541076708813599085350531187384917164032
507580213877224835833540161373088490724281389843871724559898414118829028410677788672
31469973260387939390320343330721249710233204778005956144519390914718240063804258910208
1982608315404440084965732774767545707658109829136018902789196017241837351744329178152960
126886932185884165437806897585122925290119029064705209778508545103477590511637067401789440
8247650592082471516735380327295020523842257210146637473076098881993433291790288339528056832
544344939077443069445496060275635856761283034568718387417404234993819829995466026946857533440
36471110918188683221214362054827498508015278133658067038296405766134083781086959639263732301824
2480035542436830547970901153987107983847555399761061789915503815309070879417337773547217359994880
171122452428141297375735434272073448876652721480628511030304905066123383956194496253690059725733888
11978571669969890269925854460558840225267029209529303278944419871214396524861374498691473966836482048
850478588567862176139364498862283450363106657876759119884137546969850291984346623845721803156845756416
61234458376886076682034243918084408426143679367126656631657903381829221022872956916891969827292894461952
4470115461512683367030518111879159855011125453675376127499372976904192242294440576507107183576895048384512
330788544151938558975078458606627397928594841525087028177611631961228972749086355889619432768006702663467008
24809140811395391401649674453868616759922516881580717884144963618612492793329521656999844718186305916810297344
1885494701666049846649767567286674986020753759697889931196791720482648043560619012598537844549685032003930423296
145183092028285837925033723190960213624220326225543510376814897182582303110787462992309948307745678711432381726720
11324281178206294606285193764734547659641544873910049469239570110699644621282776159978832493218689331409071173009408
894618213078297291394536105678124660091699861979864830895281485890971416487504167917951760559283842969422038852173824
71569457046263778832073404098641551692451427821500630228331524401978643519022131505852398484420816675798776564959674368
5797126020747365547859207609316955153302418317114924299299934140244381749043408420663995193999459259329102516576025837568
475364333701283981804950871934204857403260987909684614932289567004882674634326008655216234173410083475042065689611178344448
39455239697206569095363763848575524105091557652834963035068520647090338762195044371133838774594273621857211161281310377377792
3314240134565351991893962785187002255986138585985099085000359647021178112607661449751964466234594461331925608329126314254532608
281710411438054936145340070063731769270618697594701234453923080571636972801512907171791271651453851961008097129997800044545179648
24227095383672724277628115968482030522825707406365319023898287641277303090920578351271773357158562842391230015963508220979549569024
2107757298379526908723379823722428723253356281476796516744835439819327865545820130415624631892617112325588092315921658107822337949696
185482642257398355359015441641340379717002520724001675848028952155157918424578282117944000727442910232086550560628100855367408841392128
16507955160908452497218052643056785820348586593118454131228292379023767876373998621963681432374396360185860939835599722920061648806871040
1485715964481760688598126444658390468648504385338494631194665214503009929244428884407664620677918947601692538663274334960161140774153486336
135200152767840228116141248983464474000315190336164090023053168975093962659595589494945258181259227289565007146009064474002163016890673266688
12438414054641300055918190849808704283732243800785472203727097932000687811897290487171609691949473988773572358313712866199772466321053448142848
1156772507081640872708168771053910362702557453046286765300873130615713476227390429979366250504125810956242157790289615727775585913909863306493952
108736615665674240994816729183762001752135081532694567149618171011945851076444857687577437120278217589896360543234882091889200965381187714945122304
10329978488239052206885505130495304991006115078121225426977331552932345209983325487893772190631276157126501537780698430576453773283127019334195478528
991677934870949102715849029478410597802063033500204435194993914996262675344674144417713144475342926858025486847392744430654763018452435547907969515520
96192759682482062236598631563798937437476306361515295860273049067319419226943192827878886900710340579037421510433530649010990406523708314612471414390784
9426890448883242029410148360874034376137592466180063696357188182614769297217373775790759257383412737431326439501183709769874985637770333212700442263289856
933262154439440960911604687726529403237621654151826305939361630078862160424520003803285166480957861005701317510617187267217623578139262988057343784065695744
93326215443944102188325606108575267240944254854960571509166910400407995064242937148632694030450512898042989296944474898258737204311236641477561877016501813248
9425947759838353638138390835342801376610975723095888339920014242478768251047951733961834230251772401385381873852523273511991274827736142409989069734354272387072
961446671503512108551098468996872516993486281273776279655019164712488171916774592368567957322868226384982187894861027816146283489308561271194593264363559458963456
99029007164861753574104173353829959119840213587603550977595970143247198666981236326723820707135417324040064427202242800388622136039545500810497297411854931555516416
10299016745145621553359182054762848245165958006210981247509469384131511290543206553945828252373444680882643618952993963518745670258094979779977582459484649549641809920
1081396758240290348210869921049787686085357708169730988621187482453493212072612258663790673020750118457699980463572452092522082693661819116546316351266107589254107365376
114628056373470783195262178791869885150372134974981857049798601542754676287616326162938700437174657932394775194874661651509774290443489164051042101334162066851708352331776
12265202031961373185133888353370611130684668524854136840277622617824654987491120488233295563394527465880023230174031122970080372499095001280782895493213123617184939419631616
1324641819451828358912841223208903721969150593335392516905465643871506013804748686213541388027857383295519634150431801099139171361749124435300456711000167851248199786240671744
144385958320249281895211638114231048758962740708165300332574711789391685278558717651905972808586720726491483073402144430319927168280381361555797909879849011686559753441596407808
15882455415227421289655392351515189289144558208272049215939288190702874949438682229949505800912531174854106019567414459205248903105897894971654398555177121848553771682648345804800
1762952551090243665975210588885144142387414195100989311383803233846619844411373359108703401397152961517526948327940490533490958560703331680555091440947787242605540750086427113422848
197450685722107283218203224975563190350604098858598125302874203564961210901795886052940088784315404959246758100114554585320567376838138578357747136064291560700269291680195376450633728
22311927486598122561395742763464263293811085711458827504898079485289004131566259313948148502297697156551908058556057846875398914665071585162560040055953309730837353620093009923758096384
2543559733472186205984785013970489854470847792378977572407496727142253030323344177018786997575166305186774479690234182114834728179176544494308091621715820528124381901517757837570933260288
292509369349301414171317466983763626351066482490080838864228039834663473319338986788600397507339178163503285987117253188513945744543074764016065369127692817702167502857927244907779926786048
33931086844518965033194432062534716898733796047438137448775849025668322561295546839066546536978813348231985419477781328258303410431554030031323720045817206723636050739092232075729560101978112
3969937160808719035516914105546083316129144899609623379494025075823655775671863699734165199370837152945139637435339450080229269140755163261563772810417232690049049348643093126163025872065396736
468452584975428833021146646814567601649600858899761983789620882005863329785250305734040051053149920962518753508300395503370245666101721723083039844863345029073890156274277583663759649529785221120
55745857612076033333946596938961053806019179795001037546326945837492339367137742780510072138432293369384101420636637780506186496519649138194305750351243690774695184219682241207240231351096375771136
6689502913449123933681342530579439120523775756341546089360093784620090324126011858610763674005344861930779387322735825049576193801545269664994272835560837966773424760538449951889918907707412713570304
809429852527344001286822374367783120479258932042013350108502525209350161213688816895938003163169155685249328518343891519892014312451987782930100389629933788074784183691025963616992896186529128659288064
98750442008335975771386594694042667512406118845315870110284243810270370655301200902289870551084388902234050553564994114624253260948981065526033632124062906320002086008101445028038232030953741735287586816
12146304367025324845837253661169005205421689094860932220203889171869434996608192505473415582579316934206019475818161948117127665043471218350935019389220629267648749936761134253494071712465660221123700195328
1506141741511140361082970935625106973149134079213189040694064611290402469291784349376899830829867484515795051439621218300470678438729622083715830084690979092223507252900426925300358777584774193094861734805504
188267717688892538291710440519845543681884351351211642746915315538460412792684266823199728040717355805605164453895552619595370444449591794431421678523215389148946427029232083284552835657699349012546428480782336

Try it online!

| improve this answer | |
\$\endgroup\$
1
3 4 5 6
7

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.