Data update

This commit is contained in:
Ingy döt Net 2026-02-01 16:33:20 -08:00
parent 5150844a7d
commit 4bb20c9b71
7735 changed files with 38060 additions and 199180 deletions

View file

@ -1,9 +0,0 @@
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

@ -1,8 +0,0 @@
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

@ -1,62 +0,0 @@
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

@ -1,4 +1,7 @@
factorial: $[n][
if? n>0 [n * factorial n-1]
else [1]
factorial: function [n][
switch n > 0 -> n * factorial n-1
-> 1
]
loop 1..19 [x]->
print ["Factorial of" x "=" factorial x]

View file

@ -1,3 +1,7 @@
factorial: $[n][
fold.seed:1 1..n [a,b][a*b]
]
loop 1..19 [x][
print ["Factorial of" x "=" factorial x]
]

View file

@ -1,12 +0,0 @@
;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

@ -1,10 +0,0 @@
;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

@ -1 +0,0 @@
MOVE FUNCTION FACTORIAL(num) TO result

View file

@ -1,21 +0,0 @@
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.

View file

@ -1,22 +0,0 @@
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.

View file

@ -1,34 +0,0 @@
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.

View file

@ -1,7 +0,0 @@
proc fac(n) {
var r = 1;
for i in 1..n do
r *= i;
return r;
}

View file

@ -1,5 +0,0 @@
Fixpoint factorial (n : nat) : nat :=
match n with
| 0 => 1
| S k => (S k) * (factorial k)
end.

View file

@ -1,11 +0,0 @@
;; 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.)

View file

@ -1,8 +0,0 @@
;; 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))))))

View file

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

View file

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

View file

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

View file

@ -1,7 +0,0 @@
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

@ -1,106 +0,0 @@
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

@ -1,6 +0,0 @@
static function factorial(n:Int):Int {
var result = 1;
while (1<n)
result *= n--;
return result;
}

View file

@ -1,3 +0,0 @@
static function factorial(n:Int):Int {
return n == 0 ? 1 : n * factorial2(n - 1);
}

View file

@ -1,7 +0,0 @@
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);
}

View file

@ -1,3 +0,0 @@
static function factorial(n:Int):Int {
return [for (i in 1...(n+1)) i].fold(function(num, total) return total *= num, 1);
}

View file

@ -1,58 +0,0 @@
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');
}
}

View file

@ -1,6 +0,0 @@
function Get-Factorial ($x) {
if ($x -eq 0) {
return 1
}
return $x * (Get-Factorial ($x - 1))
}

View file

@ -1,9 +0,0 @@
function Get-Factorial ($x) {
if ($x -eq 0) {
return 1
} else {
$product = 1
1..$x | ForEach-Object { $product *= $_ }
return $product
}
}

View file

@ -1,6 +0,0 @@
function Get-Factorial ($x) {
if ($x -eq 0) {
return 1
}
return (Invoke-Expression (1..$x -join '*'))
}

View file

@ -1,55 +0,0 @@
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
]
; 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]
]
; Test them on numbers zero to ten.
for i 0 10 1 [print [i ":" factorial i ifactorial i mfactorial i]]

View file

@ -1,13 +1,17 @@
-- 30 Jul 2025
include Settings
-- 23 Aug 2025
include Setting
say 'Factorial'
say version
say
call First20
call Imp 10, '1 13 71 450 3249 25206 205022 1723508 14842907 130202808'
call ReC 10, '1 13 71 450 3249 25206 205022'
call Imp 1E6,'1 13 71 450 3249 25206'
call Imperative 10, '1 13 71 450 3249 25206 205022 1723508 14842907 130202808'
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 Timer
exit
First20:
@ -19,9 +23,9 @@ end
say
return
Imp:
Imperative:
call ResetMemo
call Time('r')
call Time('R')
arg d,p
numeric digits d; Fact. = 0
say 'Imperative in' d 'digits precision...'
@ -36,14 +40,14 @@ end
say
return
ReC:
Recursive:
call ResetMemo
call Time('r')
call Time('R')
arg d,p
numeric digits d; Fact. = 0
say 'Recursive in' d 'digits precision...'
do i = 1 to Words(p)
call Time('r'); f = Word(p,i); h = Recursive(f)
call Time('r'); f = Word(p,i); h = Recurs(f)
parse var h 'E' e
if e = '' then
say Right(f'!',10) 'has exact' Right(Length(h),9) 'digits' '('Format(Time('e'),,3)'s)'
@ -53,12 +57,12 @@ end
say
return
Recursive:
Recurs:
procedure
arg xx
if xx = 0 then
return 1
else
return xx*Recursive(xx-1)
return xx*Recurs(xx-1)
include Math

View file

@ -1,5 +1,6 @@
proc ifact_caching n {
global fact_cache
tailcall fact [expr {$n-1}] [expr {$n*$result}]
if { ! [info exists fact_cache]} {
set fact_cache {1 1}
}

View file

@ -1,11 +1,52 @@
puts [ifact 30]
puts [rfact 30]
puts [ifact_caching 30]
# calls cmd with args
# retpeatedly in scope above
proc trampoline {cmd args} {
set n 400
set iterations 10000
puts "calculate $n factorial $iterations times"
puts "ifact: [time {ifact $n} $iterations]"
puts "rfact: [time {rfact $n} $iterations]"
# for the caching proc, reset the cache between each iteration so as not to skew the results
puts "ifact_caching: [time {ifact_caching $n; unset -nocomplain fact_cache} $iterations]"
# thunk is {cmd {arg1 arg2 arg3 ...} }
set result [uplevel 1 [concat $cmd $args]]
# split into vars
lassign $result type thunk
# loop
while {$type eq "next"} {
set result [uplevel 1 $thunk]
lassign $result type thunk
}
set final_value $thunk
return $final_value
}
# return { value final_value }
# or { next {cmd arg1 arg2} }
proc factorial_step {n result} {
if {$n <= 1} {
set ret_value [list "value" $result]
} else {
# n-1
set next_n [expr {$n-1}]
# (n-1) * fact(n)
set next_result [expr {$n * $result}]
# {func arg1 arg2}
set next_thunk [list factorial_step $next_n $next_result]
# Return the next step as a list
set ret_value [list "next" $next_thunk]
}
return $ret_value
}
# The execution is wrapped by the trampoline
proc factorial {n} {
return [trampoline factorial_step $n 1]
}
set f [factorial 100]

View file

@ -1,5 +1,11 @@
package require math::special
puts [ifact 30]
puts [rfact 30]
puts [ifact_caching 30]
proc gfact n {
expr {round([::math::special::Gamma [expr {$n+1}]])}
}
set n 400
set iterations 10000
puts "calculate $n factorial $iterations times"
puts "ifact: [time {ifact $n} $iterations]"
puts "rfact: [time {rfact $n} $iterations]"
# for the caching proc, reset the cache between each iteration so as not to skew the results
puts "ifact_caching: [time {ifact_caching $n; unset -nocomplain fact_cache} $iterations]"

View file

@ -1,41 +0,0 @@
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

View file

@ -1,74 +0,0 @@
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 ;