Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
2
Task/Factorial/8th/factorial.8th
Normal file
2
Task/Factorial/8th/factorial.8th
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
: factorial \ n -- n!
|
||||
1 swap ' n:* 1 rot loop ;
|
||||
9
Task/Factorial/Ada/factorial-1.adb
Normal file
9
Task/Factorial/Ada/factorial-1.adb
Normal 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;
|
||||
8
Task/Factorial/Ada/factorial-2.adb
Normal file
8
Task/Factorial/Ada/factorial-2.adb
Normal 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;
|
||||
62
Task/Factorial/Ada/factorial-3.adb
Normal file
62
Task/Factorial/Ada/factorial-3.adb
Normal 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;
|
||||
13
Task/Factorial/ArkScript/factorial.ark
Normal file
13
Task/Factorial/ArkScript/factorial.ark
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
(let factorial (fun (n) {
|
||||
(mut result 1)
|
||||
(mut i 2)
|
||||
(while (< i n) {
|
||||
(set result (* result i))
|
||||
(set i (+ 1 i)) })
|
||||
result }))
|
||||
|
||||
(import std.List)
|
||||
|
||||
(list:forEach
|
||||
(list:iota 0 8)
|
||||
(fun (n) (print n "\t" (factorial n))))
|
||||
12
Task/Factorial/AutoIt/factorial-1.au3
Normal file
12
Task/Factorial/AutoIt/factorial-1.au3
Normal 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
|
||||
10
Task/Factorial/AutoIt/factorial-2.au3
Normal file
10
Task/Factorial/AutoIt/factorial-2.au3
Normal 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
|
||||
1
Task/Factorial/COBOL/factorial-1.cob
Normal file
1
Task/Factorial/COBOL/factorial-1.cob
Normal file
|
|
@ -0,0 +1 @@
|
|||
MOVE FUNCTION FACTORIAL(num) TO result
|
||||
21
Task/Factorial/COBOL/factorial-2.cob
Normal file
21
Task/Factorial/COBOL/factorial-2.cob
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
FUNCTION-ID. factorial_iterative.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i PIC 9(38).
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 n PIC 9(38).
|
||||
01 ret PIC 9(38).
|
||||
|
||||
PROCEDURE DIVISION USING BY VALUE n RETURNING ret.
|
||||
MOVE 1 TO ret
|
||||
|
||||
PERFORM VARYING i FROM 2 BY 1 UNTIL n < i
|
||||
MULTIPLY i BY ret
|
||||
END-PERFORM
|
||||
|
||||
GOBACK.
|
||||
|
||||
END FUNCTION factorial_iterative.
|
||||
22
Task/Factorial/COBOL/factorial-3.cob
Normal file
22
Task/Factorial/COBOL/factorial-3.cob
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
FUNCTION-ID. factorial_recursive.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 prev-n PIC 9(38).
|
||||
|
||||
LINKAGE SECTION.
|
||||
01 n PIC 9(38).
|
||||
01 ret PIC 9(38).
|
||||
|
||||
PROCEDURE DIVISION USING BY VALUE n RETURNING ret.
|
||||
IF n = 0
|
||||
MOVE 1 TO ret
|
||||
ELSE
|
||||
SUBTRACT 1 FROM n GIVING prev-n
|
||||
MULTIPLY n BY factorial_recursive(prev-n) GIVING ret
|
||||
END-IF
|
||||
|
||||
GOBACK.
|
||||
|
||||
END FUNCTION factorial_recursive.
|
||||
34
Task/Factorial/COBOL/factorial-4.cob
Normal file
34
Task/Factorial/COBOL/factorial-4.cob
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. factorial_test.
|
||||
|
||||
ENVIRONMENT DIVISION.
|
||||
CONFIGURATION SECTION.
|
||||
REPOSITORY.
|
||||
FUNCTION factorial_iterative
|
||||
FUNCTION factorial_recursive.
|
||||
|
||||
DATA DIVISION.
|
||||
LOCAL-STORAGE SECTION.
|
||||
01 i PIC 9(38).
|
||||
|
||||
PROCEDURE DIVISION.
|
||||
DISPLAY
|
||||
"i = "
|
||||
WITH NO ADVANCING
|
||||
END-DISPLAY.
|
||||
ACCEPT i END-ACCEPT.
|
||||
DISPLAY SPACE END-DISPLAY.
|
||||
|
||||
DISPLAY
|
||||
"factorial_iterative(i) = "
|
||||
factorial_iterative(i)
|
||||
END-DISPLAY.
|
||||
|
||||
DISPLAY
|
||||
"factorial_recursive(i) = "
|
||||
factorial_recursive(i)
|
||||
END-DISPLAY.
|
||||
|
||||
GOBACK.
|
||||
|
||||
END PROGRAM factorial_test.
|
||||
7
Task/Factorial/Chapel/factorial.chpl
Normal file
7
Task/Factorial/Chapel/factorial.chpl
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
proc fac(n) {
|
||||
var r = 1;
|
||||
for i in 1..n do
|
||||
r *= i;
|
||||
|
||||
return r;
|
||||
}
|
||||
11
Task/Factorial/Emacs-Lisp/factorial-1.el
Normal file
11
Task/Factorial/Emacs-Lisp/factorial-1.el
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
;; Functional (most elegant and best suited to Lisp dialects):
|
||||
(defun fact (n)
|
||||
"Return the factorial of integer N, which require to be positive or 0."
|
||||
;; Elisp won't do any type checking automatically, so
|
||||
;; good practice would be doing that ourselves:
|
||||
(if (not (and (integerp n) (>= n 0)))
|
||||
(error "Function fact (N): Not a natural number or 0: %S" n))
|
||||
;; But the actual code is very short:
|
||||
(apply '* (number-sequence 1 n)))
|
||||
;; (For N = 0, number-sequence returns the empty list, resp. nil,
|
||||
;; and the * function works with zero arguments, returning 1.)
|
||||
8
Task/Factorial/Emacs-Lisp/factorial-2.el
Normal file
8
Task/Factorial/Emacs-Lisp/factorial-2.el
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
;; Recursive:
|
||||
(defun fact (n)
|
||||
"Return the factorial of integer N, which require to be positive or 0."
|
||||
(if (not (and (integerp n) (>= n 0))) ; see above
|
||||
(error "Function fact (N): Not a natural number or 0: %S" n))
|
||||
(cond ; (or use an (if ...) with an else part)
|
||||
((or (= n 0) (= n 1)) 1)
|
||||
(t (* n (fact (1- n))))))
|
||||
4
Task/Factorial/Emacs-Lisp/factorial-3.el
Normal file
4
Task/Factorial/Emacs-Lisp/factorial-3.el
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
(require 'calc)
|
||||
(calc-eval "fact(30)")
|
||||
=>
|
||||
"265252859812191058636308480000000"
|
||||
9
Task/Factorial/Euphoria/factorial-1.eu
Normal file
9
Task/Factorial/Euphoria/factorial-1.eu
Normal 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
|
||||
7
Task/Factorial/Euphoria/factorial-2.eu
Normal file
7
Task/Factorial/Euphoria/factorial-2.eu
Normal 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
|
||||
7
Task/Factorial/Euphoria/factorial-3.eu
Normal file
7
Task/Factorial/Euphoria/factorial-3.eu
Normal 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
|
||||
106
Task/Factorial/Euphoria/factorial-4.eu
Normal file
106
Task/Factorial/Euphoria/factorial-4.eu
Normal 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
|
||||
1
Task/Factorial/Gleam/factorial-1.gleam
Normal file
1
Task/Factorial/Gleam/factorial-1.gleam
Normal file
|
|
@ -0,0 +1 @@
|
|||
list.fold(int.range(1, n + 1, [], list.prepend), 1, int.multiply)
|
||||
6
Task/Factorial/Gleam/factorial-2.gleam
Normal file
6
Task/Factorial/Gleam/factorial-2.gleam
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
pub fn fac(n: Int) -> Int {
|
||||
case n {
|
||||
1 -> 1
|
||||
n -> n * fac(n - 1)
|
||||
}
|
||||
}
|
||||
10
Task/Factorial/Gleam/factorial-3.gleam
Normal file
10
Task/Factorial/Gleam/factorial-3.gleam
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
pub fn fac(n: Int) -> Int {
|
||||
fac_helper(n, n - 1)
|
||||
}
|
||||
|
||||
fn fac_helper(n: Int, acc: Int) -> Int {
|
||||
case n, acc {
|
||||
n, 1 -> n
|
||||
n, i -> fac_helper(n * i, i - 1)
|
||||
}
|
||||
}
|
||||
6
Task/Factorial/Haxe/factorial-1.hx
Normal file
6
Task/Factorial/Haxe/factorial-1.hx
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
static function factorial(n:Int):Int {
|
||||
var result = 1;
|
||||
while (1<n)
|
||||
result *= n--;
|
||||
return result;
|
||||
}
|
||||
3
Task/Factorial/Haxe/factorial-2.hx
Normal file
3
Task/Factorial/Haxe/factorial-2.hx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
static function factorial(n:Int):Int {
|
||||
return n == 0 ? 1 : n * factorial2(n - 1);
|
||||
}
|
||||
7
Task/Factorial/Haxe/factorial-3.hx
Normal file
7
Task/Factorial/Haxe/factorial-3.hx
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
inline static function _fac_aux(n, acc:Int):Int {
|
||||
return n < 1 ? acc : _fac_aux(n - 1, acc * n);
|
||||
}
|
||||
|
||||
static function factorial(n:Int):Int {
|
||||
return _fac_aux(n,1);
|
||||
}
|
||||
3
Task/Factorial/Haxe/factorial-4.hx
Normal file
3
Task/Factorial/Haxe/factorial-4.hx
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
static function factorial(n:Int):Int {
|
||||
return [for (i in 1...(n+1)) i].fold(function(num, total) return total *= num, 1);
|
||||
}
|
||||
58
Task/Factorial/Haxe/factorial-5.hx
Normal file
58
Task/Factorial/Haxe/factorial-5.hx
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
using StringTools;
|
||||
using Lambda;
|
||||
|
||||
class Factorial {
|
||||
// iterative
|
||||
static function factorial1(n:Int):Int {
|
||||
var result = 1;
|
||||
while (1<n)
|
||||
result *= n--;
|
||||
return result;
|
||||
}
|
||||
|
||||
// recursive
|
||||
static function factorial2(n:Int):Int {
|
||||
return n == 0 ? 1 : n * factorial2(n - 1);
|
||||
}
|
||||
|
||||
// tail-recursive
|
||||
inline static function _fac_aux(n, acc:Int):Int {
|
||||
return n < 1 ? acc : _fac_aux(n - 1, acc * n);
|
||||
}
|
||||
|
||||
static function factorial3(n:Int):Int {
|
||||
return _fac_aux(n,1);
|
||||
}
|
||||
|
||||
// functional
|
||||
static function factorial4(n:Int):Int {
|
||||
return [for (i in 1...(n+1)) i].fold(function(num, total) return total *= num, 1);
|
||||
}
|
||||
|
||||
static function main() {
|
||||
var v = 12;
|
||||
// iterative
|
||||
var start = haxe.Timer.stamp();
|
||||
var result = factorial1(v);
|
||||
var duration = haxe.Timer.stamp() - start;
|
||||
Sys.println('iterative'.rpad(' ', 20) + 'result: $result time: $duration ms');
|
||||
|
||||
// recursive
|
||||
start = haxe.Timer.stamp();
|
||||
result = factorial2(v);
|
||||
duration = haxe.Timer.stamp() - start;
|
||||
Sys.println('recursive'.rpad(' ', 20) + 'result: $result time: $duration ms');
|
||||
|
||||
// tail-recursive
|
||||
start = haxe.Timer.stamp();
|
||||
result = factorial3(v);
|
||||
duration = haxe.Timer.stamp() - start;
|
||||
Sys.println('tail-recursive'.rpad(' ', 20) + 'result: $result time: $duration ms');
|
||||
|
||||
// functional
|
||||
start = haxe.Timer.stamp();
|
||||
result = factorial4(v);
|
||||
duration = haxe.Timer.stamp() - start;
|
||||
Sys.println('functional'.rpad(' ', 20) + 'result: $result time: $duration ms');
|
||||
}
|
||||
}
|
||||
|
|
@ -1,2 +1,2 @@
|
|||
val factorial = fn n: fold(2 .. n, by=fn{*})
|
||||
val factorial = fn(n number) { fold(2 .. n, by=fn{*}) }
|
||||
writeln factorial(7)
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
val factorial = fn x: if(x < 2: 1; x * fn((x - 1)))
|
||||
val factorial = fn(x number) { if(x < 2: 1; x * fn((x - 1))) }
|
||||
writeln factorial(7)
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
val factorial = fn(i) {
|
||||
val factorial = fn(i number) {
|
||||
var answer = 1
|
||||
for x in 2 .. i {
|
||||
answer *= x
|
||||
|
|
|
|||
|
|
@ -1,2 +1,2 @@
|
|||
val factorial = fn n: for[=1] x in n { _for *= x }
|
||||
val factorial = fn(n number) { for[=1] x in n { _for *= x } }
|
||||
writeln factorial(7)
|
||||
|
|
|
|||
6
Task/Factorial/PowerShell/factorial-1.ps1
Normal file
6
Task/Factorial/PowerShell/factorial-1.ps1
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
function Get-Factorial ($x) {
|
||||
if ($x -eq 0) {
|
||||
return 1
|
||||
}
|
||||
return $x * (Get-Factorial ($x - 1))
|
||||
}
|
||||
9
Task/Factorial/PowerShell/factorial-2.ps1
Normal file
9
Task/Factorial/PowerShell/factorial-2.ps1
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function Get-Factorial ($x) {
|
||||
if ($x -eq 0) {
|
||||
return 1
|
||||
} else {
|
||||
$product = 1
|
||||
1..$x | ForEach-Object { $product *= $_ }
|
||||
return $product
|
||||
}
|
||||
}
|
||||
6
Task/Factorial/PowerShell/factorial-3.ps1
Normal file
6
Task/Factorial/PowerShell/factorial-3.ps1
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
function Get-Factorial ($x) {
|
||||
if ($x -eq 0) {
|
||||
return 1
|
||||
}
|
||||
return (Invoke-Expression (1..$x -join '*'))
|
||||
}
|
||||
1
Task/Factorial/R/factorial-4.r
Normal file
1
Task/Factorial/R/factorial-4.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
fact_fp <- function(n) ifelse(!n, 1, Reduce(`*`, seq_len(n)))
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
-- 23 Aug 2025
|
||||
-- 10 Mar 2026
|
||||
include Setting
|
||||
|
||||
say 'Factorial'
|
||||
|
|
@ -10,7 +10,7 @@ if pos('Regina',version) > 0 then
|
|||
call Recursive 10, '1 13 71 450 3249 25206 205022'
|
||||
else
|
||||
call Recursive 10, '1 13 71 450 3249'
|
||||
call Imperative 1E5,'1 13 71 450 3249 25206'
|
||||
call Imperative 100000,'1 13 71 450 3249 25206'
|
||||
call Timer
|
||||
exit
|
||||
|
||||
|
|
@ -24,10 +24,10 @@ say
|
|||
return
|
||||
|
||||
Imperative:
|
||||
call ResetMemo
|
||||
Memo.=''
|
||||
call Time('R')
|
||||
arg d,p
|
||||
numeric digits d; Fact. = 0
|
||||
numeric digits d
|
||||
say 'Imperative in' d 'digits precision...'
|
||||
do i = 1 to Words(p)
|
||||
call Time('r'); f = Word(p,i); h = Fact(f)
|
||||
|
|
@ -41,10 +41,10 @@ say
|
|||
return
|
||||
|
||||
Recursive:
|
||||
call ResetMemo
|
||||
Memo.=''
|
||||
call Time('R')
|
||||
arg d,p
|
||||
numeric digits d; Fact. = 0
|
||||
numeric digits d
|
||||
say 'Recursive in' d 'digits precision...'
|
||||
do i = 1 to Words(p)
|
||||
call Time('r'); f = Word(p,i); h = Recurs(f)
|
||||
|
|
|
|||
131
Task/Factorial/RISC-V-Assembly/factorial.asm
Normal file
131
Task/Factorial/RISC-V-Assembly/factorial.asm
Normal file
|
|
@ -0,0 +1,131 @@
|
|||
# riscv assembly raspberry pico2 rp2350
|
||||
# program factorial.s
|
||||
# connexion putty com3
|
||||
/*********************************************/
|
||||
/* CONSTANTES */
|
||||
/********************************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../constantesRiscv.inc"
|
||||
|
||||
/****************************************************/
|
||||
/* MACROS */
|
||||
/****************************************************/
|
||||
#.include "../ficmacrosriscv.inc" # for debugging only
|
||||
|
||||
/*******************************************/
|
||||
/* INITIALED DATAS */
|
||||
/*******************************************/
|
||||
.data
|
||||
szMessStart: .asciz "Program riscv start.\r\n"
|
||||
szMessEnd: .asciz "\nProgram end OK.\r\n"
|
||||
szCariageReturn: .asciz "\r\n"
|
||||
|
||||
szMessLargeNumber: .asciz "Number N to large. \n"
|
||||
szMessNegNumber: .asciz "Number N is negative. \n"
|
||||
|
||||
szMessResult: .ascii "Resultat = " # message result
|
||||
|
||||
.align 2
|
||||
|
||||
|
||||
/*******************************************/
|
||||
/* UNINITIALED DATA */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 2
|
||||
sConvArea: .skip 24
|
||||
|
||||
/********************************...-..--*****/
|
||||
/* SECTION CODE */
|
||||
/**********************************************/
|
||||
.text
|
||||
.global main
|
||||
|
||||
main:
|
||||
call stdio_init_all # général init
|
||||
1: # start loop connexion
|
||||
li a0,0 # raz argument register
|
||||
call tud_cdc_n_connected # waiting for USB connection
|
||||
beqz a0,1b # return code = zero ?
|
||||
|
||||
la a0,szMessStart # message address
|
||||
call writeString # display message
|
||||
|
||||
li a0,-10
|
||||
call factorial #
|
||||
li a0,0
|
||||
call factorial #
|
||||
li a0,10
|
||||
call factorial #
|
||||
|
||||
la a0,szMessEnd
|
||||
call writeString
|
||||
call getchar
|
||||
100: # final loop
|
||||
j 100b
|
||||
/**********************************************/
|
||||
/* compute factorial */
|
||||
/**********************************************/
|
||||
/* a0 value */
|
||||
factorial: # INFO: factorial
|
||||
addi sp, sp, -4 # reserve stack
|
||||
sw ra, 0(sp)
|
||||
bltz a0,99f # is negative ?
|
||||
li t0,1
|
||||
ble a0,t0,1f # is 0 or 1
|
||||
call computeFactorial
|
||||
li t0,-1 # error overflow ?
|
||||
beq a0,t0,98f
|
||||
1: # display result
|
||||
la a1,sConvArea
|
||||
call conversion10
|
||||
la a0,szMessResult
|
||||
call writeString
|
||||
la a0,sConvArea
|
||||
call writeString
|
||||
la a0,szCariageReturn
|
||||
call writeString
|
||||
j 100f
|
||||
98:
|
||||
la a0,szMessLargeNumber
|
||||
call writeString
|
||||
j 100f
|
||||
99:
|
||||
la a0,szMessNegNumber
|
||||
call writeString
|
||||
j 100f
|
||||
100:
|
||||
lw ra, 0(sp)
|
||||
addi sp, sp, 4
|
||||
ret
|
||||
/**********************************************/
|
||||
/* compute factorial */
|
||||
/**********************************************/
|
||||
/* a0 value */
|
||||
computeFactorial: # INFO:computeFactorial
|
||||
addi sp,sp, -8 # reserve stack
|
||||
sw ra,0(sp)
|
||||
sw s0,4(sp)
|
||||
li t0,1
|
||||
beq a0,t0,100f # if n = 1 return
|
||||
mv s0,a0
|
||||
addi a0,a0,-1
|
||||
call computeFactorial
|
||||
li t0,-1 # error overflow ?
|
||||
beq a0,t0,100f
|
||||
mulh t0,a0,s0 # compute half high 32 bits
|
||||
beqz t0,1f # is ok if result is zero
|
||||
li a0,-1 # error overflow
|
||||
j 100f
|
||||
1:
|
||||
mul a0,a0,s0 # multiplication
|
||||
100:
|
||||
lw ra,0(sp)
|
||||
lw s0,4(sp)
|
||||
addi sp,sp, 8
|
||||
ret
|
||||
/************************************/
|
||||
/* file include Fonctions */
|
||||
/***********************************/
|
||||
/* for this file see risc-v task include a file */
|
||||
.include "../../includeFunctions.s"
|
||||
56
Task/Factorial/Rebol/factorial.rebol
Normal file
56
Task/Factorial/Rebol/factorial.rebol
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
Rebol [
|
||||
Title: "Factorial"
|
||||
URL: http://rosettacode.org/wiki/Factorial_function
|
||||
]
|
||||
|
||||
;; Standard recursive implementation.
|
||||
factorial: func [n][
|
||||
either n > 1 [n * factorial n - 1] [1]
|
||||
]
|
||||
|
||||
;; Iteration.
|
||||
ifactorial: func [n][
|
||||
f: 1
|
||||
for i 2 n 1 [f: f * i]
|
||||
f
|
||||
]
|
||||
|
||||
either system/version < 3.0.0 [
|
||||
; Automatic memoization.
|
||||
; I'm just going to say up front that this is a stunt. However, you've
|
||||
; got to admit it's pretty nifty. Note that the 'memo' function
|
||||
; works with an unlimited number of arguments (although the expected
|
||||
; gains decrease as the argument count increases).
|
||||
memo: func [
|
||||
"Defines memoizing function -- keeps arguments/results for later use."
|
||||
args [block!] "Function arguments. Just specify variable names."
|
||||
body [block!] "The body block of the function."
|
||||
/local m-args m-r
|
||||
][
|
||||
do compose/deep [
|
||||
func [
|
||||
(args)
|
||||
/dump "Dump memory."
|
||||
][
|
||||
m-args: []
|
||||
if dump [return m-args]
|
||||
|
||||
if m-r: select/only m-args reduce [(args)] [return m-r]
|
||||
|
||||
m-r: do [(body)]
|
||||
append m-args reduce [reduce [(args)] m-r]
|
||||
m-r
|
||||
]
|
||||
]
|
||||
]
|
||||
|
||||
mfactorial: memo [n][
|
||||
either n > 1 [n * mfactorial n - 1] [1]
|
||||
]
|
||||
][
|
||||
;; Above hack is not supported in Rebol3
|
||||
mfactorial: func[i][none]
|
||||
]
|
||||
|
||||
;; Test them on numbers zero to ten.
|
||||
for i 0 10 1 [print [i ":" factorial i ifactorial i mfactorial i]]
|
||||
5
Task/Factorial/Rocq/factorial.rocq
Normal file
5
Task/Factorial/Rocq/factorial.rocq
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
Fixpoint factorial (n : nat) : nat :=
|
||||
match n with
|
||||
| 0 => 1
|
||||
| S k => (S k) * (factorial k)
|
||||
end.
|
||||
5
Task/Factorial/Tcl/factorial-8.tcl
Normal file
5
Task/Factorial/Tcl/factorial-8.tcl
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
package require math::special
|
||||
|
||||
proc gfact n {
|
||||
expr {round([::math::special::Gamma [expr {$n+1}]])}
|
||||
}
|
||||
41
Task/Factorial/VBScript/factorial.vbs
Normal file
41
Task/Factorial/VBScript/factorial.vbs
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
Dim lookupTable(170), returnTable(170), currentPosition, input
|
||||
currentPosition = 0
|
||||
|
||||
Do While True
|
||||
input = InputBox("Please type a number (-1 to quit):")
|
||||
MsgBox "The factorial of " & input & " is " & factorial(CDbl(input))
|
||||
Loop
|
||||
|
||||
Function factorial (x)
|
||||
If x = -1 Then
|
||||
WScript.Quit 0
|
||||
End If
|
||||
Dim temp
|
||||
temp = lookup(x)
|
||||
If x <= 1 Then
|
||||
factorial = 1
|
||||
ElseIf temp <> 0 Then
|
||||
factorial = temp
|
||||
Else
|
||||
temp = factorial(x - 1) * x
|
||||
store x, temp
|
||||
factorial = temp
|
||||
End If
|
||||
End Function
|
||||
|
||||
Function lookup (x)
|
||||
Dim i
|
||||
For i = 0 To currentPosition - 1
|
||||
If lookupTable(i) = x Then
|
||||
lookup = returnTable(i)
|
||||
Exit Function
|
||||
End If
|
||||
Next
|
||||
lookup = 0
|
||||
End Function
|
||||
|
||||
Function store (x, y)
|
||||
lookupTable(currentPosition) = x
|
||||
returnTable(currentPosition) = y
|
||||
currentPosition = currentPosition + 1
|
||||
End Function
|
||||
74
Task/Factorial/VHDL/factorial.vhd
Normal file
74
Task/Factorial/VHDL/factorial.vhd
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
LIBRARY ieee;
|
||||
USE ieee.std_logic_1164.ALL;
|
||||
USE ieee.numeric_std.ALL;
|
||||
|
||||
ENTITY Factorial IS
|
||||
GENERIC (
|
||||
Nbin : INTEGER := 3 ; -- number of bit to input number
|
||||
Nbou : INTEGER := 13) ; -- number of bit to output factorial
|
||||
|
||||
PORT (
|
||||
clk : IN STD_LOGIC ; -- clock of circuit
|
||||
sr : IN STD_LOGIC_VECTOR(1 DOWNTO 0); -- set and reset
|
||||
N : IN STD_LOGIC_VECTOR(Nbin-1 DOWNTO 0) ; -- max number
|
||||
Fn : OUT STD_LOGIC_VECTOR(Nbou-1 DOWNTO 0)); -- factorial of "n"
|
||||
|
||||
END Factorial ;
|
||||
|
||||
ARCHITECTURE Behavior OF Factorial IS
|
||||
---------------------- Program Multiplication --------------------------------
|
||||
FUNCTION Mult ( CONSTANT MFa : IN UNSIGNED ;
|
||||
CONSTANT MI : IN UNSIGNED ) RETURN UNSIGNED IS
|
||||
VARIABLE Z : UNSIGNED(MFa'RANGE) ;
|
||||
VARIABLE U : UNSIGNED(MI'RANGE) ;
|
||||
BEGIN
|
||||
Z := TO_UNSIGNED(0, MFa'LENGTH) ; -- to obtain the multiplication
|
||||
U := MI ; -- regressive counter
|
||||
LOOP
|
||||
Z := Z + MFa ; -- make multiplication
|
||||
U := U - 1 ;
|
||||
EXIT WHEN U = 0 ;
|
||||
END LOOP ;
|
||||
RETURN Z ;
|
||||
END Mult ;
|
||||
-------------------Program Factorial ---------------------------------------
|
||||
FUNCTION Fact (CONSTANT Nx : IN NATURAL ) RETURN UNSIGNED IS
|
||||
VARIABLE C : NATURAL RANGE 0 TO 2**Nbin-1 ;
|
||||
VARIABLE I : UNSIGNED(Nbin-1 DOWNTO 0) ;
|
||||
VARIABLE Fa : UNSIGNED(Nbou-1 DOWNTO 0) ;
|
||||
BEGIN
|
||||
C := 0 ; -- counter
|
||||
I := TO_UNSIGNED(1, Nbin) ;
|
||||
Fa := TO_UNSIGNED(1, Nbou) ;
|
||||
LOOP
|
||||
EXIT WHEN C = Nx ; -- end loop
|
||||
C := C + 1 ; -- progressive couter
|
||||
Fa := Mult (Fa , I ); -- call function to make a multiplication
|
||||
I := I + 1 ; --
|
||||
END LOOP ;
|
||||
RETURN Fa ;
|
||||
END Fact ;
|
||||
--------------------- Program TO Call Factorial Function ------------------------------------------------------
|
||||
TYPE Table IS ARRAY (0 TO 2**Nbin-1) OF UNSIGNED(Nbou-1 DOWNTO 0) ;
|
||||
FUNCTION Call_Fact RETURN Table IS
|
||||
VARIABLE Fc : Table ;
|
||||
BEGIN
|
||||
FOR c IN 0 TO 2**Nbin-1 LOOP
|
||||
Fc(c) := Fact(c) ;
|
||||
END LOOP ;
|
||||
RETURN Fc ;
|
||||
END FUNCTION Call_Fact;
|
||||
|
||||
CONSTANT Result : Table := Call_Fact ;
|
||||
------------------------------------------------------------------------------------------------------------
|
||||
SIGNAL Nin : STD_LOGIC_VECTOR(N'RANGE) ;
|
||||
BEGIN -- start of architecture
|
||||
|
||||
|
||||
Nin <= N WHEN RISING_EDGE(clk) AND sr = "10" ELSE
|
||||
(OTHERS => '0') WHEN RISING_EDGE(clk) AND sr = "01" ELSE
|
||||
UNAFFECTED;
|
||||
|
||||
Fn <= STD_LOGIC_VECTOR(Result(TO_INTEGER(UNSIGNED(Nin)))) WHEN RISING_EDGE(clk) ;
|
||||
|
||||
END Behavior ;
|
||||
Loading…
Add table
Add a link
Reference in a new issue