-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcombin2.f90
More file actions
74 lines (62 loc) · 1.18 KB
/
combin2.f90
File metadata and controls
74 lines (62 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
subroutine combin2 ( n, k, icnk )
!*****************************************************************************80
!
!! COMBIN2 computes the binomial coefficient C(N,K).
!
! Discussion:
!
! The value is calculated in such a way as to avoid overflow and
! roundoff. The calculation is done in integer arithmetic.
!
! The formula used is:
!
! C(N,K) = N! / ( K! * (N-K)! )
!
! Licensing:
!
! This code is distributed under the GNU LGPL license.
!
! Modified:
!
! 17 January 1999
!
! Author:
!
! John Burkardt
!
! Reference:
!
! ML Wolfson, HV Wright,
! Algorithm 160:
! Combinatorial of M Things Taken N at a Time,
! Communications of the ACM,
! April, 1963.
!
! Parameters:
!
! Input, integer N, K, are the values of N and K.
!
! Output, integer ICNK, the number of combinations of N
! things taken K at a time.
!
implicit none
integer i
integer icnk
integer k
integer mn
integer mx
integer n
mn = min ( k, n - k )
if ( mn < 0 ) then
icnk = 0
else if ( mn == 0 ) then
icnk = 1
else
mx = max ( k, n - k )
icnk = mx + 1
do i = 2, mn
icnk = ( icnk * ( mx + i ) ) / i
end do
end if
return
end