September 2017 Update

This commit is contained in:
Ingy döt Net 2017-09-23 10:01:46 +02:00
parent bba7bfd280
commit ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions

View file

@ -1,7 +1,7 @@
;Definitions:
:*   The factorial of   '''0'''   (zero)   is [[wp:Factorial#Definition|defined]] as being   1   (unity).
:* &nbsp; The &nbsp; '''Factorial Function''' &nbsp; of a positive integer, &nbsp; <big> ''n'', </big> &nbsp; is defined as the product of the sequence:
<big><big> ''n'', &nbsp; ''n''-1, &nbsp; ''n''-2, &nbsp; ... &nbsp; 1 </big></big>
:* &nbsp; The factorial of &nbsp; '''0''' &nbsp; (zero) &nbsp; is [[wp:Factorial#Definition|defined]] as being &nbsp; 1 &nbsp; (unity).
;Task:

View file

@ -0,0 +1,8 @@
INTEGER FUNCTION FACTORIAL( N ); INTEGER N;
BEGIN
INTEGER I, FACT;
FACT := 1;
FOR I := 2 STEP 1 UNTIL N DO
FACT := FACT * I;
FACTORIAL := FACT;
END;

View file

@ -1,17 +1,18 @@
-- FACTORIAL -----------------------------------------------------------------
-- factorial :: Int -> Int
on factorial(x)
script product
on lambda(a, b)
on |λ|(a, b)
a * b
end lambda
end |λ|
end script
foldl(product, 1, range(1, x))
foldl(product, 1, enumFromTo(1, x))
end factorial
-- TEST
-- TEST ----------------------------------------------------------------------
on run
factorial(11)
@ -21,23 +22,11 @@ on run
end run
-- GENERIC LIBRARY PRIMITIVES
-- GENERIC FUNCTIONS ---------------------------------------------------------
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to lambda(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- range :: Int -> Int -> [Int]
on range(m, n)
if n < m then
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m > n then
set d to -1
else
set d to 1
@ -47,8 +36,19 @@ on range(m, n)
set end of lst to i
end repeat
return lst
end range
end enumFromTo
-- foldl :: (a -> b -> a) -> a -> [b] -> a
on foldl(f, startValue, xs)
tell mReturn(f)
set v to startValue
set lng to length of xs
repeat with i from 1 to lng
set v to |λ|(v, item i of xs, i, xs)
end repeat
return v
end tell
end foldl
-- Lift 2nd class handler function into 1st class script wrapper
-- mReturn :: Handler -> Script
@ -57,7 +57,7 @@ on mReturn(f)
f
else
script
property lambda : f
property |λ| : f
end script
end if
end mReturn

View file

@ -0,0 +1,11 @@
10 REM FACTORIAL
20 REM COMMODORE BASIC 2.0
30 N = 10 : GOSUB 100
40 PRINT N"! ="F
50 END
100 REM FACTORIAL CALC USING SIMPLE LOOP
110 F = 1
120 FOR I=1 TO N
130 F = F*I
140 NEXT
150 RETURN

View file

@ -0,0 +1,6 @@
10 INPUT N
20 LET FACT=1
30 FOR I=2 TO N
40 LET FACT=FACT*I
50 NEXT I
60 PRINT FACT

View file

@ -0,0 +1,10 @@
10 INPUT N
20 LET FACT=1
30 GOSUB 60
40 PRINT FACT
50 STOP
60 IF N=0 THEN RETURN
70 LET FACT=FACT*N
80 LET N=N-1
90 GOSUB 60
100 RETURN

View file

@ -0,0 +1,11 @@
' Factorial
FUNCTION factorial(NUMBER n) TYPE NUMBER
IF n <= 1 THEN
RETURN 1
ELSE
RETURN n * factorial(n - 1)
ENDIF
END FUNCTION
n = VAL(TOKEN$(ARGUMENT$, 2))
PRINT n, factorial(n) FORMAT "%ld! = %ld\n"

View file

@ -0,0 +1,7 @@
#! /usr/bin/bc -q
define f(x) {
if (x <= 1) return (1); return (f(x-1) * x)
}
f(1000)
quit

View file

@ -0,0 +1,4 @@
p <
_>1FT"pF>M"p~.~d
>Pd >~{Np
d <

View file

@ -0,0 +1,3 @@
p <
_1FT"pF>M"p~.~d
>Pd >~{;

View file

@ -0,0 +1,13 @@
julia> beeswax("factorial.bswx")
i0
1
i1
1
i2
2
i3
6
i10
3628800
i22
17196083355034583040

View file

@ -0,0 +1,15 @@
n = 5 # calculates n!
acc = 1
factorial
comefrom
comefrom accumulate if n < 1
accumulate
comefrom factorial
acc = acc * n
comefrom factorial if n is 0
n = n - 1
acc # prints the result

View file

@ -0,0 +1,19 @@
[*
* (n) lfx -- (factorial of n)
*]sz
[
1 Sp [product = 1]sz
[ [Loop while 1 < n:]sz
d lp * sp [product = n * product]sz
1 - [n = n - 1]sz
d 1 <f
]Sf d 1 <f
Lfsz [Drop loop.]sz
sz [Drop n.]sz
Lp [Push product.]sz
]sf
[*
* For example, print the factorial of 50.
*]sz
50 lfx psz

View file

@ -9,7 +9,17 @@ defmodule Factorial do
def fac_tail(1, acc), do: acc
def fac_tail(n, acc) when n > 1, do: fac_tail(n - 1, acc * n)
# Tail recursive function with default parameter
def fac_default(n, acc \\ 1)
def fac_default(0, acc), do: acc
def fac_default(n, acc) when n > 0, do: fac_default(n - 1, acc * n)
# Using Enumeration features
def fac_reduce(0), do: 1
def fac_reduce(n) when n > 0, do: Enum.reduce(1..n, 1, &*/2)
# Using Enumeration features with pipe operator
def fac_pipe(0), do: 1
def fac_pipe(n) when n > 0, do: 1..n |> Enum.reduce(1, &*/2)
end

View file

@ -0,0 +1,8 @@
function factorial
set x $argv[1]
set result 1
for i in (seq $x)
set result (expr $i '*' $result)
end
echo $result
end

View file

@ -0,0 +1,8 @@
function factorial
set x $argv[1]
if [ $x -eq 1 ]
echo 1
else
expr (factorial (expr $x - 1)) '*' $x
end
end

View file

@ -0,0 +1,3 @@
set xrange [0:4.95]
set key left
plot int(x)!

View file

@ -0,0 +1,11 @@
# Using int(n) allows non-integer "n" inputs with the factorial
# calculated on int(n) in that case.
# Arranging the condition as "n>=2" avoids infinite recursion if
# n==NaN, since any comparison involving NaN is false. Could change
# "1" to an expression like "n*0+1" to propagate a NaN input to the
# output too, if desired.
#
factorial(n) = (n >= 2 ? int(n)*factorial(n-1) : 1)
set xrange [0:4.95]
set key left
plot factorial(x)

View file

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

View file

@ -0,0 +1,7 @@
(defn ! [n]
(reduce *
(range 1 (inc n))
1))
(print (! 6)) ; 720
(print (! 0)) ; 1

View file

@ -6,7 +6,7 @@ function factorial(n) {
//we skip zero and one since both are 1 and are identity
while (n > 1) {
sum *= n;
n -= 1;
n--;
}
return sum;
}

View file

@ -1,2 +1,2 @@
factRecursive::{:[x>1;x*_.f(x-1);1]}
factRecursive::{:[x>1;x*.f(x-1);1]}
factIterative::{*/1+!x}

View file

@ -1,6 +1,6 @@
fun facti(n: Int) = when {
n < 0 -> throw IllegalArgumentException("negative numbers not allowed")
else -> {
else -> {
var ans = 1L
for (i in 2..n) ans *= i
ans
@ -10,7 +10,7 @@ fun facti(n: Int) = when {
fun factr(n: Int): Long = when {
n < 0 -> throw IllegalArgumentException("negative numbers not allowed")
n < 2 -> 1L
else -> n * factr(n - 1)
else -> n * factr(n - 1)
}
fun main(args: Array<String>) {

View file

@ -0,0 +1,4 @@
> (define (factorial n) (exp (gammaln (+ n 1))))
(lambda (n) (exp (gammaln (+ n 1))))
> (factorial 4)
24

View file

@ -1 +0,0 @@
<@ SAYFCTLIT>5</@>

View file

@ -1,2 +0,0 @@
<@ DEFUDOLITLIT>FAT|__Transformer|<@ LETSCPLIT>result|1</@><@ ITEFORPARLIT>1|<@ ACTMULSCPPOSFOR>result|...</@></@><@ LETRESSCP>...|result</@></@>
<@ SAYFATLIT>123</@>

View file

@ -1,5 +0,0 @@
def factorial(n):
z=1
if n>1:
z=n*factorial(n-1)
return z

View file

@ -1,4 +0,0 @@
public int factorial_rec(int n){
if(n>1) return n*factorial_rec(n-1);
else return 1;
}

View file

@ -1,2 +0,0 @@
rascal>factorial_rec(10)
int: 3628800

View file

@ -0,0 +1,4 @@
fact(n)=
? n!>1, <=1
<= n*fact(n-1)
.

View file

@ -0,0 +1,3 @@
function f=factorgamma(n)
f = gamma(n+1)
endfunction

View file

@ -0,0 +1,3 @@
func factorial_recursive(n) {
n == 0 ? 1 : (n * __FUNC__(n-1))
}

View file

@ -0,0 +1,3 @@
func factorial_reduce(n) {
1..n -> reduce({|a,b| a * b }, 1)
}

View file

@ -0,0 +1,5 @@
func factorial_iterative(n) {
var f = 1
{|i| f *= i } << 2..n
return f
}

View file

@ -0,0 +1 @@
say 5!

View file

@ -1,19 +0,0 @@
# Recursive
func factorial_recursive(n) {
n == 0 ? 1 : (n * __FUNC__(n-1));
}
# Iterative with Array#reduce
func factorial_reduce(n) {
1..n -> reduce('*');
}
# Iterative with Block#repeat
func factorial_iterative(n) {
var f = 1;
{|i| f *= i } * n;
return f;
}
# Built-in Number#factorial:
say 5!;

View file

@ -1,2 +0,0 @@
slate[5]> 100 factorial.
93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000

View file

@ -0,0 +1,15 @@
mata
real scalar function fact1(real scalar n) {
if (n<2) return(1)
else return(fact1(n-1)*n)
}
real scalar function fact2(real scalar n) {
a=1
for (i=2;i<=n;i++) a=a*i
return(a)
}
printf("%f\n",fact1(8))
printf("%f\n",fact2(8))
printf("%f\n",factorial(8))

View file

@ -1,4 +1,3 @@
func factorial(num: Int) -> Int {
return num < 1 ? 1 : reduce(1...num, 1, *)
func factorial(_ n: Int) -> Int {
return n < 2 ? 1 : (2...n).reduce(1, *)
}

View file

@ -1,4 +1,3 @@
func factorial(num: Int) -> Int {
return num < 1 ? 1 : num * factorial(num - 1)
func factorial(_ n: Int) -> Int {
return n < 2 ? 1 : n * factorial(n - 1)
}

View file

@ -0,0 +1,4 @@
: FACTORIAL
1 SWAP
1 + 1 DO
I * LOOP ;

View file

@ -0,0 +1,11 @@
global factorial
section .text
; Input in ECX register (greater than 0!)
; Output in EAX register
factorial:
mov eax, 1
.factor:
mul ecx
loop .factor
ret

View file

@ -0,0 +1,20 @@
global fact
section .text
; Input and output in EAX register
fact:
cmp eax, 1
je .done ; if eax == 1 goto done
; inductive case
push eax ; save n (ie. what EAX is)
dec eax ; n - 1
call fact ; fact(n - 1)
pop ebx ; fetch old n
mul ebx ; multiplies EAX with EBX, ie. n * fac(n - 1)
ret
.done:
; base case: return 1
mov eax, 1
ret

View file

@ -0,0 +1,17 @@
global factorial
section .text
; Input in ECX register
; Output in EAX register
factorial:
mov eax, 1 ; default argument, store 1 in accumulator
.base_case:
test ecx, ecx
jnz .inductive_case ; return accumulator if n == 0
ret
.inductive_case:
mul ecx ; accumulator *= n
dec ecx ; n -= 1
jmp .base_case ; tail call

View file

@ -0,0 +1,6 @@
(defun factorial (x)
(if (< x 0)
nil
(if (<= x 1)
1
(* x (factorial (- x 1))) ) ) )

View file

@ -0,0 +1,5 @@
fcn fact(n){[2..n].reduce('*,1)}
fcn factTail(n,N=1) { // tail recursion
if (n == 0) return(N);
return(self.fcn(n-1,n*N));
}

View file

@ -0,0 +1,3 @@
fact(6).println();
var BN=Import("zklBigNum");
factTail(BN(42)) : "%,d".fmt(_).println(); // built in as BN(42).factorial()