'********************************************** '* This program allows user performing combi- * '* natory analysis, such as Factorial N, * '* Combination C(n,p) and Permutation A(n,p). * '* ------------------------------------------ * '* Ref.: "Mathematiques en Turbo-Pascal By * '* M. Ducamp and A. Reverchon (2), * '* Eyrolles, Paris, 1988" [BIBLI 05]. * '* ------------------------------------------ * '* Sample runs: * '* * '* COMBINATORY ANALYSIS * '* * '* 1: Factorial n! * '* 2: Combination C n,p * '* 3: Permutation A n,p * '* 0: Quit * '* * '* Your choice (0 to 3): 1 * '* * '* N = 100 * '* N! = 9.3248476268 10^ 157 * '* * '* Your choice (0 to 3): 2 * '* * '* N = 7 * '* P = 3 * '* Cnp = 35 * '* * '* Your choice (0 to 3): 3 * '* * '* N = 10 * '* P = 6 * '* Anp = 151200 * '* * '* BASIC Release 1.1 By J-P Moreau * '* (www.jpmoreau.fr) * '* ------------------------------------------ * '* Release 1.1: bug corrected in subroutine * '* 1100. * '********************************************** MAXREAL=1E30 PI=3.1415926535 choice%=1 10 cls print print " COMBINATORY ANALYSIS" print print " 1: Factorial n!" print " 2: Combination C n,p" print " 3: Permutation A n,p\n" print " 0: Quit" print input " Your choice (0 to 3): ", choice% print if choice%<0 then choice%=0 if choice%>3 then choice%=3 if choice%=0 then goto 100 'quit if choice%=1 then 'Factorial n input " N = ", n% gosub 1000 : s=Factorial print " N! = "; if s>0 then print using "############"; s else print using "##.############";r1; print " 10^"; : print using "####"; r2 end if end if if Choice%=2 then 'Combination n,p input " N = ", n% input " P = ", p% gosub 1100 : s=Cnp print " Cnp = "; if s>0 then print using "############"; s else print " Quantity impossible to evaluate" end if end if if Choice%=3 then 'Permutation n,p input " N = ", n% input " P = ", p% gosub 1200 : s=Anp print " Anp = "; if s>0 then print using "############"; s else print " Quantity impossible to evaluate" end if end if print input " Hit a key to continue...",r$ if choice%<>0 then goto 10 100 end 'Factorial n! 1000 fa=1 if n%<25 then for i%=1 to n% fa=fa*i% next Factorial=fa else fa=(log(2*PI*n%)/2 + n%*log(n%)-n%)/log(10) r2= int(fa) r1= exp((fa-int(fa))*log(10)) Factorial=0 end if return 'Combination Cn,p 1100 if p%>n% then Cnp=0 : return end if r=1 if p%>n%-p% then u%=n%-p% else u%=p% end if for i%=0 to u%-1 x= (n%-i%)/(u%-i%) if r>MAXREAL/x then return r=r*x next Cnp=r return 'Permutation An,p 1200 if p%>n% then Anp=0 return end if r=1 for i%=n%-p%+1 to n% if r>MAXREAL/i% then Anp=0 return end if r=r*i% next Anp=r return ' end of file combi.bas