langs a-z

This commit is contained in:
Ingy döt Net 2013-04-10 22:43:41 -07:00
parent db842d013d
commit d066446780
11389 changed files with 98361 additions and 1020 deletions

View file

@ -0,0 +1,35 @@
using System;
using System.Console;
module Program
{
Main() : void
{
WriteLine("Factorial of which number?");
def number = long.Parse(ReadLine());
WriteLine("Using Fold : Factorial of {0} is {1}", number, FactorialFold(number));
WriteLine("Using Match: Factorial of {0} is {1}", number, FactorialMatch(number));
WriteLine("Iterative : Factorial of {0} is {1}", number, FactorialIter(number));
}
FactorialFold(number : long) : long
{
$[1L..number].FoldLeft(1L, _ * _ )
}
FactorialMatch(number : long) : long
{
|0L => 1L
|n => n * FactorialMatch(n - 1L)
}
FactorialIter(number : long) : long
{
mutable accumulator = 1L;
for (mutable factor = 1L; factor <= number; factor++)
{
accumulator *= factor;
}
accumulator //implicit return
}
}

View file

@ -0,0 +1,51 @@
/* NetRexx */
options replace format comments java crossref savelog symbols nobinary
numeric digits 64 -- switch to exponential format when numbers become larger than 64 digits
say 'Input a number: \-'
say
do
n_ = long ask -- Gets the number, must be an integer
say n_'! =' factorial(n_) '(using iteration)'
say n_'! =' factorial(n_, 'r') '(using recursion)'
catch ex = Exception
ex.printStackTrace
end
return
method factorial(n_ = long, fmethod = 'I') public static returns Rexx signals IllegalArgumentException
if n_ < 0 then -
signal IllegalArgumentException('Sorry, but' n_ 'is not a positive integer')
select
when fmethod.upper = 'R' then -
fact = factorialRecursive(n_)
otherwise -
fact = factorialIterative(n_)
end
return fact
method factorialIterative(n_ = long) private static returns Rexx
fact = 1
loop i_ = 1 to n_
fact = fact * i_
end i_
return fact
method factorialRecursive(n_ = long) private static returns Rexx
if n_ > 1 then -
fact = n_ * factorialRecursive(n_ - 1)
else -
fact = 1
return fact

View file

@ -0,0 +1 @@
fact is recur [ 0 =, 1 first, pass, product, -1 +]

View file

@ -0,0 +1,2 @@
|fact 4
=24

View file

@ -0,0 +1,5 @@
[ dup 1 > [ dup 1 - factorial * ] when ] 'factorial ;
( test )
4 factorial . ( => 24 )
10 factorial . ( => 3628800 )

View file

@ -0,0 +1,3 @@
let rec factorial n =
if n <= 0 then 1
else n * factorial (n-1)

View file

@ -0,0 +1,5 @@
let factorial n =
let rec loop i accum =
if i > n then accum
else loop (i + 1) (accum * i)
in loop 1 1

View file

@ -0,0 +1,6 @@
let factorial n =
let result = ref 1 in
for i = 1 to n do
result := !result * i
done;
!result

View file

@ -0,0 +1,7 @@
bundle Default {
class Fact {
function : Main(args : String[]) ~ Nil {
5->Factorial()->PrintLine();
}
}
}

View file

@ -0,0 +1,23 @@
% built in factorial
printf("%d\n", factorial(50));
% let's define our recursive...
function fact = my_fact(n)
if ( n <= 1 )
fact = 1;
else
fact = n * my_fact(n-1);
endif
endfunction
printf("%d\n", my_fact(50));
% let's define our iterative
function fact = iter_fact(n)
fact = 1;
for i = 2:n
fact = fact * i;
endfor
endfunction
printf("%d\n", iter_fact(50));

View file

@ -0,0 +1,9 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fac \
ORDER_PP_FN(8fn(8N, \
8if(8less_eq(8N, 0), \
1, \
8mul(8N, 8fac(8dec(8N))))))
ORDER_PP(8to_lit(8fac(8))) // 40320

View file

@ -0,0 +1,11 @@
#include <order/interpreter.h>
#define ORDER_PP_DEF_8fac \
ORDER_PP_FN(8fn(8N, \
8let((8F, 8fn(8I, 8A, 8G, \
8if(8greater(8I, 8N), \
8A, \
8apply(8G, 8seq_to_tuple(8seq(8inc(8I), 8mul(8A, 8I), 8G)))))), \
8apply(8F, 8seq_to_tuple(8seq(1, 1, 8F))))))
ORDER_PP(8to_lit(8fac(8))) // 40320

View file

@ -0,0 +1,3 @@
fun {Fac1 N}
{FoldL {List.number 1 N 1} Number.'*' 1}
end

View file

@ -0,0 +1,10 @@
fun {Fac2 N}
fun {Loop N Acc}
if N < 1 then Acc
else
{Loop N-1 N*Acc}
end
end
in
{Loop N 1}
end

View file

@ -0,0 +1,8 @@
fun {Fac3 N}
Result = {NewCell 1}
in
for I in 1..N do
Result := @Result * I
end
@Result
end

View file

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

View file

@ -0,0 +1 @@
fact(n)=my(p=1);for(k=2,n,p*=k);p

View file

@ -0,0 +1 @@
fact(n)=prod(k=2,n,k)

View file

@ -0,0 +1,11 @@
f( a, b )={
my(c);
if( b == a, return(a));
if( b-a > 1,
c=(b + a) >> 1;
return(f(a, c) * f(c+1, b))
);
return( a * b );
}
fact(n) = f(1, n)

View file

@ -0,0 +1 @@
fact(n)=n!

View file

@ -0,0 +1 @@
fact(n)=round(gamma(n+1))

View file

@ -0,0 +1,9 @@
fact(n)={
my(v=vector(n+1,i,i==1));
for(i=2,n+1,
forstep(j=i,2,-1,
for(k=2,j,v[k]+=v[k-1])
)
);
v[n+1]
};

View file

@ -0,0 +1,12 @@
factorial: procedure (N) returns (fixed decimal (30));
declare N fixed binary nonassignable;
declare i fixed decimal (10);
declare F fixed decimal (30);
if N < 0 then signal error;
F = 1;
do i = 2 to N;
F = F * i;
end;
return (F);
end factorial;

View file

@ -0,0 +1,9 @@
function factorial(n: integer): integer;
var
i, result: integer;
begin
result := 1;
for i := 2 to n do
result := result * i;
factorial := result
end;

View file

@ -0,0 +1,8 @@
function factorial(n: integer): integer;
begin
if n = 0
then
factorial := 1
else
factorial := n*factorial(n-1)
end;

View file

@ -0,0 +1,3 @@
sub postfix:<!> { [*] 1..$^n }
say 5!; #prints 120

View file

@ -0,0 +1,37 @@
push 1
not
in(number)
duplicate
not // label a
pointer // pointer 1
duplicate
push 1
subtract
push 1
pointer
push 1
noop
pointer
duplicate // the next op is back at label a
push 1 // this part continues from pointer 1
noop
push 2 // label b
push 1
rot 1 2
duplicate
not
pointer // pointer 2
multiply
push 3
pointer
push 3
pointer
push 3
push 3
pointer
pointer // back at label b
pop // continues from pointer 2
out(number)
exit

View file

@ -0,0 +1,12 @@
/fact {
dup 0 eq % check for the argument being 0
{
pop 1 % if so, the result is 1
}
{
dup
1 sub
fact % call recursively with n - 1
mul % multiply the result with n
} ifelse
} def

View file

@ -0,0 +1,6 @@
/fact {
1 % initial value for the product
1 1 % for's start value and increment
4 -1 roll % bring the argument to the top as for's end value
{ mul } for
} def

View file

@ -0,0 +1 @@
/myfact {{dup 0 eq} {pop 1} {dup pred} {mul} linrec}.

View file

@ -0,0 +1,16 @@
function fact1#(n%)
local i%,r#
r#=1
for i%=1 to n%
r#=r#*i%
next
fact1#=r#
end function
function fact2#(n%)
if n%<=2 then fact2#=n% else fact2#=fact2#(n%-1)*n%
end function
for i%=1 to 20
print i%,fact1#(i%),fact2#(i%)
next

View file

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

View 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
}
}

View file

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

View file

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

View file

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

View file

@ -0,0 +1,3 @@
fact n = n*fact (n-1) if n>0;
= 1 otherwise;
let facts = map fact (1..10); facts;

View file

@ -0,0 +1,3 @@
fact n = loop 1 n with
loop p n = if n>0 then loop (p*n) (n-1) else p;
end;

View file

@ -0,0 +1,7 @@
Procedure factorial(n)
Protected i, f = 1
For i = 2 To n
f = f * i
Next
ProcedureReturn f
EndProcedure

View file

@ -0,0 +1,7 @@
Procedure Factorial(n)
If n < 2
ProcedureReturn 1
Else
ProcedureReturn n * Factorial(n - 1)
EndIf
EndProcedure

View file

@ -0,0 +1 @@
f:(*/)1+til@

View file

@ -0,0 +1 @@
f:(*)over 1+til@

View file

@ -0,0 +1 @@
f:prd 1+til@

View file

@ -0,0 +1 @@
f:{(*/)1+til x}

View file

@ -0,0 +1 @@
f:{$[x=1;1;x*.z.s x-1]}

View file

@ -0,0 +1,57 @@
REBOL [
Title: "Factorial"
Author: oofoe
Date: 2009-12-10
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

@ -0,0 +1,6 @@
public int factorial_iter(int n){
result = 1;
for(i <- [1..n])
result *= i;
return result;
}

View file

@ -0,0 +1 @@
public int factorial_iter2(int n) = (1 | it*e | int e <- [1..n]);

View file

@ -0,0 +1,5 @@
rascal>factorial_iter(10)
int: 3628800
rascal>factorial_iter2(10)
int: 3628800

View file

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

View file

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

View file

@ -0,0 +1,2 @@
: <factorial> dup 1 = if; dup 1- <factorial> * ;
: factorial dup 0 = [ 1+ ] [ <factorial> ] if ;

View file

@ -0,0 +1,4 @@
define('rfact(n)') :(rfact_end)
rfact rfact = le(n,0) 1 :s(return)
rfact = n * rfact(n - 1) :(return)
rfact_end

View file

@ -0,0 +1,4 @@
define('trfact(n,f)') :(trfact_end)
trfact trfact = le(n,0) f :s(return)
trfact = trfact(n - 1, n * f) :(return)
trfact_end

View file

@ -0,0 +1,5 @@
define('ifact(n)') :(ifact_end)
ifact ifact = 1
if1 ifact = gt(n,0) n * ifact :f(return)
n = n - 1 :(if1)
ifact_end

View file

@ -0,0 +1,3 @@
loop i = le(i,10) i + 1 :f(end)
output = rfact(i) ' ' trfact(i,1) ' ' ifact(i) :(loop)
end

View file

@ -0,0 +1,10 @@
const func bigInteger: factorial (in bigInteger: n) is func
result
var bigInteger: result is 1_;
local
var bigInteger: i is 0_;
begin
for i range 1_ to n do
result *:= i;
end for;
end func;

View file

@ -0,0 +1,8 @@
const func bigInteger: factorial (in bigInteger: n) is func
result
var bigInteger: result is 1_;
begin
if n > 1_ then
result := n * factorial(pred(n));
end if;
end func;

View file

@ -0,0 +1,10 @@
define main
function main(x : integer returns integer)
for a in 1, x
returns
value of product a
end for
end function

View file

@ -0,0 +1,11 @@
define main
function main(x : integer returns integer)
if x = 0 then
1
else
x * main(x - 1)
end if
end function

View file

@ -0,0 +1,8 @@
n@(Integer traits) factorial
"The standard recursive definition."
[
n isNegative ifTrue: [error: 'Negative inputs to factorial are invalid.'].
n <= 1
ifTrue: [1]
ifFalse: [n * ((n - 1) factorial)]
].

View file

@ -0,0 +1,5 @@
n@(Integer traits) factorial2
[
n isNegative ifTrue: [error: 'Negative inputs to factorial are invalid.'].
(1 upTo: n by: 1) reduce: [|:a :b| a * b]
].

View file

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

View file

@ -0,0 +1,3 @@
fun factorial n =
if n <= 0 then 1
else n * factorial (n-1)

View file

@ -0,0 +1,7 @@
fun factorial n = let
fun loop (i, accum) =
if i > n then accum
else loop (i + 1, accum * i)
in
loop (1, 1)
end

View file

@ -0,0 +1,4 @@
factorial(x)
Func
Return Π(y,y,1,x)
EndFunc

View file

@ -0,0 +1,16 @@
$$ MODE TUSCRIPT
LOOP num=-1,12
IF (num==0,1) THEN
f=1
ELSEIF (num<0) THEN
PRINT num," is negative number"
CYCLE
ELSE
f=VALUE(num)
LOOP n=#num,2,-1
f=f*(n-1)
ENDLOOP
ENDIF
formatnum=CENTER(num,+2," ")
PRINT "factorial of ",formatnum," = ",f
ENDLOOP

View file

@ -0,0 +1,8 @@
function Factorial(%num)
{
if(%num < 2)
return 1;
for(%a = %num-1; %a > 1; %a--)
%num *= %a;
return %num;
}

View file

@ -0,0 +1,6 @@
function Factorial(%num)
{
if(%num < 2)
return 1;
return %num * Factorial(%num-1);
}

View file

@ -0,0 +1,7 @@
factorial() {
set -- "$1" 1
until test "$1" -lt 2; do
set -- "`expr "$1" - 1`" "`expr "$2" \* "$1"`"
done
echo "$2"
}

View file

@ -0,0 +1,7 @@
function factorial {
typeset n=$1 f=1 i
for ((i=2; i < n; i++)); do
(( f *= i ))
done
echo $f
}

View file

@ -0,0 +1,7 @@
factorial ()
{
if [ $1 -eq 0 ]
then echo 1
else echo $(($1 * $(factorial $(($1-1)) ) ))
fi
}

View file

@ -0,0 +1,5 @@
function factorial {
typeset n=$1
(( n < 2 )) && echo 1 && return
echo $(( n * $(factorial $((n-1))) ))
}

View file

@ -0,0 +1,13 @@
alias factorial eval \''set factorial_args=( \!*:q ) \\
@ factorial_n = $factorial_args[2] \\
@ factorial_i = 1 \\
while ( $factorial_n >= 2 ) \\
@ factorial_i *= $factorial_n \\
@ factorial_n -= 1 \\
end \\
@ $factorial_args[1] = $factorial_i \\
'\'
factorial f 12
echo $f
# => 479001600

View file

@ -0,0 +1,4 @@
#import nat
good_factorial = ~&?\1! product:-1^lrtPC/~& iota
better_factorial = ~&?\1! ^T(~&lSL,@rS product:-1)+ ~&Z-~^*lrtPC/~& iota

View file

@ -0,0 +1,3 @@
#cast %nL
test = better_factorial* <0,1,2,3,4,5,6,7,8>

View 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

View file

@ -0,0 +1 @@
DEF fac(n) n <= 1 | PROD 1:to(n);

View file

@ -0,0 +1 @@
DEF fac(n) n <= 0 => 1 // n * fac(n - 1);

View file

@ -0,0 +1 @@
DEF fac(n) n <= 1 | :"*":foldl(ALL 1:to(n));

View file

@ -0,0 +1,2 @@
0! -> 1
N! -> N * (N-1)!

View file

@ -0,0 +1,11 @@
func FactIter(N); \Factorial of N using iterative method
int N; \range: 0..12
int F, I;
[F:= 1;
for I:= 2 to N do F:= F*I;
return F;
];
func FactRecur(N); \Factorial of N using recursive method
int N; \range: 0..12
return if N<2 then 1 else N*FactRecur(N-1);

View file

@ -0,0 +1,9 @@
10 LET x=5: GO SUB 1000: PRINT "5! = ";r
999 STOP
1000 REM *************
1001 REM * FACTORIAL *
1002 REM *************
1010 LET r=1
1020 IF x<2 THEN RETURN
1030 FOR i=2 TO x: LET r=r*i: NEXT i
1040 RETURN

View file

@ -0,0 +1 @@
DEF FN f(n) = VAL (("1" AND n<=0) + ("n*FN f(n-1)" AND n>0))

View file

@ -0,0 +1 @@
DEF FN f(n) = VAL (("1" AND n<=0) + ("FN f(n-1)*n" AND n>0))