Just another update

This commit is contained in:
Ingy döt Net 2015-02-20 00:35:01 -05:00
parent a25938f123
commit 00a190b0a6
6591 changed files with 94363 additions and 23227 deletions

View file

@ -0,0 +1,43 @@
#include <stdio.h>
#include <limits.h>
/* We go to some effort to handle overflow situations */
static unsigned long gcd_ui(unsigned long x, unsigned long y) {
unsigned long t;
if (y < x) { t = x; x = y; y = t; }
while (y > 0) {
t = y; y = x % y; x = t; /* y1 <- x0 % y0 ; x1 <- y0 */
}
return x;
}
unsigned long binomial(unsigned long n, unsigned long k) {
unsigned long d, g, r = 1;
if (k == 0) return 1;
if (k == 1) return n;
if (k >= n) return (k == n);
if (k > n/2) k = n-k;
for (d = 1; d <= k; d++) {
if (r >= ULONG_MAX/n) { /* Possible overflow */
unsigned long nr, dr; /* reduced numerator / denominator */
g = gcd_ui(n, d); nr = n/g; dr = d/g;
g = gcd_ui(r, dr); r = r/g; dr = dr/g;
if (r >= ULONG_MAX/nr) return 0; /* Unavoidable overflow */
r *= nr;
r /= dr;
n--;
} else {
r *= n--;
r /= d;
}
}
return r;
}
int main() {
printf("%lu\n", binomial(5, 3));
printf("%lu\n", binomial(40, 19));
printf("%lu\n", binomial(67, 31));
return 0;
}

View file

@ -0,0 +1,9 @@
binomial_coefficient = (n, k) ->
result = 1
for i in [0...k]
result *= (n - i) / (i + 1)
result
n = 5
for k in [0..n]
console.log "binomial_coefficient(#{n}, #{k}) = #{binomial_coefficient(n,k)}"

View file

@ -1,8 +1,8 @@
T binomial(T)(in T n, T k) pure /*nothrow*/ {
T binomial(T)(in T n, T k) pure nothrow {
if (k > (n / 2))
k = n - k;
T bc = 1;
foreach (T i; cast(T)2 .. k + 1)
foreach (T i; T(2) .. k + 1)
bc = (bc * (n - k + i)) / i;
return bc;
}
@ -10,7 +10,7 @@ T binomial(T)(in T n, T k) pure /*nothrow*/ {
void main() {
import std.stdio, std.bigint;
foreach (d; [[5, 3], [100, 2], [100, 98]])
foreach (const d; [[5, 3], [100, 2], [100, 98]])
writefln("(%3d %3d) = %s", d[0], d[1], binomial(d[0], d[1]));
writeln("(100 50) = ", binomial(100.BigInt, 50.BigInt));
}

View file

@ -0,0 +1,31 @@
program test_choose
implicit none
write (*, '(i0)') choose (5, 3)
contains
function factorial (n) result (res)
implicit none
integer, intent (in) :: n
integer :: res
integer :: i
res = product ((/(i, i = 1, n)/))
end function factorial
function choose (n, k) result (res)
implicit none
integer, intent (in) :: n
integer, intent (in) :: k
integer :: res
res = factorial (n) / (factorial (k) * factorial (n - k))
end function choose
end program test_choose

View file

@ -0,0 +1,5 @@
coeffs = iterate next [1]
where
next ns = zipWith (+) (0:ns) $ ns ++ [0]
main = print $ coeffs !! 5 !! 3

View file

@ -1,17 +1,18 @@
public class Binom {
public class Binomial
{
private static long binomial(int n, int k)
{
if (k>n-k)
k=n-k;
static long combinations(int n, int k) {
long coeff = 1;
for (int i = n - k + 1; i <= n; i++) {
coeff *= i;
}
for (int i = 1; i <= k; i++) {
coeff /= i;
}
return coeff;
}
long b=1;
for (int i=1, m=n; i<=k; i++, m--)
b=b*m/i;
return b;
}
public static void main(String[] args){
System.out.println(combinations(5, 3));
public static void main(String[] args)
{
System.out.println(binomial(5, 3));
}
}

View file

@ -0,0 +1,17 @@
public class Binomial
{
private static long binom(int n, int k)
{
if (k==0)
return 1;
else if (k>n-k)
return binom(n, n-k);
else
return binom(n-1, k-1)*n/k;
}
public static void main(String[] args)
{
System.out.println(binom(5, 3));
}
}

View file

@ -0,0 +1,22 @@
binomial_coefficients:
procedure options (main);
declare (n, k) fixed;
get (n, k);
put (coefficient(n, k));
coefficient: procedure (n, k) returns (fixed decimal (15));
declare (n, k) fixed;
return (fact(n)/ (fact(n-k) * fact(k)) );
end coefficient;
fact: procedure (n) returns (fixed decimal (15));
declare n fixed;
declare i fixed, f fixed decimal (15);
f = 1;
do i = 1 to n;
f = f * i;
end;
return (f);
end fact;
end binomial_coefficients;

View file

@ -1,3 +1,2 @@
sub infix:<choose>($n, $k) { ([*] $n-$k+1 .. $n) / [*] 2 .. $k }
sub infix:<choose> { [*] ($^n ... 0) Z/ 1 .. $^p }
say 5 choose 3;

View file

@ -0,0 +1,8 @@
sub binomial {
use bigint;
my ($r, $n, $k) = (1, @_);
for (1 .. $k) { $r *= $n--; $r /= $_ }
$r;
}
print binomial(5, 3);

View file

@ -0,0 +1,5 @@
sub binomial {
use bigint;
my($n,$k) = @_;
(0+$n)->bnok($k);
}

View file

@ -0,0 +1,2 @@
use ntheory qw/binomial/;
print length(binomial(100000,50000)), "\n";

View file

@ -1,8 +0,0 @@
sub binomial {
use bigint;
my ($r, $n, $k) = (1, @_);
for (1 .. $k) { $r *= $n + 1 - $_; $r /= $_ }
$r;
}
print binomial(30, 13);

View file

@ -1,3 +1,7 @@
(de binomial (N K)
(let f '((N) (apply * (range 1 N)))
(/ (f N) (* (f (- N K)) (f K))) ) )
(let f
'((N)
(if (=0 N) 1 (apply * (range 1 N))) )
(/
(f N)
(* (f (- N K)) (f K)) ) ) )

View file

@ -0,0 +1 @@
choose(5,3)

View file

@ -1,12 +1,9 @@
/*REXX program calculates binomial coefficients (aka, combinations). */
numeric digits 100000
parse arg n k .
say 'combinations('n","k') =' comb(n,k)
numeric digits 100000 /*allow some gihugeic numbers. */
parse arg n k . /*get N and K from the C.L.*/
say 'combinations('n","k')=' comb(n,k) /*display the result to terminal.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COMB subroutine─────────────────────*/
comb: procedure; parse arg x,y; return fact(x) / (fact(x-y) * fact(y))
comb: procedure; parse arg x,y; return fact(x) % (fact(x-y) * fact(y))
/*──────────────────────────────────FACT subroutine─────────────────────*/
fact: procedure; parse arg z; !=1; do j=2 to z; !=!*j; end; return !
fact: procedure; !=1; do j=2 to arg(1); !=!*j; end; return !

View file

@ -0,0 +1,9 @@
/*REXX program calculates binomial coefficients (aka, combinations). */
numeric digits 100000 /*allow some gihugeic numbers. */
parse arg n k . /*get N and K from the C.L.*/
say 'combinations('n","k')=' comb(n,k) /*display the result to terminal.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────COMB subroutine─────────────────────*/
comb: procedure; parse arg x,y; return pfact(x-y+1,x) % pfact(2,y)
/*──────────────────────────────────PFACT subroutine────────────────────*/
pfact: procedure; !=1; do j=arg(1) to arg(2); !=!*j; end; return !

View file

@ -1 +1,8 @@
The Binomial Coefficient of 5 and 3 equals 10.
object Binomial extends App {
def binomialCoefficient(n: Int, k: Int) =
(BigInt(n - k + 1) to n).product /
(BigInt(1) to k).product
val Array(n, k) = args.map(_.toInt)
println("The Binomial Coefficient of %d and %d equals %,3d.".format(n, k, binomialCoefficient(n, k)))
}

View file

@ -1,8 +1,6 @@
object Binomial extends App {
def binomialCoefficient(n: Int, k: Int) =
(BigInt(n - k + 1) to n).product /
(BigInt(1) to k).product
val Array(n, k) = args.map(_.toInt)
println("The Binomial Coefficient of %d and %d equals %,3d.".format(n, k, binomialCoefficient(n, k)))
}
def bico(n: Long, k: Long): Long = (n, k) match {
case (n, 0) => 1
case (0, k) => 0
case (n, k) => bico(n - 1, k - 1) + bico(n - 1, k)
}
println("bico(5,3) = " + bico(5, 3))

View file

@ -0,0 +1,12 @@
(define (factorial n)
(define (*factorial n acc)
(if (zero? n)
acc
(*factorial (- n 1) (* acc n))))
(*factorial n 1))
(define (choose n k)
(/ (factorial n) (* (factorial k) (factorial (- n k)))))
(display (choose 5 3))
(newline)

View file

@ -0,0 +1,14 @@
$ include "seed7_05.s7i";
const proc: main is func
local
var integer: n is 0;
var integer: k is 0;
begin
for n range 0 to 66 do
for k range 0 to n do
write(n ! k <& " ");
end for;
writeln;
end for;
end func;

View file

@ -0,0 +1,26 @@
const func bigInteger: (in bigInteger: n) ! (in var bigInteger: k) is func
result
var bigInteger: binom is 0_;
local
var bigInteger: numerator is 0_;
var bigInteger: denominator is 0_;
begin
if n >= 0_ and k > n >> 1 then
k := n - k;
end if;
if k < 0_ then
binom := 0_;
elsif k = 0_ then
binom := 1_;
else
binom := n;
numerator := pred(n);
denominator := 2_;
while denominator <= k do
binom *:= numerator;
binom := binom div denominator;
decr(numerator);
incr(denominator);
end while;
end if;
end func;

View file

@ -1,26 +0,0 @@
$ include "seed7_05.s7i";
const func integer: binomial (in integer: n, in var integer: k) is func
result
var integer: binomial is 0;
local
var integer: l is 0;
begin
if n >= k then
if k > n - k then
k := n - k; # Optimization
end if;
binomial := 1;
l := 0;
while l < k do
binomial *:= n - l;
incr(l);
binomial := binomial div l;
end while;
end if;
end func;
const proc: main is func
begin
writeln("binomial coefficient of (5, 3) is " <& binomial(5, 3));
end func;