Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1 @@
factorial:{1 */ 1+range[x]} /Call: factorial[1000]

View file

@ -0,0 +1,11 @@
public static long fact(final Integer n) {
if (n < 0) {
System.debug('No negative numbers');
return 0;
}
long ans = 1;
for (Integer i = 1; i <= n; i++) {
ans *= i;
}
return ans;
}

View file

@ -0,0 +1,7 @@
public static long factRec(final Integer n) {
if (n < 0){
System.debug('No negative numbers');
return 0;
}
return (n < 2) ? 1 : n * fact(n - 1);
}

View file

@ -0,0 +1,7 @@
Lbl FACT
1→R
For(I,1,r₁)
R*I→R
End
R
Return

View file

@ -0,0 +1,3 @@
Lbl FACT
r₁??1,r₁*FACT(r₁-1)
Return

View file

@ -0,0 +1,20 @@
shared void run() {
Integer? recursiveFactorial(Integer n) =>
switch(n <=> 0)
case(smaller) null
case(equal) 1
case(larger) if(exists f = recursiveFactorial(n - 1)) then n * f else null;
Integer? iterativeFactorial(Integer n) =>
switch(n <=> 0)
case(smaller) null
case(equal) 1
case(larger) (1:n).reduce(times);
for(Integer i in 0..10) {
print("the iterative factorial of ``i`` is ``iterativeFactorial(i) else "negative"``
and the recursive factorial of ``i`` is ``recursiveFactorial(i) else "negative"``\n");
}
}

View file

@ -0,0 +1,10 @@
0 => int total;
fun int factorial(int i)
{
if (i == 0) return 1;
else
{
i * factorial(i - 1) => total;
}
return total;
}

View file

@ -0,0 +1,10 @@
1 => int total;
fun int factorial(int i)
{
while(i > 0)
{
total * i => total;
1 -=> i;
}
return total;
}

View file

@ -0,0 +1,7 @@
PROC Recursive(n) CLOSED
r:=1
IF n>1 THEN
r:=n*Recursive(n-1)
ENDIF
RETURN r
ENDPROC Recursive

View file

@ -0,0 +1,43 @@
LDA x
BRZ done_i ; 0! = 1
STA i
loop_i: LDA fact
STA n
LDA i
SUB one
BRZ done_i
STA j
loop_j: LDA fact
ADD n
STA fact
LDA j
SUB one
BRZ done_j
STA j
JMP loop_j
done_j: LDA i
SUB one
STA i
JMP loop_i
done_i: LDA fact
STP
one: 1
fact: 1
i: 0
j: 0
n: 0
x: 5

View file

@ -0,0 +1,15 @@
LDA load
ADD x
STA load
load: LDA fact
STP
fact: 1
1
2
6
24
120
x: 5

View file

@ -0,0 +1,8 @@
PROCEDURE FACTORIAL(X%->F)
F=1
IF X%<>0 THEN
FOR I%=X% TO 2 STEP Ä1 DO
F=F*X%
END FOR
END IF
END PROCEDURE

View file

@ -0,0 +1,4 @@
PROCEDURE FACTORIAL(FACT,X%->FACT)
IF X%>1 THEN FACTORIAL(X%*FACT,X%-1->FACT)
END IF
END PROCEDURE

View file

@ -0,0 +1,4 @@
(define (fact n)
(for/product ((f (in-range 2 (1+ n)))) f))
(fact 10)
→ 3628800

View file

@ -0,0 +1,6 @@
(define (fact n)
(if (zero? n) 1
(* n (fact (1- n)))))
(remember 'fact)
(fact 10)
→ 3628800

View file

@ -0,0 +1,5 @@
(define (fact n (acc 1))
(if (zero? n) acc
(fact (1- n) (* n acc))))
(fact 10)
→ 3628800

View file

@ -0,0 +1,2 @@
(factorial 10)
→ 3628800

View file

@ -0,0 +1,4 @@
(lib 'math)
math.lib v1.13 ® EchoLisp
(gamma 11)
→ 3628800.0000000005

View file

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

View file

@ -0,0 +1,9 @@
நிரல்பாகம் fact ( n )
@( n == 0 ) ஆனால்
பின்கொடு 1
இல்லை
பின்கொடு n*fact( n - 1 )
முடி
முடி
பதிப்பி fact ( 10 )

View file

@ -0,0 +1,27 @@
' FB 1.05.0 Win64
Function Factorial_Iterative(n As Integer) As Integer
Var result = 1
For i As Integer = 2 To n
result *= i
Next
Return result
End Function
Function Factorial_Recursive(n As Integer) As Integer
If n = 0 Then Return 1
Return n * Factorial_Recursive(n - 1)
End Function
For i As Integer = 1 To 5
Print i; " =>"; Factorial_Iterative(i)
Next
For i As Integer = 6 To 10
Print Using "##"; i;
Print " =>"; Factorial_Recursive(i)
Next
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1,10 @@
def factorial( n ) =
if n < 0
error( 'factorial: n should be non-negative' )
else
res = 1
for i <- 2..n
res *= i
res

View file

@ -0,0 +1,5 @@
def
factorial( (0|1) ) = 1
factorial( n )
| n > 0 = n*factorial( n - 1 )
| otherwise = error( 'factorial: n should be non-negative' )

View file

@ -0,0 +1,8 @@
def factorial( n )
| n >= 0 =
def
fact( acc, 0 ) = acc
fact( acc, n ) = fact( acc*n, n - 1 )
fact( 1, n )
| otherwise = error( 'factorial: n should be non-negative' )

View file

@ -0,0 +1,3 @@
def factorial( n )
| n >= 0 = product( 1..n )
| otherwise = error( 'factorial: n should be non-negative' )

View file

@ -0,0 +1,3 @@
fun fact(n: int): int =
if n == 0 then 1
else n * fact(n-1)

View file

@ -0,0 +1,4 @@
fun fact(n: int): int =
loop (out = 1) = for i < n do
out * (i+1)
in out

View file

@ -0,0 +1,33 @@
include "ConsoleWindow"
local fn factorialIterative( n as long ) as double
dim as double f
dim as long i
if ( n > 1 )
f = 1
for i = 2 To n
f = f * i
next i
else
f = 1
end if
end fn = f
local fn factorialRecursive( n as long ) as double
dim as double f
if ( n < 2 )
f = 1
else
f = n * fn factorialRecursive( n -1 )
end if
end fn = f
dim as long i
for i = 0 to 12
print "Iterative:"; using "####"; i; " ="; fn factorialIterative( i )
print "Recursive:"; using "####"; i; " ="; fn factorialRecursive( i )
print
next i

View file

@ -0,0 +1,18 @@
function fact(n, acc) r {
if n = 0
return acc
end
return fact(n-1, n*acc)
}
function factorial(n) r {
return fact(n, 1)
}
software {
print(factorial(0))
print(factorial(1))
print(factorial(2))
print(factorial(3))
print(factorial(22))
}

View file

@ -0,0 +1,9 @@
PROC FACTORIAL(ARG) U;
BEGIN
ITEM ARG U;
ITEM TEMP U;
TEMP = 1;
FOR I:2 BY 1 WHILE I<=ARG;
TEMP = TEMP*I;
FACTORIAL = TEMP;
END

View file

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

View file

@ -0,0 +1,4 @@
(defun factorial (n)
(cond
((== n 0) 1)
((> n 0) (* n (factorial (- n 1))))))

View file

@ -0,0 +1,4 @@
(defun factorial
((n) (when (== n 0)) 1)
((n) (when (> n 0))
(* n (factorial (- n 1)))))

View file

@ -0,0 +1,4 @@
(defun factorial
((0) 1)
((n) (when (> n 0))
(* n (factorial (- n 1)))))

View file

@ -0,0 +1,7 @@
(defun factorial (n)
(factorial n 1))
(defun factorial
((0 acc) acc)
((n acc) (when (> n 0))
(factorial (- n 1) (* n acc))))

View file

@ -0,0 +1,12 @@
> (lists:map #'factorial/1 (lists:seq 10 20))
(3628800
39916800
479001600
6227020800
87178291200
1307674368000
20922789888000
355687428096000
6402373705728000
121645100408832000
2432902008176640000)

View file

@ -0,0 +1,16 @@
> (lists:foreach
(lambda (x)
(io:format '"~p~n" `(,(factorial x))))
(lists:seq 10 20))
3628800
39916800
479001600
6227020800
87178291200
1307674368000
20922789888000
355687428096000
6402373705728000
121645100408832000
2432902008176640000
ok

View file

@ -0,0 +1,8 @@
define factorial(n) => {
local(x = 1)
with i in generateSeries(2, #n)
do {
#x *= #i
}
return #x
}

View file

@ -0,0 +1 @@
define factorial(n) => #n < 2 ? 1 | #n * factorial(#n - 1)

View file

@ -0,0 +1,4 @@
on fact (n)
if n<=1 then return 1
return n * fact(n-1)
end

View file

@ -0,0 +1,7 @@
on fact (n)
res = 1
repeat with i = 2 to n
res = res*i
end repeat
return res
end

View file

@ -0,0 +1,32 @@
// recursive
function factorialr n
if n < 2 then
return 1
else
return n * factorialr(n-1)
end if
end factorialr
// using accumulator
function factorialacc n acc
if n = 0 then
return acc
else
return factorialacc(n-1, n * acc)
end if
end factorialacc
function factorial n
return factorialacc(n,1)
end factorial
// iterative
function factorialit n
put 1 into f
if n > 1 then
repeat with i = 1 to n
multiply f by i
end repeat
end if
return f
end factorialit

View file

@ -0,0 +1,15 @@
fmod FACTORIAL is
protecting INT .
op undefined : -> Int .
op _! : Int -> Int .
var n : Int .
eq 0 ! = 1 .
eq n ! = if n < 0 then undefined else n * (sd(n, 1) !) fi .
endfm
red 11 ! .

View file

@ -0,0 +1,7 @@
func factorial args: int a : returns: int {
int factorial = a
repeat int i = (a - 1) : i == 0 : i-- {
factorial *= i
}
return factorial
}

View file

@ -0,0 +1,3 @@
proc factorial(x): int =
if x > 0: x * factorial(x - 1)
else: 1

View file

@ -0,0 +1,4 @@
proc factorial(x): int =
result = 1
for i in 1..x+1:
result *= i

View file

@ -0,0 +1 @@
: fact(n) n ifZero: [ 1 ] else: [ n n 1- fact * ] ;

View file

@ -0,0 +1 @@
: fact | i | 1 swap loop: i [ i * ] ;

View file

@ -0,0 +1,4 @@
fun fac(n) type integer->integer
product{{1..n}}
1..10.fac

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,8 @@
global function factorial(integer n)
atom res = 1
while n>1 do
res *= n
n -= 1
end while
return res
end function

View file

@ -0,0 +1,7 @@
Фун Факт(n)
f := 1
для i от 1 до n
f := f * i
кц
Возврат f
Кон Фун

View file

@ -0,0 +1,13 @@
Фун Факт(n)
Если n = 1
Возврат 1
Иначе
Возврат n * Факт(n - 1)
Всё
Кон Фун
Проц Старт()
n := ВводЦел('Введите число (n <= 12) :')
печать 'n! = '
печать Факт(n)
Кон проц

View file

@ -0,0 +1,5 @@
give n
x = fact(n)
see n + " factorial is : " + x
func fact nr if nr = 1 return 1 else return nr * fact(nr-1) ok

View file

@ -0,0 +1,22 @@
11100000000000100000000000000000 0. -7 to c
10101000000000010000000000000000 1. Sub. 21
10100000000001100000000000000000 2. c to 5
10100000000000100000000000000000 3. -5 to c
10100000000001100000000000000000 4. c to 5
00000000000000000000000000000000 5. generated at run time
00000000000001110000000000000000 6. Stop
00010000000000100000000000000000 7. -8 to c
11111111111111111111111111111111 8. -1
11111111111111111111111111111111 9. -1
01111111111111111111111111111111 10. -2
01011111111111111111111111111111 11. -6
00010111111111111111111111111111 12. -24
00010001111111111111111111111111 13. -120
00001100101111111111111111111111 14. -720
00001010001101111111111111111111 15. -5040
00000001010001101111111111111111 16. -40320
00000001011011100101111111111111 17. -362880
00000000100001010001001111111111 18. -3628800
00000000110101110111100110111111 19. -39916800
00000000001000001100111011000111 20. -479001600
01010000000000000000000000000000 21. 10

View file

@ -0,0 +1 @@
factorial(n) := product(1 ... n);

View file

@ -0,0 +1 @@
factorials(n)[i] := product(1 ... i) foreach i within 1 ... n;

View file

@ -0,0 +1,5 @@
factorial: int -> int;
factorial(n) :=
1 when n <= 0
else
n * factorial(n-1);

View file

@ -0,0 +1,7 @@
factorial(n) :=
factorialHelper(1, n);
factorialHelper(acc, n) :=
acc when n <= 0
else
factorialHelper(acc * n, n-1);

View file

@ -0,0 +1,3 @@
(define factorial
0 -> 1
X -> (* X (factorial (- X 1))))

View file

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

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

View file

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

View file

@ -0,0 +1,9 @@
def factorial (int n)
decl int result
set result 1
decl int i
for (set i 1) (< i (+ n 1)) (inc i)
set result (* result i)
end
return result
end

View file

@ -0,0 +1,8 @@
def factorial (int n)
decl int z
set z 1
if (> n 1)
set z (* n (factorial (- n 1)))
end if
return z
end

View file

@ -0,0 +1,4 @@
def (fact n)
if (n = 0)
1
(n * (fact n-1))

View file

@ -0,0 +1,5 @@
def (fact n)
(n * (fact n-1))
def (fact 0)
1

View file

@ -0,0 +1,4 @@
def (fact n)
ret result 1
for i 1 (i <= n) ++i
result <- result*i

View file

@ -0,0 +1,7 @@
# a useful helper to generate all the natural numbers until n
def (nums n)
collect+for i 1 (i <= n) ++i
yield i
def (fact n)
(reduce (*) nums.n 1)

View file

@ -0,0 +1 @@
@fac 10

View file

@ -0,0 +1 @@
!#~F 10

View file

@ -0,0 +1,3 @@
!/^* @to 10
; or
@prod @to 10

View file

@ -0,0 +1,6 @@
~!10 &n [
@var r 1
@for x to n
:!*r x
r
]

View file

@ -0,0 +1,14 @@
@let {
fac &{fac n}?{
<n 2 n
*n !fac -n 1
}
; memoized
facM @mem &n?{
<n 2 n
*n !facM -n 1
}
[[!fac 10 !facM 10]]
}

View file

@ -0,0 +1,2 @@
def fact:
reduce range(1; .+1) as $i (1; . * $i);

View file

@ -0,0 +1,4 @@
def fact(n):
if n <= 1 then n
else n * fact(n-1)
end;

View file

@ -0,0 +1,8 @@
def fact:
def _fact:
# Input: [accumulator, counter]
if .[1] <= 1 then .
else [.[0] * .[1], .[1] - 1]| _fact
end;
# Extract the accumulated value from the output of _fact:
[1, .] | _fact | .[0] ;