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

@ -0,0 +1,41 @@
// version 1.1.2
import java.math.BigInteger
fun perm(n: Int, k: Int): BigInteger {
require(n > 0 && k >= 0)
return (n - k + 1 .. n).fold(BigInteger.ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong()) }
}
fun comb(n: Int, k: Int): BigInteger {
require(n > 0 && k >= 0)
val fact = (2..k).fold(BigInteger.ONE) { acc, i -> acc * BigInteger.valueOf(i.toLong()) }
return perm(n, k) / fact
}
fun main(args: Array<String>) {
println("A sample of permutations from 1 to 12:")
for (n in 1..12) System.out.printf("%2d P %-2d = %d\n", n, n / 3, perm(n, n / 3))
println("\nA sample of combinations from 10 to 60:")
for (n in 10..60 step 10) System.out.printf("%2d C %-2d = %d\n", n, n / 3, comb(n, n / 3))
println("\nA sample of permutations from 5 to 15000:")
val na = intArrayOf(5, 50, 500, 1000, 5000, 15000)
for (n in na) {
val k = n / 3
val s = perm(n, k).toString()
val l = s.length
val e = if (l <= 40) "" else "... (${l - 40} more digits)"
System.out.printf("%5d P %-4d = %s%s\n", n, k, s.take(40), e)
}
println("\nA sample of combinations from 100 to 1000:")
for (n in 100..1000 step 100) {
val k = n / 3
val s = comb(n, k).toString()
val l = s.length
val e = if (l <= 40) "" else "... (${l - 40} more digits)"
System.out.printf("%4d C %-3d = %s%s\n", n, k, s.take(40), e)
}
}

View file

@ -0,0 +1,6 @@
comb := proc (n::integer, k::integer)
return factorial(n)/(factorial(k)*factorial(n-k));
end proc;
perm := proc (n::integer, k::integer)
return factorial(n)/factorial(n-k);
end proc;

View file

@ -0,0 +1,24 @@
(define (combinations n k)
(do ((i 0 (+ 1 i))
(res 1 (/ (* res (- n i))
(- k i))))
((= i k) res)))
(define (permutations n k)
(do ((i 0 (+ 1 i))
(res 1 (* res (- n i))))
((= i k) res)))
(display "P(4,2) = ") (display (permutations 4 2)) (newline)
(display "P(8,2) = ") (display (permutations 8 2)) (newline)
(display "P(10,8) = ") (display (permutations 10 8)) (newline)
(display "C(10,8) = ") (display (combinations 10 8)) (newline)
(display "C(20,8) = ") (display (combinations 20 8)) (newline)
(display "C(60,58) = ") (display (combinations 60 58)) (newline)
(display "P(1000,10) = ") (display (permutations 1000 10)) (newline)
(display "P(1000,20) = ") (display (permutations 1000 20)) (newline)
(display "P(15000,2) = ") (display (permutations 15000 3)) (newline)
(display "C(1000,10) = ") (display (combinations 1000 10)) (newline)
(display "C(1000,999) = ") (display (combinations 1000 999)) (newline)
(display "C(1000,1000) = ") (display (combinations 1000 1000)) (newline)
(display "C(15000,14998) = ") (display (combinations 15000 14998)) (newline)

View file

@ -1,17 +1,16 @@
func P(n, k) { n! / ((n-k)!) };
func C(n, k) { binomial(n, k) };
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);
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)));
n < 10 ? (lstirling(n+1) - log(n+1))
 : (0.5*log(2*Num.pi*n) + n*log(n/Num.e + 1/(12*Num.e*n)))
}
func P_approx(n, k) {
@ -22,25 +21,25 @@ 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)}";
say "=> Exact results:"
for n (1..12) {
var p = 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)}";
for n (10..60 `by` 10) {
var p = 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)}";
say "=> Floating point approximations:"
for n ([5, 50, 500, 1000, 5000, 15000]) {
var p = 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)}";
for n (100..1000 `by` 100) {
var p = n//3
say "C(#{n}, #{p}) = #{C_approx(n, p)}"
}

View file

@ -0,0 +1,59 @@
' Combinations and permutations - vbs - 10/04/2017
dim i,j
Wscript.StdOut.WriteLine "-- Long Integer - Permutations - from 1 to 12"
for i=1 to 12
for j=1 to i
Wscript.StdOut.Write "P(" & i & "," & j & ")=" & perm(i,j) & " "
next 'j
Wscript.StdOut.WriteLine ""
next 'i
Wscript.StdOut.WriteLine "-- Float integer - Combinations from 10 to 60"
for i=10 to 60 step 10
for j=1 to i step i\5
Wscript.StdOut.Write "C(" & i & "," & j & ")=" & comb(i,j) & " "
next 'j
Wscript.StdOut.WriteLine ""
next 'i
Wscript.StdOut.WriteLine "-- Float integer - Permutations from 5000 to 15000"
for i=5000 to 15000 step 5000
for j=10 to 70 step 20
Wscript.StdOut.Write "C(" & i & "," & j & ")=" & perm(i,j) & " "
next 'j
Wscript.StdOut.WriteLine ""
next 'i
Wscript.StdOut.WriteLine "-- Float integer - Combinations from 200 to 1000"
for i=200 to 1000 step 200
for j=20 to 100 step 20
Wscript.StdOut.Write "P(" & i & "," & j & ")=" & comb(i,j) & " "
next 'j
Wscript.StdOut.WriteLine ""
next 'i
function perm(x,y)
dim i,z
z=1
for i=x-y+1 to x
z=z*i
next 'i
perm=z
end function 'perm
function fact(x)
dim i,z
z=1
for i=2 to x
z=z*i
next 'i
fact=z
end function 'fact
function comb(byval x,byval y)
if y>x then
comb=0
elseif x=y then
comb=1
else
if x-y<y then y=x-y
comb=perm(x,y)/fact(y)
end if
end function 'comb

View file

@ -0,0 +1,69 @@
' Combinations and permutations - 10/04/2017
Imports System.Numerics 'BigInteger
Module CombPermRc
Sub Main()
Dim i, j As Long
For i = 1 To 12
For j = 1 To i
Console.Write("P(" & i & "," & j & ")=" & PermBig(i, j).ToString & " ")
Next j
Console.WriteLine("")
Next i
Console.WriteLine("--")
For i = 10 To 60 Step 10
For j = 1 To i Step i \ 5
Console.Write("C(" & i & "," & j & ")=" & CombBig(i, j).ToString & " ")
Next j
Console.WriteLine("")
Next i
Console.WriteLine("--")
For i = 5000 To 15000 Step 5000
For j = 4000 To 5000 Step 1000
Console.Write("P(" & i & "," & j & ")=" & PermBig(i, j).ToString("E") & " ")
Next j
Console.WriteLine("")
Next i
Console.WriteLine("--")
For i = 5000 To 15000 Step 5000
For j = 4000 To 5000 Step 1000
Console.Write("C(" & i & "," & j & ")=" & CombBig(i, j).ToString("E") & " ")
Next j
Console.WriteLine("")
Next i
Console.WriteLine("--")
i = 5000 : j = 4000
Console.WriteLine("C(" & i & "," & j & ")=" & CombBig(i, j).ToString)
End Sub 'Main
Function PermBig(x As Long, y As Long) As BigInteger
Dim i As Long, z As BigInteger
z = 1
For i = x - y + 1 To x
z = z * i
Next i
Return (z)
End Function 'PermBig
Function FactBig(x As Long) As BigInteger
Dim i As Long, z As BigInteger
z = 1
For i = 2 To x
z = z * i
Next i
Return (z)
End Function 'FactBig
Function CombBig(ByVal x As Long, ByVal y As Long) As BigInteger
If y > x Then
Return (0)
ElseIf x = y Then
Return (1)
Else
If x - y < y Then y = x - y
Return (PermBig(x, y) / FactBig(y))
End If
End Function 'CombBig
End Module