Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
|
|
@ -0,0 +1,20 @@
|
|||
;; rename native functions according to task
|
||||
(define-syntax-rule (Cnk n k) (Cnp n k))
|
||||
(define-syntax-rule (Ank n k) (Anp n k))
|
||||
|
||||
|
||||
(Cnk 10 1)
|
||||
→ 10
|
||||
(lib 'bigint) ;; no floating point needed : use large integers
|
||||
|
||||
(Cnk 100 10)
|
||||
→ 17310309456440
|
||||
(Cnk 1000 42)
|
||||
→ 297242911333923795640059429176065863139989673213703918037987737481286092000
|
||||
(Ank 10 10)
|
||||
→ 3628800
|
||||
(factorial 10)
|
||||
→ 3628800
|
||||
(Ank 666 42)
|
||||
→ 1029024198692120734765388598788124551227594950478035495578451793852872815678512303375588360
|
||||
1398831219998720000000000000
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
import bigints
|
||||
|
||||
proc perm(n, k: int32): BigInt =
|
||||
result = initBigInt 1
|
||||
var
|
||||
k = n - k
|
||||
n = n
|
||||
while n > k:
|
||||
result *= n
|
||||
dec n
|
||||
|
||||
proc comb(n, k: int32): BigInt =
|
||||
result = perm(n, k)
|
||||
var k = k
|
||||
while k > 0:
|
||||
result = result div k
|
||||
dec k
|
||||
|
||||
echo "P(1000, 969) = ", perm(1000, 969)
|
||||
echo "C(1000, 969) = ", comb(1000, 969)
|
||||
|
|
@ -0,0 +1,27 @@
|
|||
function P(integer n,k)
|
||||
return factorial(n)/factorial(n-k)
|
||||
end function
|
||||
|
||||
function C(integer n,k)
|
||||
return P(n,k)/factorial(k)
|
||||
end function
|
||||
|
||||
function lstirling(atom n)
|
||||
if n<10 then
|
||||
return lstirling(n+1)-log(n+1)
|
||||
end if
|
||||
return 0.5*log(2*PI*n) + n*log(n/E + 1/(12*E*n))
|
||||
end function
|
||||
|
||||
function P_approx(integer n, k)
|
||||
return lstirling(n)-lstirling(n-k)
|
||||
end function
|
||||
|
||||
function C_approx(integer n, k)
|
||||
return lstirling(n)-lstirling(n-k)-lstirling(k)
|
||||
end function
|
||||
|
||||
function to_s(atom v)
|
||||
integer e = floor(v/log(10))
|
||||
return sprintf("%.9ge%d",{power(E,v-e*log(10)),e})
|
||||
end function
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
printf(1,"=> Exact results:\n")
|
||||
for n=1 to 12 do
|
||||
integer p = floor(n/3)
|
||||
printf(1,"P(%d,%d) = %d\n",{n,p,P(n,p)})
|
||||
end for
|
||||
|
||||
for n=10 to 60 by 10 do
|
||||
integer p = floor(n/3)
|
||||
printf(1,"C(%d,%d) = %d\n",{n,p,C(n,p)})
|
||||
end for
|
||||
|
||||
printf(1,"=> Floating point approximations:\n")
|
||||
constant tests = {5, 50, 500, 1000, 5000, 15000}
|
||||
for i=1 to length(tests) do
|
||||
integer n=tests[i], p = floor(n/3)
|
||||
printf(1,"P(%d,%d) = %s\n",{n,p,to_s(P_approx(n,p))})
|
||||
end for
|
||||
|
||||
for n=100 to 1000 by 100 do
|
||||
integer p = floor(n/3)
|
||||
printf(1,"C(%d,%d) = %s\n",{n,p,to_s(C_approx(n,p))})
|
||||
end for
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
func P(n, k) { n! / ((n-k)!) };
|
||||
func C(n, k) { binomial(n, k) };
|
||||
|
||||
class Logarithm(value) {
|
||||
method get_value { value };
|
||||
method to_s {
|
||||
var e = int(value/10.log);
|
||||
"%.8fE%+d" % (exp(value - e*10.log), e);
|
||||
}
|
||||
}
|
||||
|
||||
func lstirling(n) {
|
||||
n < 10 ? (lstirling(n+1) - log(n+1))
|
||||
: (0.5*log(2*Number.pi*n) + n*log(n/Number.e + 1/(12*Number.e*n)));
|
||||
}
|
||||
|
||||
func P_approx(n, k) {
|
||||
Logarithm((lstirling(n) - lstirling(n -k)))
|
||||
}
|
||||
|
||||
func C_approx(n, k) {
|
||||
Logarithm((lstirling(n) - lstirling(n -k) - lstirling(k)))
|
||||
}
|
||||
|
||||
say "=> Exact results:";
|
||||
range(1, 12).each { |n|
|
||||
var p = int(n / 3);
|
||||
say "P(#{n}, #{p}) = #{P(n, p)}";
|
||||
}
|
||||
|
||||
range(10, 60, 10).each { |n|
|
||||
var p = int(n / 3);
|
||||
say "C(#{n}, #{p}) = #{C(n, p)}";
|
||||
}
|
||||
|
||||
say '';
|
||||
say "=> Floating point approximations:";
|
||||
[5, 50, 500, 1000, 5000, 15000].each { |n|
|
||||
var p = int(n / 3);
|
||||
say "P(#{n}, #{p}) = #{P_approx(n, p)}";
|
||||
}
|
||||
|
||||
range(100, 1000, 100).each { |n|
|
||||
var p = int(n / 3);
|
||||
say "C(#{n}, #{p}) = #{C_approx(n, p)}";
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
def permutation(k): . as $n
|
||||
| reduce range($n-k+1; 1+$n) as $i (1; . * $i);
|
||||
|
||||
def combination(k): . as $n
|
||||
| if k > ($n/2) then combination($n-k)
|
||||
else reduce range(0; k) as $i (1; (. * ($n - $i)) / ($i + 1))
|
||||
end;
|
||||
|
||||
# natural log of n!
|
||||
def log_factorial: (1+.) | tgamma | log;
|
||||
|
||||
def log_permutation(k):
|
||||
(log_factorial - ((.-k) | log_factorial));
|
||||
|
||||
def log_combination(k):
|
||||
(log_factorial - ((. - k)|log_factorial) - (k|log_factorial));
|
||||
|
||||
def big_permutation(k): log_permutation(k) | exp;
|
||||
|
||||
def big_combination(k): log_combination(k) | exp;
|
||||
Loading…
Add table
Add a link
Reference in a new issue