92
\$\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
  • 15
    \$\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
    Commented Feb 6, 2011 at 22:43
  • 7
    \$\begingroup\$ @SHiNKiROU, even golfscript can manage 125! less than 1/10th of a second and it's and interpreted interpreted language! \$\endgroup\$
    – gnibbler
    Commented Feb 8, 2011 at 3:21
  • 8
    \$\begingroup\$ Completes in under a minute seems a very hardware-dependent requirement. Completes in under a minute on what hardware? \$\endgroup\$
    – sergiol
    Commented Aug 24, 2017 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\$ Commented Aug 24, 2017 at 21:20
  • 5
    \$\begingroup\$ Why aren't built-ins allowed? You haven't specified what built-ins are, and if you said that it was up to a "reasonable person" to decide (which is completely subjective, but ignoring that), you still say that any form of eval is a built-in for the factorial, even though it evaluates code, not the factorial of a given number. \$\endgroup\$
    – MilkyWay90
    Commented May 7, 2019 at 2:02

217 Answers 217

1
4 5
6
7 8
1
\$\begingroup\$

Julia, 14 characters

f(n)=prod(1:n)

although I think recursive implementations are more interesting for this challenge. The best I could do there was 18 characters:

f(n)=n<1||n*f(n-1)

for n = 125, note that one has to use a BigInt like this:

julia> f(big(125))
188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000

Both complete in much less than a second.

\$\endgroup\$
1
  • \$\begingroup\$ by overloading !, you can shave of a few bytes: 12 bytes: !n=prod(1:n) and recursive in 15 bytes: !n=n<1||!(n-1)n \$\endgroup\$
    – MarcMush
    Commented Apr 20, 2021 at 15:19
1
\$\begingroup\$

C, 37 bytes

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

This program accepts one number from stdin and then prints the answer in floating point form.

\$\endgroup\$
8
  • \$\begingroup\$ Hello, and welcome to Code Golf! This is a great answer, but to reduce the byte count even more, why not remove the extra spaces? \$\endgroup\$ Commented Aug 3, 2019 at 22:23
  • \$\begingroup\$ Oh, so you only want the nonmain function. Alright. I will keep that in mind for next time. \$\endgroup\$
    – T. Salim
    Commented Aug 3, 2019 at 22:33
  • \$\begingroup\$ You are right Jo, I fixed it. \$\endgroup\$
    – T. Salim
    Commented Aug 4, 2019 at 0:05
  • \$\begingroup\$ Thank you A__, I removed the parentheses. \$\endgroup\$
    – T. Salim
    Commented Aug 4, 2019 at 2:41
  • \$\begingroup\$ No, I tried to simply use long but the computer outputted 0 due to overflow, so I kept it as long double. \$\endgroup\$
    – T. Salim
    Commented Aug 4, 2019 at 2:45
1
\$\begingroup\$

Pip, 13 bytes

Fi,a{o*:i+1}o

Try it online!

Recursive factorial is given in Pip documentation, so I did iterative. Also, it couldn't handle up to 125! anyway.

\$\endgroup\$
1
\$\begingroup\$

Wren, 34 bytes

Generate range from a to 1 and then reduce it with product of this whole sequence.

Fn.new{|a|(a..1).reduce{|a,b|a*b}}

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Keg, 9 5 4 bytes

Ï⑨∑*

Try it online!

-1 byte thanks to @A̲̲

Answer History

5 Bytes

Ï_1∑*

Try it online!

This:

  • Takes the (Ï)ota of the implicit input (pushes input, input - 1, input - 2 ... 0)
  • Pops the bottom 0
  • Pushes an extra 1 (so that the 0 case works)
  • Multiplies the entire stack together
\$\endgroup\$
1
1
\$\begingroup\$

@, 8 bytes

#*¨1^ň

Explanation

     ň Input a single number from STDIN
    ^  Increment this number
  ¨1   Exclusive range from 1 to this number
#*     Fold multiplication over this vector
\$\endgroup\$
1
\$\begingroup\$

W, 5 bytes

*R:e+

Explanation

*R    % Reduce by the product
  :e  % Is this item 0?
    + % Add this value to the product
      % We need to also calculate fact(0)
      % Implicit output
\$\endgroup\$
1
\$\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!

\$\endgroup\$
1
  • \$\begingroup\$ How would the method posted on sci.comp.stackexchange.com performs compared to the above methods. The method basically cut the number of multiplications by half. See link below. I cannot code but I am curious to know how the method performs. Here's the link: scicomp.stackexchange.com/questions/42510/… \$\endgroup\$
    – user25406
    Commented Mar 1, 2023 at 19:12
1
\$\begingroup\$

MAWP, 31 bytes

%@<:.>!!1A[1A~!1A~]%1A[1A~W~]%:

125! takes around 11 seconds, so it fulfills the criteria.

Try it!

\$\endgroup\$
7
  • \$\begingroup\$ 19 bytes: |[!1A]%_1A[%W_1A]~: \$\endgroup\$
    – lyxal
    Commented Aug 13, 2020 at 7:49
  • \$\begingroup\$ That's very, very different from mine. probably better as it's own answer. \$\endgroup\$
    – Razetime
    Commented Aug 13, 2020 at 7:51
  • \$\begingroup\$ Also, it doesn't seem to work for 0. \$\endgroup\$
    – Razetime
    Commented Aug 13, 2020 at 7:52
  • \$\begingroup\$ oh dang. i'll fix that. \$\endgroup\$
    – lyxal
    Commented Aug 13, 2020 at 7:52
  • \$\begingroup\$ never mind, it does work for 0 \$\endgroup\$
    – lyxal
    Commented Aug 13, 2020 at 7:53
1
\$\begingroup\$

MAWP, 19 bytes

|[!1A]%_1A[%W_1A]~:

Utilises this input/output consensus that numbers can be taken as ascii characters.

MAWP, 19 bytes

@[!1A]%_1A[%W_1A]~:

Try it!

If you want to enter normal numbers like normal people.

\$\endgroup\$
1
  • \$\begingroup\$ Increment quote here \$\endgroup\$ Commented Aug 13, 2020 at 8:11
1
\$\begingroup\$

Lua (LuaJIT), 36 bytes

x=1 for i=1,...do x=x*i end print(x)

Try it online!

Because who needs recursion anyways?

I'm using LuaJIT instead of mainline Lua because Lua 5.3+ would make x 64-bit signed integer (and, sadly, LuaJIT implements Lua 5.1), making code fail on big values. It costs single byte to fix it:

Lua, 37 bytes

x=1. for i=1,...do x=x*i end print(x)

Try it online!

Now it's double again.

\$\endgroup\$
1
\$\begingroup\$

JavaScript (V8), 17 bytes

f=n=>n?n*f(n-1):1

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal, 2 bytes

ɾΠ

Range of 1 to input and product

Try it!

\$\endgroup\$
1
1
\$\begingroup\$

RickRoll-Lang, 164 160 96 bytes

eval too op lol

takemetourheart
ijustwannatelluhowimfeeling eval("*".join(map(str,[1,*range(1,int(input())+1)])))

Explanation:

RickRoll-Lang keywords do not need spaces between them

takemetourheart => main function declaration

ijustwannatelluhowimfeeling eval("*".join(map(str,[1,*range(1,int(input())+1)]))) => print the evaluated result of joining "*" a mapping of string to each item in a list of 1 and the range from [1, n)

=> implicit say goodbye (end block)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

x86 32-bit machine code, 69 bytes

\x6a\x01\x58\x89\x03\x89\xc1\xc1\xe1\x02\x29\xcc\x89\xe7\x89\xde\xf3\xa4
\x89\xc5\x89\xd1\x49\x7e\x25\x31\xf6\x89\xf7\xf7\xdf\x8b\x3c\xb4\x11\x3c
\xb3\x19\xff\x46\x39\xee\x7c\xf1\xf7\xdf\x74\x07\x83\x04\xb3\x01\x46\x72
\xf9\x39\xf0\x0f\x4c\xc6\xeb\xd8\x8d\x24\xac\x4a\x7f\xc1\xc3

Try it online!

Unlike other machine code entries, I implemented arbitrary-precision multiplication from scratch.

The algorithm is the most basic one. For \$a × b\$, \$a\$ is added \$b\$ times with a loop. It doesn't scale well for really big numbers, but 125! is calculated within 0.3 seconds (user time) in TIO.

The function outputs an arbitrary-precision integer in base \$2 ^ {32}\$. The challenge doesn't require the function to output a string, so I can use any external method to convert the output to printable string apart from the challenge. The huge footer in TIO is the source code of mini-GMP that I copy-pasted for bignum-to-decimal-string conversion. TIO has GMP, but only for 64-bit, unfortunately.


The code is quite straightforward. Here is a pseudocode to illustrate what the assembly code is doing.

for x = y!:
  x = 1
  i from y to 1:
    z = x
    j from y - 1 to 1:
      x += z

assembly (nasm)

; input: edx, output: ebx (bignum array), eax (number of "limbs")
; custom calling convention, everything except `ebx` and `esp` gets dirty
_fac:
    push 1
    pop eax
    mov [ebx], eax
.L0:
    mov ecx, eax
    shl ecx, 2
    sub esp, ecx
    mov edi, esp
    mov esi, ebx
    rep movsb
    mov ebp, eax
    mov ecx, edx
.L1:
    dec ecx
    jle .1
    xor esi, esi
    mov edi, esi
.L2:
    neg edi
    mov edi, [esp + esi * 4]
    adc [ebx + esi * 4], edi
    sbb edi, edi
    inc esi
    cmp esi, ebp
    jl .L2
    neg edi
    jz .0
.L3:
    add dword [ebx + esi * 4], 1
    inc esi
    jc .L3
.0:
    cmp eax, esi
    cmovl eax, esi
    jmp .L1
.1:
    lea esp, [esp + ebp * 4]
    dec edx
    jg .L0
    ret
\$\endgroup\$
1
\$\begingroup\$

rusty_deque, 25 bytes

1~+~1~{*~}~1~rot!2~range~

Expects a non-negative integer n on the deque, and a clean deque.

Explanation

# given an int n (n -- n!)
1~ +~  # exclusive end for range
1~     # guard for final multiplication
{*~}~  # loop body: multiply the right two numbers
1~     # index step
rot!   # ending index (n+1), rotate from the front of the deque
2~     # starting index
range~ # for i in range(2, n+1, 1)...

A version that doesn't assume a clean deque, 29 bytes:

1~+~rot~1~{*~}~1~rot!2~range~

Explanation

# given an int n (n -- n!)
1~ +~  # exclusive end for range
rot~   # save the range end at the front of the deque
1~     # guard for final multiplication
{*~}~  # loop body: multiply the right two numbers
1~     # index step
rot!   # ending index (n+1), bring the range end back
2~     # starting index
range~ # for i in range(2, n+1, 1)...
\$\endgroup\$
1
\$\begingroup\$

Ruby, 25 bytes

f=->(n){n!=0?n*f.(n-1):1}

Ruby's lambda function is a bit verbose than it should have been.

Attempt This Online!

\$\endgroup\$
1
\$\begingroup\$

J-uby, 15 bytes

:+|:+&[1]|:/&:*

Try it online!

\$\endgroup\$
1
\$\begingroup\$

JavaScript, 17 bytes

f=n=>!n||n*f(n-1)
\$\endgroup\$
1
  • \$\begingroup\$ How would the method posted on sci.comp.stackexchange.com performs compared to the above methods. The method basically cut the number of multiplications by half. See link below. I cannot code but I am curious to know how the method performs. scicomp.stackexchange.com/questions/42510/… \$\endgroup\$
    – user25406
    Commented Mar 1, 2023 at 19:44
1
\$\begingroup\$

RASEL, 22 bytes

1&\:?v:1-3\-/
1\/.@>-1
\$\endgroup\$
1
\$\begingroup\$

Python 2, 59 55 bytes

lambda x:reduce(lambda a,b:a*b,range(1,x+1))if x else 1

This solution uses Python 2 because reduce is built-in, in Python 3, it's part of functools. This solution is iterative, and it works for values larger than 100.

-4 by Steffan

\$\endgroup\$
0
1
\$\begingroup\$

Fig, \$2\log_{256}(96)\approx\$ 1.646 bytes

ra

See the README to see how to run this

ra # Takes a num as input
 a # Range [1, n]
r  # Product
\$\endgroup\$
1
\$\begingroup\$

Chocolate, 2 bytes

ΠỌ

Try it online!

Product of range \$[1, n]\$.

Or, using a builtin overload of product:

Chocolate, 1 byte

Π

Try it online!

\$\endgroup\$
1
\$\begingroup\$

dc, 22 bytes

[1q]sg[d2>gd1-d2<f*]sf

Try it online! (requires Javascript)

This is the obvious 12-byte implementation ([d1-d1<f*]sf) with an additional test to short-circuit the calculation for numbers less than 2 (which otherwise subtract too far, yielding a product of zero).

Results

$ time bash -c 'for i in 0 1 2 3 4 125 1000; do ./607.dc <<<$i | tr -dc "[0-9]"; echo; done'
1
1
2
6
24
188267717688892609974376770249160085759540364871492425887598231508353156331613598866882932889495923133646405445930057740630161919341380597818883457558547055524326375565007131770880000000000000000000000000000000
402387260077093773543702433923003985719374864210714632543799910429938512398629020592044208486969404800479988610197196058631666872994808558901323829669944590997424504087073759918823627727188732519779505950995276120874975462497043601418278094646496291056393887437886487337119181045825783647849977012476632889835955735432513185323958463075557409114262417474349347553428646576611667797396668820291207379143853719588249808126867838374559731746136085379534524221586593201928090878297308431392844403281231558611036976801357304216168747609675871348312025478589320767169132448426236131412508780208000261683151027341827977704784635868170164365024153691398281264810213092761244896359928705114964975419909342221566832572080821333186116811553615836546984046708975602900950537616475847728421889679646244945160765353408198901385442487984959953319101723355556602139450399736280750137837615307127761926849034352625200015888535147331611702103968175921510907788019393178114194545257223865541461062892187960223838971476088506276862967146674697562911234082439208160153780889893964518263243671616762179168909779911903754031274622289988005195444414282012187361745992642956581746628302955570299024324153181617210465832036786906117260158783520751516284225540265170483304226143974286933061690897968482590125458327168226458066526769958652682272807075781391858178889652208164348344825993266043367660176999612831860788386150279465955131156552036093988180612138558600301435694527224206344631797460594682573103790084024432438465657245014402821885252470935190620929023136493273497565513958720559654228749774011413346962715422845862377387538230483865688976461927383814900140767310446640259899490222221765904339901886018566526485061799702356193897017860040811889729918311021171229845901641921068884387121855646124960798722908519296819372388642614839657382291123125024186649353143970137428531926649875337218940694281434118520158014123344828015051399694290153483077644569099073152433278288269864602789864321139083506217095002597389863554277196742822248757586765752344220207573630569498825087968928162753848863396909959826280956121450994871701244516461260379029309120889086942028510640182154399457156805941872748998094254742173582401063677404595741785160829230135358081840096996372524230560855903700624271243416909004153690105933983835777939410970027753472000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000

real    0m0.014s
user    0m0.015s
sys     0m0.004s
\$\endgroup\$
1
\$\begingroup\$

Japt, 8 bytes

UòJ ¤r*1

Test it online!

How it works

UòJ ¤r*1   // Implicit: U = input integer                5
UòJ        // Create the inclusive range [-1..U].        [-1, 0, 1, 2, 3, 4, 5]
    ¤      // Slice off the first two items.             [1, 2, 3, 4, 5]
     r*1   // Reduce by multiplication, starting at 1.   1*1=1*2=2*3=6*4=24*5=120
           // Implicit output                            120
\$\endgroup\$
4
  • \$\begingroup\$ This does not handle the zero case correctly (0! should return 1). \$\endgroup\$ Commented Jan 27, 2016 at 3:40
  • \$\begingroup\$ @ՊՓԼՃՐՊՃՈԲՍԼ Thanks, fixed now. \$\endgroup\$ Commented Jan 27, 2016 at 23:21
  • \$\begingroup\$ I know this is old but, wouldn't á be enough? \$\endgroup\$ Commented Aug 7, 2018 at 17:51
  • \$\begingroup\$ @LuisfelipeDejesusMunoz I think you mean l, but yes :-) \$\endgroup\$ Commented Aug 8, 2018 at 15:30
1
\$\begingroup\$

Pushy, 3 bytes

RP#

Explanation:

R  \ Push the inclusive range of the input
P  \ Push the product
#  \ Print

Try it online!

\$\endgroup\$
0
1
\$\begingroup\$

Fourier, 18 bytes

1~NI(i^~iN*i~Ni)No

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Vyxal 3, 3 bytes

Ω}Π

Try it Online!

-2 bytes thanks to @lyxal

Explanation (outdated):

[ɾΠ|1­⁡​‎‎⁡⁠⁡‏‏​⁡⁠⁡‌⁢​‎‎⁡⁠⁣‏⁠‏​⁡⁠⁡‌⁣​‎‎⁡⁠⁢‏‏​⁡⁠⁡‌⁤​‎‎⁡⁠⁤‏⁠‎⁡⁠⁢⁡‏‏​⁡⁠⁡‌­
[      ## ‎⁡If the input is 1 or greater:
  Π    ## ‎⁢Product of
 ɾ     ## ‎⁣Range [1..n]
   |1  ## ‎⁤Else 1
💎

Created with the help of Luminespire.

\$\endgroup\$
6
  • \$\begingroup\$ Using builtins, this becomes ! for 1 byte. Without explicit factorial, ɾΠ or work for 2 bytes \$\endgroup\$
    – lyxal
    Commented Dec 1, 2023 at 22:58
  • \$\begingroup\$ @lyxal This challenge doesn't allow bulitins and the input 0 must output 1. \$\endgroup\$
    – Fmbalbuena
    Commented Dec 1, 2023 at 23:00
  • \$\begingroup\$ then ɾΩ}Π for 4 bytes \$\endgroup\$
    – lyxal
    Commented Dec 1, 2023 at 23:08
  • \$\begingroup\$ @lyxal can you explain to me why that works? \$\endgroup\$
    – Fmbalbuena
    Commented Dec 1, 2023 at 23:12
  • \$\begingroup\$ Ω}Π for 3 bytes \$\endgroup\$
    – lyxal
    Commented Dec 1, 2023 at 23:12
0
\$\begingroup\$

MuPAD – 7

`*`($n)

Computes n!, no recursion.

\$\endgroup\$
0
\$\begingroup\$

Clojure, 29

#(reduce * (range 1 (inc %)))
\$\endgroup\$
1
4 5
6
7 8

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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