33
\$\begingroup\$

Task

Given a non-negative integer \$n\$, evaluate the factorial \$n!\$.

The factorial is defined as follows:

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

Rules

  • All default I/O methods are allowed.
  • Standard loopholes are forbidden.
  • Built-ins are allowed.
  • There is no time or memory limit.
  • Giving imprecise or incorrect results for large inputs due to the limit of the native number format is fine, as long as the underlying algorithm is correct. Specifically, it is not allowed to abuse the native number type to trivialize the challenge, which is one of the standard loopholes.
  • This is . Shortest code in bytes wins, but feel free to participate in various esolangs (especially the ones hindered by the restrictions of the former challenge).

Test cases

0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
11! = 39916800
12! = 479001600

Note: We already have the old factorial challenge, but it has some restrictions on the domain, performance, and banning built-ins. As the consensus here was to create a separate challenge without those restrictions so that more esolangs can participate, here it goes.

Also, we discussed whether we should close the old one as a duplicate of this, and we decided to leave it open.

\$\endgroup\$

94 Answers 94

3
\$\begingroup\$

K (ngn/k), 6 bytes

Solution:

*/1+!:

Try it online!

Explanation:

*/1+!: / the solution
    !: / range 0..N
  1+   / add 1 (vectorised)
*/     / product

Extra:

  • Also */-!-: for the same byte count.
\$\endgroup\$
3
\$\begingroup\$

Rust, 20 bytes

|n|(1..=n).product()

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pepe, 57 bytes

3 bytes have been decreased from my Pepe factorial program.

rrEEReREEEEErEeREeEreEREErEEEEErEEEeeReererRrEEEEEEeEreEE

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Mini-Flak, 56 bytes

{(({}[()])({({}({}))[({}[({})()])]}{}{})[({})])}{}({}())

Try it online!

When golfing a fold operation in Brain-Flak, it is generally better to keep the stack small and use a "generator" than to push the entire list at once. I'm not exactly why this is true, but it typically works out that way. In this case, it avoids an entire swap operation, since the loop index can stay on top the whole time.

The other main optimization is to make the bottom of the stack equal the running total minus 1. This 1 has to be added back later, but this is shorter than pushing a 1 below the input at the beginning.

The multiplication algorithm here is a variant of the swap algorithm. Specifically, there are two equivalent swap algorithms: (({}({}))[({}[{}])]) calculates B as (A + B) - A, while (([{}]({}))([{}]{})) calculates B as (B - A) + A. To use this in a multiplication loop, we can modify these to not destroy B, and also decrement A by 1. There are three ways to add a single () to decrement A:

  • ([{}]()({}))([{}]({})) evaluates to B.
  • ([{}]({}))([{}()]({})) evaluates to B - 1
  • ({}({}))[({}[({})()])] evaluates to B + 1

The latter is what we want, since B is one less than the factor I actually want to represent. By multiplying A - 1 by B + 1 and then adding B, we obtain A(B+1) - 1 as desired.

# Running counter: A
# Running product: B+1

# Start main loop
{

  (

    # Subtract 1 from A
    ({}[()])

    # Multiply by B+1
    ({({}({}))[({}[({})()])]}

    # Add B to get A(B+1) - 1 and push
    {}{})

  # Cancel out product to push A-1 again
  [({})])

}{}

# Add 1 to get final result
({}())
\$\endgroup\$
3
\$\begingroup\$

Whispers v3, 25 bytes

> Input
>> 1!
>> Output 2

Try it online!

Squeezed pseudo code:

>> Output ((Input)!)

Explanation:

As always in Whispers, we run the last line first:

>> Output 2

This line outputs the result from line 2:

>> 1!

Use the result from line 1 to calculate the factorial (line 1)!

> Input

Takes the first line of the input.

So we get the squeezed pseudo code:

>> Output ((Input)!)
\$\endgroup\$
3
\$\begingroup\$

Python 3, 61 50 48 bytes

a=1
for i in range(int(input())):a*=i+1
print(a)

Try it online!

Thanks to Bubbler for shortening the code by 11 bytes. Another thanks to Jo King♦ for shortening the code by 2 bytes.

New contributor
r u even listening to me is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
\$\endgroup\$
3
  • \$\begingroup\$ You can remove the variable x and just use a*=i+1. And then you can write the for loop on a single line as for i in range(n):a*=i+1. Nice first answer. Also check out Tips for golfing in Python. \$\endgroup\$ – Bubbler May 11 at 2:32
  • \$\begingroup\$ Also since n is used only once, you can pass int(input()) to range(...) directly. Try it online! \$\endgroup\$ – Bubbler May 11 at 2:35
  • \$\begingroup\$ You can move the a*=i+1 to the same line as the for loop to save 2 bytes \$\endgroup\$ – Jo King May 11 at 5:39
2
\$\begingroup\$

Pyth, 2 bytes

*F

Try it online!

Alternate solution:

.!
\$\endgroup\$
2
\$\begingroup\$

05AB1E, 1 byte

!

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Zephyr, 72 bytes

input n as Integer
set f to 1
for i from 1to n
set f to f*i
next
print f

Try it online!

Same algorithm as my QBasic answer, just in a more-verbose syntax: If n is zero, the for loop does nothing and 1 is output. Otherwise, the for loop runs over i from 1 up to and including the input number, multiplying the result by each i.

\$\endgroup\$
2
\$\begingroup\$

Japt, 1 byte

l

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Charcoal, 8 bytes

IΠ…·¹∨N¹

Try it online! Link is to verbose version of code. Explanation:

      N     `n` as a number
     ∨      Logical Or
       ¹    Literal `1`
  …·¹       Inclusive range from `1` to `n`
 Π          Take the product
I           Cast to string
            Implicitly print
\$\endgroup\$
2
\$\begingroup\$

T-SQL, 91 bytes

CREATE FUNCTION dbo.f(@ INT)RETURNS INT AS BEGIN SET @=IIF(@<=1,1,@*dbo.f(@-1))RETURN @ END

Demo

It seems you need to specify a schema (dbo.) in order to make a recursive function :( Perhaps other SQL dialects are shorter.

\$\endgroup\$
2
\$\begingroup\$

ReRegex, 47 bytes

#import math
\b1!/1/(\d+)!/($1-1)!*$1/
#input
!

Defines two regexes;

(\d+)!/($1-1)!*$1/ Looks for a base10 number followed by a !, and replaces it with (n-1)!*n.

\b1!/1/ Replaces any free standing 1!'s with just 1.

The math library then takes care of the subtractions and multiplications. ReRegex will continue attempting its regex operations until it can no-longer modify the text.

The final two lines put the input as n! ready for the operations.

Values greater than 5 work offline, but due to how long they take; do not work on TIO.

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Perl 5 -p, 20 bytes

$\*=$_ for++$\..$_}{

Try it online!

\$\endgroup\$
5
  • 1
    \$\begingroup\$ Alternatively, for 20 bytes as well: $;=$_;$_*=$;while--$ ;-) \$\endgroup\$ – Dada Aug 25 '20 at 15:13
  • 1
    \$\begingroup\$ Nice to see you back around @Dada! \$\endgroup\$ – Dom Hastings Aug 25 '20 at 15:15
  • \$\begingroup\$ $_*="@F"while--$F[0] too (with -a)... there must be something shorter! \$\endgroup\$ – Dom Hastings Aug 25 '20 at 15:17
  • \$\begingroup\$ Oh, those don't work with 0 :( \$\endgroup\$ – Dom Hastings Aug 25 '20 at 15:29
  • \$\begingroup\$ Indeed, I didn't noticed that :x \$\endgroup\$ – Dada Aug 25 '20 at 15:32
2
\$\begingroup\$

Assembly (MIPS, SPIM), 108 bytes, 6*10 = 60 assembled bytes

main:li $v0,5
syscall
li $a0,1
beqz $v0,e
f:mul $a0,$a0,$v0
sub $v0,$v0,1
ble $v0,0,e
b f
e:li $v0,1
syscall

Try it online!

Explanation

main:
    li $v0, 5                  # set syscall code 5
    syscall                    # Read an integer into v0
    li $a0, 1                  # register a0 = 1
    beqz $v0, end              # Handle $v0 = 0 case
    fact:                      # Main procedure:
        mul $a0, $a0, $v0      #     a0 = a0 * v0
        sub $v0, $v0, 1        #     v0 = v0 - 1
        ble $v0, 0, end        #     If v0 <= 0, jump to end
        b fact                 #     Jump back to the start of the procedure
    end:                       # After-process:
        li $v0, 1              #     Set sycall code 1
        syscall                #     Output the number stored in a0
\$\endgroup\$
1
  • 1
    \$\begingroup\$ @640KB Sorry for the late response, it's fixed now. \$\endgroup\$ – user96495 Aug 26 '20 at 1:17
2
\$\begingroup\$

Muriel, 128 bytes

C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";@"B:\".$(1\";a:"+~+";C:\""+|C+C

Try it online!

Moving another answer from the previous challenge, this time because the answer uses an eval function... in a language where that's the only way to loop.

This is a difficult for a number of reasons. First is Muriel itself, where the only way to do loops is to create a quine and eval it, passing a condition into the code. Next is that large numbers lose precision over time (so the output for \$125!\$ is 1.88267717688893e+209), but the program can't handle that format inside the program itself, so you can't pass large numbers to the next iteration of code. The last problem is that numbers will eventually be so large, the program just renders them as Inf, but thankfully that's far beyond \$125!\$, so we don't have to worry about that.

Explanation:

The first iteration doesn't have to be a complete quine, but can take shortcuts in constructing the actual loop.

C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";

This creates an string version of the loop. This string persists across all iterations of the loop.

@                   # Evaluate the next strings as a new program
 "B:\".$(1\";       # Initialise B as the string ".$(1"
 a:"+~              # Initialise a as the inputted number
 +"C:\""+|C         # Initialise C as the persistent string C
 +C                 # And add the executing part of the program

The resulting program with input 125 looks like (newlines added for clarity)

B:".$(1";
a:125;
C:"\";@%(B+\");B:\\\"\"+B+\"*\"+$a+\"\\\";a:\"+$(a-1)+\";C:\\\"\"+|C+C),(a>0)*(&B+2),a*999+&B+2";
@%(B+");B:\""+B+"*"+$a+"\";a:"+$(a-1)+";C:\""+|C+C),(a>0)*(&B+2),a*999+&B+2

The eval then executes

@                   # Evaluate
 %(     ...     ),(a>0)*(&B+2),a*999+&B+2   # The substring
   B+");                                    # If a is 0, evaluate B
                                            # Otherwise
   B:\""+B+"*"+$a+"\";                      # Concatonate "*a" to the end of B
   a:"+$(a-1)+";                            # Decrement a
   C:\""+|C                                 # Set C to C
   +C                                       # And add the executing part of the program

Eventually, a will reach 0, and B will be evaluated. B will look like:

.                         # Print
 $(       ...         )   # The string form of
   1*125*124*123*...*1    # The factorial
  
  
\$\endgroup\$
2
\$\begingroup\$

COW, 150 bytes

oomMOOMOoMMMmoOmoOmoOMMMMMMMoOmOomOomOoMOOmoOMMMmoOmoOMMMOOOmOoMMMmOoMOOmoOMMMMOOmoOMoOmOoMOomooMMMmOoMOomoomOoMOoMMMmoomoOmoOmoOOOMOOOMOomOOmooMoOOOM

Try it online!

Commented

Four memory blocks are used. Block 1 stores a persistent counter. Blocks 2 and 3 store the temporary loop variables used for multiplication (by repeated addition). Block 4 stores the result.

oom                           # read input into Block 1 
MOO                           # if input is not 0
  MOoMMM                        # decrement and copy
  moOmoOmoOMMMMMMMoOmOomOomOo   # paste and increment to initialise Block 4
  MOO                           # Loop 1: from Block 1 value down to 1
    moOMMM                        # set Block 2 to Block 1 value
    moOmoOMMMOOOmOoMMM            # set Block 3 to Block 4 value and zero Block 4
    mOoMOO                        # Loop 2: from Block 2 value down to 1
      moOMMMMOO                     # Loop 3: as many times as Block 3 value
        moOMoO                        # increment Block 4
      mOoMOomooMMM                  # end Loop 3
    mOoMOomoo                     # end Loop 2
  mOoMOoMMMmoo                  # end Loop 1
  moOmoOmoOOOM                  # print Block 4 value
  OOOMOomOO                     # exit
moo                           # (only get here if input is 0)
MoOOOM                        # print 1
\$\endgroup\$
2
\$\begingroup\$

Haskell, 17 bytes

f n=product[1..n]

Try it online!

  • Thanks to 79037662 for suggesting it!

18 bytes

f 0=1
f x=x*f(x-1)

Try it online!

Or 19 bytes using fold.

f n=foldr(*)1[2..n]

Try it online!

\$\endgroup\$
1
  • 1
    \$\begingroup\$ Doesn't simply f n=product[1..n] work for 17 bytes? \$\endgroup\$ – 79037662 Aug 26 '20 at 21:43
2
\$\begingroup\$

Scala 2.12, 14 bytes

1 to _ product

This is a function expression, where the underscore is the parameter. 1 to _ creates a range object, and product calculates the product of all numbers.

\$\endgroup\$
5
  • \$\begingroup\$ May I ask why it's Scala 2.12 specifically? It looks like that code should work in other versions too. \$\endgroup\$ – user Sep 5 '20 at 20:26
  • \$\begingroup\$ @user In Scala 2.13, you get an error mesage: error: postfix operator product needs to be enabled by making the implicit value scala.language.postfixOps visible. It should work in older versions, though. \$\endgroup\$ – corvus_192 Sep 7 '20 at 7:54
  • \$\begingroup\$ You can also add the compiler flag -language:postfixOps, though. That’s not part of the byte count, right? \$\endgroup\$ – user Sep 7 '20 at 11:22
  • \$\begingroup\$ @user I think the common practice is to count compiler flags codegolf.meta.stackexchange.com/q/19. \$\endgroup\$ – corvus_192 Sep 8 '20 at 9:53
  • \$\begingroup\$ Oh, that’s too bad. \$\endgroup\$ – user Sep 9 '20 at 0:12
2
\$\begingroup\$

Ly, 9 bytes

1fR[s*]pl

Try it online!

Ly, for some reason, doesn't have &* (product) like it does &+ (sum). I guess I forgot about it 3 years ago when I made this terrible language.

\$\endgroup\$
2
\$\begingroup\$

Mornington Crescent, 3788 bytes

109 lines/ops

Take Northern Line to Embankment
Take Northern Line to Embankment
Take District Line to Parsons Green
Take District Line to Ravenscourt Park
Take District Line to Embankment
Take District Line to Embankment
Take Northern Line to Euston
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Victoria Line to Victoria
Take District Line to Turnham Green
Take District Line to Victoria
Take District Line to Victoria
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Victoria Line to Victoria
Take District Line to Turnham Green
Take District Line to Notting Hill Gate
Take District Line to Notting Hill Gate
Take District Line to Embankment
Take District Line to Embankment
Take District Line to Stamford Brook
Take District Line to Embankment
Take Northern Line to Goodge Street
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross
Take Northern Line to Tottenham Court Road
Take Northern Line to Tottenham Court Road
Take Northern Line to Moorgate
Take Northern Line to Moorgate
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Moorgate
Take Northern Line to Euston
Take Victoria Line to Seven Sisters
Take Victoria Line to Euston
Take Victoria Line to Euston
Take Northern Line to Moorgate
Take Northern Line to Moorgate
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Moorgate
Take Northern Line to Moorgate
Take Northern Line to Embankment
Take Northern Line to Embankment
Take Northern Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington
Take District Line to Hammersmith
Take District Line to South Kensington
Take District Line to South Kensington
Take Piccadilly Line to Bounds Green
Take Piccadilly Line to South Kensington
Take District Line to Ravenscourt Park
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith
Take District Line to South Kensington
Take District Line to South Kensington
Take Piccadilly Line to Bounds Green
Take Piccadilly Line to South Kensington
Take Piccadilly Line to South Kensington
Take District Line to Ravenscourt Park
Take District Line to Temple
Take District Line to West Kensington
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington
Take District Line to Hammersmith
Take Piccadilly Line to Eastcote
Take Piccadilly Line to Eastcote
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Eastcote
Take Piccadilly Line to Ealing Common
Take District Line to Ravenscourt Park
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith
Take Piccadilly Line to Eastcote
Take Piccadilly Line to Eastcote
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Eastcote
Take Metropolitan Line to Eastcote
Take Piccadilly Line to Ealing Common
Take District Line to Ealing Common
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington
Take District Line to Stamford Brook
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Stamford Brook
Take District Line to Hammersmith
Take District Line to Upminster
Take District Line to Ravenscourt Park
Take District Line to Upminster
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith
Take District Line to Embankment
Take District Line to Embankment
Take Northern Line to Angel
Take Northern Line to Embankment
Take Northern Line to Embankment
Take District Line to West Kensington
Take District Line to Embankment
Take Northern Line to Embankment
Take Northern Line to Mornington Crescent

Commented version for some explanations:

Take Northern Line to Embankment
Take Northern Line to Embankment
Take District Line to Parsons Green
Take District Line to Ravenscourt Park         # Save Input in Ravenscourt


Take District Line to Embankment
Take District Line to Embankment

                                               # Create -1

Take Northern Line to Euston
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Victoria Line to Victoria
Take District Line to Turnham Green
Take District Line to Victoria
Take District Line to Victoria
Take Victoria Line to Seven Sisters
Take Victoria Line to Victoria
Take Victoria Line to Victoria
Take District Line to Turnham Green
Take District Line to Notting Hill Gate
Take District Line to Notting Hill Gate
Take District Line to Embankment
Take District Line to Embankment
Take District Line to Stamford Brook           # Save -1 in Stamford Brook


Take District Line to Embankment

                                               # Create 1

Take Northern Line to Goodge Street
Take Northern Line to Charing Cross
Take Northern Line to Charing Cross
Take Northern Line to Tottenham Court Road
Take Northern Line to Tottenham Court Road
Take Northern Line to Moorgate
Take Northern Line to Moorgate
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Moorgate
Take Northern Line to Euston
Take Victoria Line to Seven Sisters
Take Victoria Line to Euston
Take Victoria Line to Euston
Take Northern Line to Moorgate
Take Northern Line to Moorgate
Take Metropolitan Line to Preston Road
Take Metropolitan Line to Moorgate
Take Northern Line to Moorgate
Take Northern Line to Embankment
Take Northern Line to Embankment
Take Northern Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington          # Save 1 in West Kensington

                                               # Save 1 as max value at Bounds Green, so we will later take the largest number between the input and 1 (to handle 0 as an input)

Take District Line to Hammersmith
Take District Line to South Kensington
Take District Line to South Kensington
Take Piccadilly Line to Bounds Green           # Save 1 as max
Take Piccadilly Line to South Kensington


Take District Line to Ravenscourt Park         # Get input
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith
Take District Line to South Kensington
Take District Line to South Kensington
Take Piccadilly Line to Bounds Green
Take Piccadilly Line to South Kensington
Take Piccadilly Line to South Kensington
Take District Line to Ravenscourt Park         # If input is 0 -> now it is 1


Take District Line to Temple                   # Save in jumpstack

                                               # Take Current Result (West Kensington) as first argument to Chalfont & Latimer

Take District Line to West Kensington
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington
Take District Line to Hammersmith
Take Piccadilly Line to Eastcote
Take Piccadilly Line to Eastcote
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Eastcote
Take Piccadilly Line to Ealing Common

                                               # Take Current Value (Ravenscourt Park) as second argument to Chalfont & Latimer

Take District Line to Ravenscourt Park
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith
Take Piccadilly Line to Eastcote
Take Piccadilly Line to Eastcote
Take Metropolitan Line to Chalfont & Latimer
Take Metropolitan Line to Eastcote
Take Metropolitan Line to Eastcote
Take Piccadilly Line to Ealing Common
Take District Line to Ealing Common

                                               # Save the new result n*(n-1) as the current result at West Kensington

Take District Line to Bank
Take District Line to Hammersmith
Take District Line to West Kensington

                                               # Put -1 (Stamford Brook) as the first argument of the addition in Upminster

Take District Line to Stamford Brook
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Stamford Brook
Take District Line to Hammersmith
Take District Line to Upminster

                                               # Put The current value (Ravenscourt Park) as the second argument of the addition in Upminster -> [decrement], save it in the current value

Take District Line to Ravenscourt Park
Take District Line to Upminster
Take District Line to Bank
Take District Line to Hammersmith
Take District Line to Ravenscourt Park
Take District Line to Hammersmith


Take District Line to Embankment
Take District Line to Embankment


Take Northern Line to Angel                    # Check if we reached 0 as current value, if it equals 0, break the loop and continue here:


Take Northern Line to Embankment
Take Northern Line to Embankment
Take District Line to West Kensington          # Get the final result
Take District Line to Embankment
Take Northern Line to Embankment


Take Northern Line to Mornington Crescent      # Print and exit

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Flurry -nii, 14 bytes

{}{<({})[]>}{}

Try it online!

Takes single number from the stack and prints its factorial from the return value.

The online interpreter implements certain arithmetic shortcuts, so it computes and prints 125! in an instant.

How it works

Iterate through 1 to n using the stack height and multiply all of them to the starting value of 1.

// n is the only content of the stack at program start
// 1 is popped from empty stack
main = pop push-mul pop
     = n push-mul 1

// height yields 1 to n, since it is called after a push
// <a b> = a * b (where a, b are Church numerals)
push-mul = \x. <(push x) height>
\$\endgroup\$
2
\$\begingroup\$

Rockstar, 70 66 62 61 60 55 bytes

listen to N
cast N
F's1
while N
let F be*N
let N be-1

say F

Try it here (Code will need to be pasted in)

\$\endgroup\$
2
  • \$\begingroup\$ Say F to Pay Respects \$\endgroup\$ – Razetime Sep 9 '20 at 16:07
  • \$\begingroup\$ I'm tempted to rename F to X... \$\endgroup\$ – Bubbler Sep 10 '20 at 1:58
2
\$\begingroup\$

PowerShell Core, 37 32 bytes

param($a)1..($a+!$a)-join'*'|iex

Try it online!

Or recursive:

PowerShell Core, 37 36 bytes

filter f{if($_){$_*(--$_|f)}else{1}}

Try it online!

All bytes reductions thanks to mazzy :)

\$\endgroup\$
4
  • 2
    \$\begingroup\$ The recursive version should include the part that makes it a named function f, because the submission wouldn't work without it. So it is 49 bytes. \$\endgroup\$ – Bubbler Feb 15 at 3:32
  • 1
    \$\begingroup\$ and you can save some bytes Try it online! \$\endgroup\$ – mazzy Feb 15 at 5:47
  • 1
    \$\begingroup\$ recursive via filter Try it online! \$\endgroup\$ – mazzy Feb 15 at 11:25
  • 1
    \$\begingroup\$ and filter f{$_ ?$_*(--$_|f):1} (27 bytes) with ternary operator for PS7 \$\endgroup\$ – mazzy Feb 15 at 11:30
2
\$\begingroup\$

Vyxal, 1 byte

¡

Try it Online!

Built-in factorial.


No built-in, 2 bytes

ɾΠ

Explanation:

    # Implicit input
ɾ   # Range [1, N]
 Π  # Reduce by multiplication
    # Implicit output

Try it Online!

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

Keg, -hr, 4 bytes

Ï_∑*

Try it online!

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

SNOBOL4 (CSNOBOL4), 65 bytes

 i =input
 p =1
i x =x + 1
 p =p * x
 output =p le(i,x) :f(i)
end

Try it online!

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

Python 3.8, 23 21 bytes

Saved 2 bytes thanks to Bubbler!!!

import math
math.perm

Try it online!

\$\endgroup\$
4
  • 1
    \$\begingroup\$ I think f= can be excluded from the byte count? \$\endgroup\$ – Bubbler Aug 24 '20 at 23:44
  • \$\begingroup\$ @Bubbler You get so used to things you forget what their orginal purpose was - thanks for the heads up! :D \$\endgroup\$ – Noodle9 Aug 24 '20 at 23:55
  • \$\begingroup\$ What's the point of the math.perm line? I mean, sure, we use math.perm() in the footer, but you can take out that math.perm line and the code will still work. (Then again, maybe the import math line can just be placed in the header, which would leave... 0 bytes? Hm... maybe we should count math.perm.) \$\endgroup\$ – J-L Aug 25 '20 at 17:41
  • \$\begingroup\$ @J-L In function answers the whole point is to provide enough code to define a function that solves the challenge. Here the function is math.perm so job done. Leaving anything out doesn't provide a full solution. \$\endgroup\$ – Noodle9 Aug 25 '20 at 17:52
1
\$\begingroup\$

Io, 20 bytes

method(\,\factorial)

Try it online!

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

Seriously, 2 bytes

,!

Try it online!

\$\endgroup\$

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.