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\$
  • 13
    \$\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
  • 6
    \$\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
  • 5
    \$\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

184 Answers 184

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

Pepe, 60 bytes

rrEERREeeeeeeeErEeREeEreEREErEEEEErEEEeeReererRrEEEEEEeEreEE

Try it online!

| improve this answer | |
\$\endgroup\$
0
\$\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.

| improve this answer | |
\$\endgroup\$
  • \$\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\$ – NoOneIsHere Aug 3 '19 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 Aug 3 '19 at 22:33
  • \$\begingroup\$ You are right Jo, I fixed it. \$\endgroup\$ – T. Salim Aug 4 '19 at 0:05
  • \$\begingroup\$ Thank you A__, I removed the parentheses. \$\endgroup\$ – T. Salim Aug 4 '19 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 Aug 4 '19 at 2:45
0
\$\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.

| improve this answer | |
\$\endgroup\$
0
\$\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
| 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.