1 !**********************************************
2 !* This program allows user to perform combi- *
3 !* natory analysis, such as Factorial N, *
4 !* Combination C(n,p) and Permutation A(n,p). *
5 !* ------------------------------------------ *
6 !* Ref.: "Mathématiques en Turbo-Pascal By *
7 !* M. Ducamp and A. Reverchon (2), *
8 !* Eyrolles, Paris, 1988". *
9 !* ------------------------------------------ *
10 !* Sample runs: *
11 !* *
12 !* COMBINATORY ANALYSIS *
13 !* *
14 !* 1: Factorial n! *
15 !* 2: Combination C n,p *
16 !* 3: Permutation A n,p *
17 !* 0: Quit *
18 !* *
19 !* Your choice (0 to 3): 1 *
20 !* *
21 !* N = 100 *
22 !* N! = 9.3248476268 10^ 157 *
23 !* *
24 !* Your choice (0 to 3): 2 *
25 !* *
26 !* N = 7 *
27 !* P = 3 *
28 !* Cnp = 35 *
29 !* *
30 !* Your choice (0 to 3): 3 *
31 !* *
32 !* N = 10 *
33 !* P = 6 *
34 !* Anp = 151200 *
35 !* *
36 !* F90 version by J-P Moreau *
37 !* (www.jpmoreau.fr) *
38 !**********************************************
39 PROGRAM COMBI_ANALYSIS
40 implicit none ! Take i,j,k,l,m,n as integer and other as real
41 INTEGER p, ichoice, n
42 real*4 Anp, Cnp, Factorial, R1, r2, s !real*4 single precision=precision 7digits
43 ichoice=1
44 do while(ichoice.ne.0)
45 print *,''
46 print *,' COMBINATORY ANALYSIS'
47 print *,''
48 print *,' 1: Factorial n!'
49 print *,' 2: Combination C n,p'
50 print *,' 3: Permutation A n,p'
51 print *,' 0: Quit'
52 print *,''
53 write(*,100,advance='no'); read *, ichoice
54 print *,''
55 if (ichoice<0) ichoice=0
56 if (ichoice>3) ichoice=3
57 if (ichoice.eq.0) goto 90 !quit
58 if (ichoice.eq.1) then !Factorial n
59 write(*,110,advance='no'); read *, n
60 s=Factorial(n,r1,r2)
61 if (s>0) then
62 write(*,150) s
63 else
64 write(*,151) r1, r2
65 endif
66 endif
67 if (ichoice.eq.2) then !Combination n,p
68 write(*,110,advance='no'); read *, n
69 write(*,120,advance='no'); read *, p
70 s=Cnp(n,p) !=Anp(n,p)/p!
71 if (s>0.) then
72 write(*,160) s
73 else
74 write(*,161)
75 endif
76 endif
77 if (ichoice.eq.3) then !Permutation n,p
78 write(*,110,advance='no'); read *, n
79 write(*,120,advance='no'); read *, p
80 s=Anp(n,p)
81 if (s>0.) then
82 write(*,170) s
83 else
84 write(*,161)
85 endif
86 endif
87 print *,''
88 Pause ' Hit any key to continue...'
89 enddo
90 90 stop
91
92 100 format(' Your choice (0 to 3): ')
93 110 format(' N = ')
94 120 format(' P = ')
95
96 150 format(' N! =',F12.0)
97 151 format(' N! =',F12.10,' 10^',F4.0)
98
99 160 format(' Cnp =',F12.0)
100 161 format(' Quantity impossible to evaluate')
101
102 170 format(' Anp =',F12.0)
103
104 END
105
106 !Factorial n
107 real*4 Function Factorial(n,mantissa,exponent)
108 implicit none
109 real*4, parameter :: PI=3.1415926535
110 REAL*4 exponent, mantissa, fa
111 integer i, n
112 fa=1; Factorial=0;
113 if (n<25) then ! i think FORTRAN can't handle such a big number 25! and more
114 do i=1, n
115 fa=fa*i
116 end do
117 Factorial=fa
118 else
119 fa=(LOG(2.*PI*n)/2. + n*LOG(FLOAT(n))-n)/LOG(10.)
120 exponent=INT(fa); mantissa=EXP((fa-INT(fa))*LOG(10.))
121 endif
122 End
123
124 !Comnination C n,p
125 real*4 Function Cnp(n,p)
126 implicit none
127 INTEGER n, p
128 REAL*4 a, b
129 real*4 Anp, Factorial
130 Cnp=Anp(n,p)/Factorial(p,a,b)
131 End
132
133 !Permutation A n,p
134 Real*4 Function Anp(n,p)
135 implicit none
136 real*4, parameter :: REALMAX=1E30
137 INTEGER n, p, i
138 real*4 r
139 Anp=0.
140 if (p>n) return
141 r=1.
142 do i=n-p+1, n
143 if (r>REALMAX/i) return
144 r=r*i
145 end do
146 Anp=r
147 End
148
149
150 !end of file combi.pas