This commit is contained in:
Ingy döt Net 2013-04-10 21:29:02 -07:00
parent 764da6cbbb
commit db842d013d
19005 changed files with 197040 additions and 7 deletions

View file

@ -0,0 +1,11 @@
}:r: Start reader loop.
|~ Read n,
#:end: if n is 0 terminates
>= enqueue it as the initial product, reposition.
}:f: Start factorial loop.
x<:1:x- Decrement n.
{=*> Dequeue product, position n, multiply, update product.
^:f:
{+% Dequeue incidental 0, add to get Y into Z, output fac(n).
<:a:~$ Output a newline.
^:r:

View file

@ -0,0 +1,3 @@
The '''Factorial Function''' of a positive integer, ''n'', is defined as the product of the sequence ''n'', ''n''-1, ''n''-2, ...1 and the factorial of zero, 0, is [[wp:Factorial#Definition|defined]] as being 1.
Write a function to return the factorial of a number. Solutions can be iterative or recursive. Support for trapping negative n errors is optional.

View file

@ -0,0 +1,7 @@
---
category:
- Recursion
- Memoization
- Classic CS problems and programs
- Arithmetic
note: Arithmetic operations

View file

@ -0,0 +1,8 @@
form factorial using iv_val type i.
data: lv_res type i value 1.
do iv_val times.
multiply lv_res by sy-index.
enddo.
iv_val = lv_res.
endform.

View file

@ -0,0 +1,11 @@
form fac_rec using iv_val type i.
data: lv_temp type i.
if iv_val = 0.
iv_val = 1.
else.
lv_temp = iv_val - 1.
perform fac_rec using lv_temp.
multiply iv_val by lv_temp.
endif.
endform.

View file

@ -0,0 +1,5 @@
PROC factorial = (INT upb n)LONG LONG INT:(
LONG LONG INT z := 1;
FOR n TO upb n DO z *:= n OD;
z
); ~

View file

@ -0,0 +1,30 @@
INT g = 7;
[]REAL p = []REAL(0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7)[@0];
PROC complex gamma = (COMPL in z)COMPL: (
# Reflection formula #
COMPL z := in z;
IF re OF z < 0.5 THEN
pi / (complex sin(pi*z)*complex gamma(1-z))
ELSE
z -:= 1;
COMPL x := p[0];
FOR i TO g+1 DO x +:= p[i]/(z+i) OD;
COMPL t := z + g + 0.5;
complex sqrt(2*pi) * t**(z+0.5) * complex exp(-t) * x
FI
);
OP ** = (COMPL z, p)COMPL: ( z=0|0|complex exp(complex ln(z)*p) );
PROC factorial = (COMPL n)COMPL: complex gamma(n+1);
FORMAT compl fmt = $g(-16, 8)"⊥"g(-10, 8)$;
test:(
printf(($q"factorial(-0.5)**2="f(compl fmt)l$, factorial(-0.5)**2));
FOR i TO 9 DO
printf(($q"factorial("d")="f(compl fmt)l$, i, factorial(i)))
OD
)

View file

@ -0,0 +1,7 @@
PROC factorial = (INT n)LONG LONG INT:
CASE n+1 IN
1,1,2,6,24,120,720 # a brief lookup #
OUT
n*factorial(n-1)
ESAC
; ~

View file

@ -0,0 +1,5 @@
function fact_r(n)
{
if ( n <= 1 ) return 1;
return n*fact_r(n-1);
}

View file

@ -0,0 +1,9 @@
function fact(n)
{
if ( n < 1 ) return 1;
r = 1
for(m = 2; m <= n; m++) {
r *= m;
}
return r
}

View file

@ -0,0 +1,11 @@
public static function factorial(n:int):int
{
if (n < 0)
return 0;
var fact:int = 1;
for (var i:int = 1; i <= n; i++)
fact *= i;
return fact;
}

View file

@ -0,0 +1,10 @@
public static function factorial(n:int):int
{
if (n < 0)
return 0;
if (n == 0)
return 1;
return n * factorial(n - 1);
}

View file

@ -0,0 +1,9 @@
function Factorial (N : Positive) return Positive is
Result : Positive := N;
Counter : Natural := N - 1;
begin
for I in reverse 1..Counter loop
Result := Result * I;
end loop;
return Result;
end Factorial;

View file

@ -0,0 +1,8 @@
function Factorial(N : Positive) return Positive is
Result : Positive := 1;
begin
if N > 1 then
Result := N * Factorial(N - 1);
end if;
return Result;
end Factorial;

View file

@ -0,0 +1,62 @@
with Ada.Numerics.Generic_Complex_Types;
with Ada.Numerics.Generic_Complex_Elementary_Functions;
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO.Complex_Io;
with Ada.Text_Io; use Ada.Text_Io;
procedure Factorial_Numeric_Approximation is
type Real is digits 15;
package Complex_Pck is new Ada.Numerics.Generic_Complex_Types(Real);
use Complex_Pck;
package Complex_Io is new Ada.Text_Io.Complex_Io(Complex_Pck);
use Complex_IO;
package Cmplx_Elem_Funcs is new Ada.Numerics.Generic_Complex_Elementary_Functions(Complex_Pck);
use Cmplx_Elem_Funcs;
function Gamma(X : Complex) return Complex is
package Elem_Funcs is new Ada.Numerics.Generic_Elementary_Functions(Real);
use Elem_Funcs;
use Ada.Numerics;
-- Coefficients used by the GNU Scientific Library
G : Natural := 7;
P : constant array (Natural range 0..G + 1) of Real := (
0.99999999999980993, 676.5203681218851, -1259.1392167224028,
771.32342877765313, -176.61502916214059, 12.507343278686905,
-0.13857109526572012, 9.9843695780195716e-6, 1.5056327351493116e-7);
Z : Complex := X;
Cx : Complex;
Ct : Complex;
begin
if Re(Z) < 0.5 then
return Pi / (Sin(Pi * Z) * Gamma(1.0 - Z));
else
Z := Z - 1.0;
Set_Re(Cx, P(0));
Set_Im(Cx, 0.0);
for I in 1..P'Last loop
Cx := Cx + (P(I) / (Z + Real(I)));
end loop;
Ct := Z + Real(G) + 0.5;
return Sqrt(2.0 * Pi) * Ct**(Z + 0.5) * Exp(-Ct) * Cx;
end if;
end Gamma;
function Factorial(N : Complex) return Complex is
begin
return Gamma(N + 1.0);
end Factorial;
Arg : Complex;
begin
Put("factorial(-0.5)**2.0 = ");
Set_Re(Arg, -0.5);
Set_Im(Arg, 0.0);
Put(Item => Factorial(Arg) **2.0, Fore => 1, Aft => 8, Exp => 0);
New_Line;
for I in 0..9 loop
Set_Re(Arg, Real(I));
Set_Im(Arg, 0.0);
Put("factorial(" & Integer'Image(I) & ") = ");
Put(Item => Factorial(Arg), Fore => 6, Aft => 8, Exp => 0);
New_Line;
end loop;
end Factorial_Numeric_Approximation;

View file

@ -0,0 +1,14 @@
integer
factorial(integer n)
{
integer i, result;
result = 1;
i = 1;
while (i < n) {
i += 1;
result *= i;
}
return result;
}

View file

@ -0,0 +1,5 @@
PROC fact(x) IS IF x>=2 THEN x*fact(x-1) ELSE 1
PROC main()
WriteF('5! = \d\n', fact(5))
ENDPROC

View file

@ -0,0 +1,6 @@
PROC fact(x)
DEF r, y
IF x < 2 THEN RETURN 1
r := 1; y := x;
FOR x := 2 TO y DO r := r * x
ENDPROC r

View file

@ -0,0 +1,8 @@
on factorial(x)
if x < 0 then return 0
set R to 1
repeat while x > 1
set {R, x} to {R * x, x - 1}
end repeat
return R
end factorial

View file

@ -0,0 +1,5 @@
on factorial(x)
if x < 0 then return 0
if x > 1 then return x * (my factorial(x - 1))
return 1
end factorial

View file

@ -0,0 +1,9 @@
MsgBox % factorial(4)
factorial(n)
{
result := 1
Loop, % n
result *= A_Index
Return result
}

View file

@ -0,0 +1,6 @@
MsgBox % factorial(4)
factorial(n)
{
return n > 1 ? n-- * factorial(n) : 1
}

View file

@ -0,0 +1,12 @@
;AutoIt Version: 3.2.10.0
MsgBox (0,"Factorial",factorial(6))
Func factorial($int)
If $int < 0 Then
Return 0
EndIf
$fact = 1
For $i = 1 To $int
$fact = $fact * $i
Next
Return $fact
EndFunc

View file

@ -0,0 +1,10 @@
;AutoIt Version: 3.2.10.0
MsgBox (0,"Factorial",factorial(6))
Func factorial($int)
if $int < 0 Then
return 0
Elseif $int == 0 Then
return 1
EndIf
return $int * factorial($int - 1)
EndFunc

View file

@ -0,0 +1,8 @@
FUNCTION factorial (n AS Integer) AS Integer
DIM f AS Integer, i AS Integer
f = 1
FOR i = 2 TO n
f = f*i
NEXT i
factorial = f
END FUNCTION

View file

@ -0,0 +1,7 @@
FUNCTION factorial (n AS Integer) AS Integer
IF n < 2 THEN
factorial = 1
ELSE
factorial = n * factorial(n-1)
END IF
END FUNCTION

View file

@ -0,0 +1,8 @@
function factorial(n)
factorial = 1
if n>0 then
for p=1 to n
factorial *= p
next p
end if
end function

View file

@ -0,0 +1,8 @@
*FLOAT64
@% = &1010
PRINT FNfactorial(18)
END
DEF FNfactorial(n)
IF n <= 1 THEN = 1 ELSE = n * FNfactorial(n-1)

View file

@ -0,0 +1,16 @@
main:
{ argv 0 th $d
fac
%d cr << }
fac!:
{ dup zero?
{ dup one?
{ cp 1 - fac * }
{ zap 1 }
if }
{ zap 1 }
if }
one?! : { 1 = }
zero?!: { 0 = }

View file

@ -0,0 +1,9 @@
@echo off
set /p x=
set /a fs=%x%-1
set y=%x%
FOR /L %%a IN (%fs%, -1, 1) DO SET /a y*=%%a
if %x% EQU 0 set y=1
echo %y%
pause
exit

View file

@ -0,0 +1,3 @@
&1\> :v v *<
^-1:_$>\:|
@.$<

View file

@ -0,0 +1,27 @@
(
=
. !arg:0&1
| !arg
* ( (
= r
. !arg:?r
&
' (
. !arg:0&1
| !arg*(($r)$($r))$(!arg+-1)
)
)
$ (
= r
. !arg:?r
&
' (
. !arg:0&1
| !arg*(($r)$($r))$(!arg+-1)
)
)
)
$ (!arg+-1)
)
$ 10
: 3628800

View file

@ -0,0 +1,4 @@
( (=(r.!arg:?r&'(.!arg:0&1|!arg*(($r)$($r))$(!arg+-1)))):?g
& (!g$!g):?f
& !f$10
)

View file

@ -0,0 +1,7 @@
(factorial=
r
. !arg:?r
& whl
' (!arg:>1&(!arg+-1:?arg)*!r:?r)
& !r
);

View file

@ -0,0 +1,4 @@
>++++++++++>>>+>+[>>>+[-[<<<<<[+<<<<<]>>[[-]>[<<+>+>-]<[>+<-]<[>+<-[>+<-[>
+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>+<-[>[-]>>>>+>+<<<<<<-[>+<-]]]]]]]]]]]>[<+>-
]+>>>>>]<<<<<[<<<<<]>>>>>>>[>>>>>]++[-<<<<<]>>>>>>-]+>>>>>]<[>++<-]<<<<[<[
>+<-]<<<<]>>[->[-]++++++[<++++++++>-]>>>>]<<<<<[<[>+>+<<-]>.<<<<<]>.>>>>]

View file

@ -0,0 +1,3 @@
factorial = { x |
true? x == 0 1 { x * factorial(x - 1)}
}

View file

@ -0,0 +1,2 @@
blsq ) 6?!
720

View file

@ -0,0 +1,12 @@
blsq ) 1 6r@pd
720
blsq ) 1 6r@{?*}r[
720
blsq ) 2 6r@(.*)\/[[1+]e!.*
720
blsq ) 1 6r@p^{.*}5E!
720
blsq ) 6ropd
720
blsq ) 7ro)(.*){0 1 11}die!
720

View file

@ -0,0 +1,8 @@
#include <boost/iterator/counting_iterator.hpp>
#include <algorithm>
int factorial(int n)
{
// last is one-past-end
return std::accumulate(boost::counting_iterator<int>(1), boost::counting_iterator<int>(n+1), 1, std::multiplies<int>());
}

View file

@ -0,0 +1,14 @@
long long int Factorial(long long int m_nValue)
{
long long int result=m_nValue;
long long int result_next;
long long int pc = m_nValue;
do
{
result_next = result*(pc-1);
result = result_next;
pc--;
}while(pc>2);
m_nValue = result;
return m_nValue;
}

View file

@ -0,0 +1,19 @@
template <int N>
struct Factorial
{
enum { value = N * Factorial<N - 1>::value };
};
template <>
struct Factorial<0>
{
enum { value = 1 };
};
// Factorial<4>::value == 24
// Factorial<0>::value == 1
void foo()
{
int x = Factorial<4>::value; // == 24
int y = Factorial<0>::value; // == 1
}

View file

@ -0,0 +1,6 @@
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; ++i)
result *= i;
return result;
}

View file

@ -0,0 +1,3 @@
int factorial(int n) {
return n == 0 ? 1 : n * factorial(n - 1);
}

View file

@ -0,0 +1,7 @@
int fac_aux(int n, int acc) {
return n < 1 ? acc : fac_aux(n - 1, acc * n);
}
int factorial(int n) {
return fac_aux(n, 1);
}

View file

@ -0,0 +1,8 @@
(deffunction factorial (?a)
(if (or (not (integerp ?a)) (< ?a 0)) then
(printout t "Factorial Error!" crlf)
else
(if (= ?a 0) then
1
else
(* ?a (factorial (- ?a 1))))))

View file

@ -0,0 +1,10 @@
function(factorial var n)
set(product 1)
foreach(i RANGE 2 ${n})
math(EXPR product "${product} * ${i}")
endforeach(i)
set(${var} ${product} PARENT_SCOPE)
endfunction(factorial)
factorial(f 12)
message("12! = ${f}")

View file

@ -0,0 +1,2 @@
define rec_fac
{ dup 1 <= [pop 1] [dec rec_fac *] if }

View file

@ -0,0 +1,17 @@
Caramel Factorials.
Only reads one value.
Ingredients.
1 g Caramel
2 g Factorials
Method.
Take Factorials from refrigerator.
Put Caramel into 1st mixing bowl.
Verb the Factorials.
Combine Factorials into 1st mixing bowl.
Verb Factorials until verbed.
Pour contents of the 1st mixing bowl into the 1st baking dish.
Serves 1.

View file

@ -0,0 +1,14 @@
factorialRec(n) {
if (n == 0) return 1;
return n * factorialRec(n - 1);
}
factorialIter(n) {
for (i in range(1, n))
n *= i;
return n;
}
factorialFold(n) {
return reduce(multiply, 1, range(1, n + 1));
}

View file

@ -0,0 +1,2 @@
[n|n > 0] factorialStatic(static n) = n * factorialStatic(static n - 1);
overload factorialStatic(static 0) = 1;

View file

@ -0,0 +1,4 @@
[N|Integer?(N)] factorial(n: N) {
if (n == 0) return N(1);
return n * factorial(n - 1);
}

View file

@ -0,0 +1,7 @@
main() {
println(factorialRec(5)); // 120
println(factorialIter(5)); // 120
println(factorialFold(5)); // 120
println(factorialStatic(static 5)); // 120
println(factorial(Int64(20))); // 2432902008176640000
}

View file

@ -0,0 +1,2 @@
(defn factorial [x]
(apply * (range 2 (inc x))))

View file

@ -0,0 +1,4 @@
(defn factorial [x]
(if (< x 2)
1
(* x (factorial (dec x)))))

View file

@ -0,0 +1,6 @@
(defn factorial [x]
(loop [x x
acc 1]
(if (< x 2)
acc
(recur (dec x) (* acc x)))))

View file

@ -0,0 +1,5 @@
fac = (n) ->
if n <= 1
1
else
n * fac n-1

View file

@ -0,0 +1,2 @@
fac = (n) ->
[1..n].reduce (x,y) -> x*y

View file

@ -0,0 +1,4 @@
(defun fact (n)
(if (< n 2)
1
(* n (fact(- n 1)))))

View file

@ -0,0 +1,5 @@
(defun factorial (n)
"Calculates N!"
(loop for result = 1 then (* result i)
for i from 2 to n
finally (return result)))

View file

@ -0,0 +1,2 @@
(defun factorial (n)
(reduce #'* (loop for i from 1 to n collect i)))

View file

@ -0,0 +1,47 @@
import std.stdio, std.algorithm, std.metastrings, std.range;
// iterative
int factorial(int n) {
int result = 1;
foreach (i; 1 .. n + 1)
result *= i;
return result;
}
// recursive
int recFactorial(int n) {
if (n == 0)
return 1;
else
return n * recFactorial(n - 1);
}
// functional-style
int fact(int n) {
return iota(1, n + 1).reduce!q{a * b}();
}
// tail recursive (at run-time, with DMD)
int tfactorial(int n) {
static int facAux(int n, int acc) {
if (n < 1)
return acc;
else
return facAux(n - 1, acc * n);
}
return facAux(n, 1);
}
// computed and printed at compile-time
pragma(msg, toStringNow!(factorial(15)));
pragma(msg, toStringNow!(recFactorial(15)));
pragma(msg, toStringNow!(fact(15)));
pragma(msg, toStringNow!(tfactorial(15)));
void main() {
// computed and printed at run-time
writeln(factorial(15));
writeln(recFactorial(15));
writeln(fact(15));
writeln(tfactorial(15));
}

View file

@ -0,0 +1,8 @@
function IterativeFactorial(n : Integer) : Integer;
var
i : Integer;
begin
Result := 1;
for i := 2 to n do
Result *= i;
end;

View file

@ -0,0 +1,6 @@
function RecursiveFactorial(n : Integer) : Integer;
begin
if n>1 then
Result := RecursiveFactorial(n-1)*n
else Result := 1;
end;

View file

@ -0,0 +1,11 @@
int fact(int n) {
if(n<0) {
throw new IllegalArgumentException('Argument less than 0');
}
return n==0 ? 1 : n*fact(n-1);
}
main() {
print(fact(10));
print(fact(-1));
}

View file

@ -0,0 +1,15 @@
int fact(int n) {
if(n<0) {
throw new IllegalArgumentException('Argument less than 0');
}
int res=1;
for(int i=1;i<=n;i++) {
res*=i;
}
return res;
}
main() {
print(fact(10));
print(fact(-1));
}

View file

@ -0,0 +1,16 @@
program Factorial1;
{$APPTYPE CONSOLE}
function FactorialIterative(aNumber: Integer): Int64;
var
i: Integer;
begin
Result := 1;
for i := 1 to aNumber do
Result := i * Result;
end;
begin
Writeln('5! = ', FactorialIterative(5));
end.

View file

@ -0,0 +1,15 @@
program Factorial2;
{$APPTYPE CONSOLE}
function FactorialRecursive(aNumber: Integer): Int64;
begin
if aNumber < 1 then
Result := 1
else
Result := aNumber * FactorialRecursive(aNumber - 1);
end;
begin
Writeln('5! = ', FactorialRecursive(5));
end.

View file

@ -0,0 +1,24 @@
program Factorial3;
{$APPTYPE CONSOLE}
function FactorialTailRecursive(aNumber: Integer): Int64;
function FactorialHelper(aNumber: Integer; aAccumulator: Int64): Int64;
begin
if aNumber = 0 then
Result := aAccumulator
else
Result := FactorialHelper(aNumber - 1, aNumber * aAccumulator);
end;
begin
if aNumber < 1 then
Result := 1
else
Result := FactorialHelper(aNumber, 1);
end;
begin
Writeln('5! = ', FactorialTailRecursive(5));
end.

View file

@ -0,0 +1,3 @@
define method factorial(n)
reduce1(\*, range(from: 1, to: n));
end

View file

@ -0,0 +1,4 @@
pragma.enable("accumulator")
def factorial(n) {
return accum 1 for i in 2..n { _ * i }
}

View file

@ -0,0 +1,11 @@
function fact(n int in) returns (bigint)
if (n < 0)
writestdout("No negative numbers");
return (0);
end
ans bigint = 1;
for (i int from 1 to n)
ans *= i;
end
return (ans);
end

View file

@ -0,0 +1,11 @@
function fact(n int in) returns (bigint)
if (n < 0)
SysLib.writeStdout("No negative numbers");
return (0);
end
if (n < 2)
return (1);
else
return (n * fact(n - 1));
end
end

View file

@ -0,0 +1,3 @@
fact = fact' 1L
where fact' acc 0 = acc
fact' acc n = fact' (n * acc) (n - 1)

View file

@ -0,0 +1,6 @@
(defun fact (n)
"n is an integer, this function returns n!, that is n * (n - 1)
* (n - 2)....* 4 * 3 * 2 * 1"
(cond
((= n 1) 1)
(t (* n (fact (1- n))))))

View file

@ -0,0 +1,4 @@
(require 'calc)
(calc-eval "fact(30)")
=>
"265252859812191058636308480000000"

View file

@ -0,0 +1 @@
lists:foldl(fun(X,Y) -> X*Y end, 1, lists:seq(1,N)).

View file

@ -0,0 +1,2 @@
fac(1) -> 1;
fac(N) -> N * fac(N-1).

View file

@ -0,0 +1,3 @@
fac(N) -> fac(N-1,N).
fac(1,N) -> N;
fac(I,N) -> fac(I-1,N*I).

View file

@ -0,0 +1,9 @@
function factorial(integer n)
atom f = 1
while n > 1 do
f *= n
n -= 1
end while
return f
end function

View file

@ -0,0 +1,7 @@
function factorial(integer n)
if n > 1 then
return factorial(n-1) * n
else
return 1
end if
end function

View file

@ -0,0 +1,7 @@
function factorial(integer n, integer acc = 1)
if n <= 0 then
return acc
else
return factorial(n-1, n*acc)
end if
end function

View file

@ -0,0 +1,106 @@
include std/mathcons.e
enum MUL_LLL,
TESTEQ_LIL,
TESTLT_LIL,
TRUEGO_LL,
MOVE_LL,
INCR_L,
TESTGT_LLL,
GOTO_L,
OUT_LI,
OUT_II,
STOP
global sequence tape = {
1,
1,
0,
0,
0,
{TESTLT_LIL, 5, 0, 4},
{TRUEGO_LL, 4, 22},
{TESTEQ_LIL, 5, 0, 4},
{TRUEGO_LL, 4, 20},
{MUL_LLL, 1, 2, 3},
{TESTEQ_LIL, 3, PINF, 4},
{TRUEGO_LL, 4, 18},
{MOVE_LL, 3, 1},
{INCR_L, 2},
{TESTGT_LLL, 2, 5, 4 },
{TRUEGO_LL, 4, 18},
{GOTO_L, 10},
{OUT_LI, 3, "%.0f\n"},
{STOP},
{OUT_II, 1, "%.0f\n"},
{STOP},
{OUT_II, "Negative argument", "%s\n"},
{STOP}
}
global integer ip = 1
procedure eval( sequence cmd )
atom i = 1
while i <= length( cmd ) do
switch cmd[ i ] do
case MUL_LLL then -- multiply location location giving location
tape[ cmd[ i + 3 ] ] = tape[ cmd[ i + 1 ] ] * tape[ cmd[ i + 2 ] ]
i += 3
case TESTEQ_LIL then -- test if location eq value giving location
tape[ cmd[ i + 3 ]] = ( tape[ cmd[ i + 1 ] ] = cmd[ i + 2 ] )
i += 3
case TESTLT_LIL then -- test if location eq value giving location
tape[ cmd[ i + 3 ]] = ( tape[ cmd[ i + 1 ] ] < cmd[ i + 2 ] )
i += 3
case TRUEGO_LL then -- if true in location, goto location
if tape[ cmd[ i + 1 ] ] then
ip = cmd[ i + 2 ] - 1
end if
i += 2
case MOVE_LL then -- move value at location to location
tape[ cmd[ i + 2 ] ] = tape[ cmd[ i + 1 ] ]
i += 2
case INCR_L then -- increment value at location
tape[ cmd[ i + 1 ] ] += 1
i += 1
case TESTGT_LLL then -- test if location gt location giving location
tape[ cmd[ i + 3 ]] = ( tape[ cmd[ i + 1 ] ] > tape[ cmd[ i + 2 ] ] )
i += 3
case GOTO_L then -- goto location
ip = cmd[ i + 1 ] - 1
i += 1
case OUT_LI then -- output location using format
printf( 1, cmd[ i + 2], tape[ cmd[ i + 1 ] ] )
i += 2
case OUT_II then -- output immediate using format
if sequence( cmd[ i + 1 ] ) then
printf( 1, cmd[ i + 2], { cmd[ i + 1 ] } )
else
printf( 1, cmd[ i + 2], cmd[ i + 1 ] )
end if
i += 2
case STOP then -- stop
abort(0)
end switch
i += 1
end while
end procedure
include std/convert.e
sequence cmd = command_line()
if length( cmd ) > 2 then
puts( 1, cmd[ 3 ] & "! = " )
tape[ 5 ] = to_number(cmd[3])
else
puts( 1, "eui fact.ex <number>\n" )
abort(1)
end if
while 1 do
if sequence( tape[ ip ] ) then
eval( tape[ ip ] )
end if
ip += 1
end while

View file

@ -0,0 +1,2 @@
[1\[$][$@*\1-]#%]f:
^'0- f;!.

View file

@ -0,0 +1 @@
[$1=~[$1-f;!*]?]f:

View file

@ -0,0 +1,3 @@
USING: math.ranges sequences ;
: factorial ( n -- n ) [1,b] product ;

View file

@ -0,0 +1,10 @@
def class Number {
def factorial {
1 upto: self . product
}
}
# print first ten factorials
1 upto: 10 do_each: |i| {
i to_s ++ "! = " ++ (i factorial) println
}

View file

@ -0,0 +1,36 @@
class Main
{
static Int factorialRecursive (Int n)
{
if (n <= 1)
return 1
else
return n * (factorialRecursive (n - 1))
}
static Int factorialIterative (Int n)
{
Int product := 1
for (Int i := 2; i <=n ; ++i)
{
product *= i
}
return product
}
static Int factorialFunctional (Int n)
{
(1..n).toList.reduce(1) |a,v|
{
v->mult(a) // use a dynamic invoke
// alternatively, cast a: v * (Int)a
}
}
public static Void main ()
{
echo (factorialRecursive(20))
echo (factorialIterative(20))
echo (factorialFunctional(20))
}
}

View file

@ -0,0 +1 @@
: fac ( n -- n! ) 1 swap 1+ 1 ?do i * loop ;

View file

@ -0,0 +1 @@
nfactorial = PRODUCT((/(i, i=1,n)/))

View file

@ -0,0 +1,6 @@
FUNCTION FACT(N)
INTEGER N,I,FACT
FACT=1
DO 10 I=1,N
10 FACT=FACT*I
END

View file

@ -0,0 +1,5 @@
# Built-in
Factorial(5);
# An implementation
fact := n -> Product([1 .. n]);

View file

@ -0,0 +1,5 @@
n = argument0
j = 1
for(i = 1; i <= n; i += 1)
j *= i
return j

View file

@ -0,0 +1,4 @@
def factorial (n)
if (< n 2) 1
* n
factorial (- n 1)

View file

@ -0,0 +1,22 @@
package main
import (
"fmt"
"math/big"
)
func main() {
fmt.Println(factorial(800))
}
func factorial(n int64) *big.Int {
if n < 0 {
return nil
}
r := big.NewInt(1)
var f big.Int
for i := int64(2); i <= n; i++ {
r.Mul(r, f.SetInt64(i))
}
return r
}

View file

@ -0,0 +1,15 @@
package main
import (
"math/big"
"fmt"
)
func factorial(n int64) *big.Int {
var z big.Int
return z.MulRange(1, n)
}
func main() {
fmt.Println(factorial(800))
}

View file

@ -0,0 +1,2 @@
{.!{1}{,{)}%{*}*}if}:fact;
5fact puts # test

View file

@ -0,0 +1 @@
{),(;{*}*}:fact;

View file

@ -0,0 +1 @@
{.1<{;1}{.(fact*}if}:fact;

View file

@ -0,0 +1,2 @@
def rFact
rFact = { (it > 1) ? it * rFact(it - 1) : 1 }

View file

@ -0,0 +1 @@
(0..6).each { println "${it}: ${rFact(it)}" }

Some files were not shown because too many files have changed in this diff Show more