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,29 @@
(lib 'math) ;; Σ aka (sigma f(n) nfrom nto)
(define (f-count N (times 100000))
(define count 0)
(for ((i times))
;; new random f mapping from 0..N-1 to 0..N-1
;; (f n) is NOT (random N)
;; because each call (f n) must return the same value
(define f (build-vector N (lambda(i) (random N))))
(define hits (make-vector N))
(define n 0)
(while (zero? [hits n])
(++ count)
(vector+= hits n 1)
(set! n [f n])))
(// count times))
(define (f-anal N)
(Σ (lambda(i) (// (! N) (! (- N i)) (^ N i))) 1 N))
(decimals 5)
(define (f-print (maxN 21))
(for ((N (in-range 1 maxN)))
(define fc (f-count N))
(define fa (f-anal N))
(printf "%3d %10d %10d %10.2d %%" N fc fa (// (abs (- fa fc)) fc 0.01))))

View file

@ -0,0 +1,34 @@
import math, strfmt
randomize()
const
maxN = 20
times = 1_000_000
proc factorial(n): float =
result = 1
for i in 1 .. n:
result *= i.float
proc expected(n): float =
for i in 1 .. n:
result += factorial(n) / pow(n.float, i.float) / factorial(n - i)
proc test(n, times): int =
for i in 1 .. times:
var
x = 1
bits = 0
while (bits and x) == 0:
inc result
bits = bits or x
x = 1 shl random(n)
echo " n\tavg\texp.\tdiff"
echo "-------------------------------"
for n in 1 .. maxN:
let cnt = test(n, times)
let avg = cnt.float / times
let theory = expected(n)
let diff = (avg / theory - 1) * 100
printlnfmt "{:2} {:8.4f} {:8.4f} {:6.3f}%", n, avg, theory, diff

View file

@ -0,0 +1,33 @@
constant MAX = 20,
ITER = 1000000
function expected(integer n)
atom sum = 0
for i=1 to n do
sum += factorial(n) / power(n,i) / factorial(n-i)
end for
return sum
end function
function test(integer n)
integer count = 0, x, bits
for i=1 to ITER do
x = 1
bits = 0
while not and_bits(bits,x) do
count += 1
bits = or_bits(bits,x)
x = power(2,rand(n)-1)
end while
end for
return count/ITER
end function
atom av, ex
puts(1," n avg. exp. (error%)\n");
puts(1,"== ====== ====== ========\n");
for n=1 to MAX do
av = test(n)
ex = expected(n)
printf(1,"%2d %8.4f %8.4f (%5.3f%%)\n", {n,av,ex,abs(1-av/ex)*100})
end for