Factorial

From Esolang
Jump to navigation Jump to search

The factorial of a number is itself and all positive integers less than it multiplied together. It is commonly used as a code example for esoteric programming languages.

It can be defined as , which is the form of most implimentations. However, defining this recursively (the main method of coding factorials), we find that and , where . Using this, we can find that , showing that . We can also see that negative integers will be undefined due to division by 0.

The factorial function can also be generalised for noninteger reals and even complex numbers via the gamma function . Note that not .

For numbers with a real component strictly greater than 0, a fact discovered by Daniel Bernoulli. Another definition, discovered by Euler because of course it was, is which works for all complex numbers other than negative real integers. However, Bernoulli's definition is more generally accepted. Using repeatedly, complex numbers with real components at most 0 can be found if using Bernoulli's definition.

Implementations

)

]!|a|
 “»€a€,==,0«|§’1’’
 §’@‘!\»€a€,-,1«’’
]

1+

.111##^"/*\1+\<1+#

AGSPL

~!.

Behaviour

factorial = &[1|a<2 a * factorial:a-1]

Brainfact

*.

Replacing "*" with n +s.

Campbell

Every program calculates the factorial function.

Clip

r*`,On

Explained as:

r   .- reduce                     -.
 *`  .- with multiplication        -.
 ,   .- the list of numbers from   -.
  O   .- 1           to             -.
  n   .- the number represented by  -.

[implicit] the next line of input.

dcScript

[NO ERRLOG]
title "Factorial - dcScript implementation"
int fact
int cpt
int n
int c
fac:
defi fact 1
defi cpt 2
start:
ifmin cpt n
mult fact cpt
addi cpt 1
call start
ifend
ifeql cpt n
mult fact cpt
addi cpt 1
call start
ifend
sysprt "x! = "
dspvar int fact
sysprtn ""
_start:
sysprt "Entrez le x = "
getstream int n
ifeql n -1
call end
ifend
call fac
end:
sysprtn "x! = 1"
sysprtn "Press any key to continue..."
wait
quit

Flow chart

(Contains other showcases) simple math library

FunctionsFTW

 setNum("input", strtoNum(input()));                           "Get number";
setNum("result", 1);                                          "The result";

label("l");
if(gt(getNum("input"), 1), () {                               "If input > 1";
  setNum("result", mult(getNum("result"), getNum("input")));    "result = result * input";
  setNum("input", add(getNum("input"), neg(1)));                "input--";
  goto("l");                                                    "Repeat";
}, () {});

print(numToStr(getNumber("result")));                         "Print result";

Alternatively, golfed:

setNum("i",strtoNum(input()));setNum("r",1);label("l");if(gt(getNum("i"),1),(){setNum("r",mult(getNum("r"),getNum("i")));setNum("i",add(getNum("i"),neg(1)));goto("l");},(){});print(numToStr(getNumber("r")));

gar

Works only with numbers greater than 2.

>
-
:
?
[
:
<
+
]
~
~
[[
*
&
]]
~
!

gar

Gofe

,&t1&s[#sMt-&s#t].

GotoScript

# Get input
1 n := max := INPUT

# Factorial operation
2 n *= GOTOS + 1

# End loop
3 GOTO 5 IF GOTOS >= max - 2

# Continue loop
4 GOTO 2

# Output
5 PRINT n

HGFTSNOA

This program calculates the factorial of the number on the second line.

T
  t 30
TT
t TTTTT ttt T
  TTT ttt TTTTT 0
    0
  TTTTTTT
    ttttttt ttt 1 1
  TTTTTT
TT
ttttttt TTTTT ttt tttt ttttt T
  TTT ttt TTTTT tttt
    TTT 0 TTTTTT
    TTTT tt ttttt tttt 0 0
  TTTTTTT
    TTTT ttttttt ttt tttt TTTTTTTT 1 tt ttttt tttt 0 0
  TTTTTT
TT
tt TTTTT ttt tttt ttttt tttttt T
  TTT ttttt TTTTT tttt
    TTT 0 TTTTTT
    TTTT tttttt
  TTTTTTT
    TTTT tt ttt tttt ttttt TTTTTTTT 1 tttttt TTTTTTTT ttt
  TTTTTT
TT

J--

main{
    echo(factorial(n));
}

Replacing "n" with the number of your choice.

kS

FUNC factorial $to
    1 > $exit
    $to > $i > LOOP
        $exit * $i > $exit
        $i - 1 > $i
        END
    $exit > RETURN

Mao

Classified

Marble Machine

\# o
o\2/
~ g
&~{~
*~ ~
&  1
~  -
&  &
\~o~
 &~x
 1\~
 g ~
~}x.
x  !

MEMORYLEEK

point f; vnput f;
point total; total = 1;

until f == 0;
    point r; r ~> -1; #r; :r;
    total * f; #total; total ~> -1;
    f - 1; #f; f ~> -1;
    point r; r ~> -2; #r; :r; 
back;
point r; r ~> -1; #r; :r;

!total; #total; :total; #f; :f;

Osis

`{*1

Trampolines

|o                 #
|,"Factorial: "    #
|}                 #
|1               \ #
|{H==============H #
|.H              H #
|=H-   };        H #
| \ 0 ^  #       H #
|    >   ]}   1! H #
|     ^ ~  * {  +H #
|      -    -    H #
|                - #

Turing (Iamcalledbob)

 % Accepts a number and calculates its factorial
 fun factorial (n)
      if n = 0
           result 1
      else
           result n * factorial (n - 1)
 
 fun loop()
      get n
      if n < 0
            put "The factorial of ", n, " is ", factorial (n)
            result n
      else
            result loop()
 
 loop()

Upsilon

factorial outvar result, a:
	equal isZero, a, 0.
	subtract x, a, 1.
	if isZero:
		assign result, 1.
	else:
		factorial result, x.
	end.
back.

x(y)

factorial(n)
-> (n < 2){1 n * {n - 1}}

Zull

~

User must wish for a factorial program.