/********************************************* * This program allows user performing combi- * * natory analysis, such as Factorial N, * * Combination C(n,p) and Permutation A(n,p). * * ------------------------------------------ * * Ref.: "Mathématiques 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 * * * * C++ Release 1.1 By J-P Moreau * * (www.jpmoreau.fr) * * ------------------------------------------ * * Release 1.1: bug corrected in function * * Cnp(). * *********************************************/ #include #include #define MAXREAL 1E40 #define PI 3.1415926535 int n,p,choice; float r1,r2,s; //Factorial n! float Factorial(int n, float *mantissa, float *exponent) { int i; float fa; fa=1; if (n<25) { for (i=1; i<=n; i++) fa=fa*i; return fa; } else { fa=(float) ((log(2*PI*n)/2 + n*log(n)-n)/log(10)); *exponent=(float) int(fa); *mantissa=(float) exp((fa-int(fa))*log(10)); return 0; } } //Combination Cn,p float Cnp(int n,int p) { int i,u; float r,x; if (p>n) return 0; r=1; if (p>n-p) u=n-p; else u=p; for (i=0; iMAXREAL/x) return 0; r=r*x; } return r; } //Permutation An,p float Anp(int n,int p) { int i; float r; if (p>n) return 0; r=1; for (i=n-p+1; i<=n; i++) { if (r>MAXREAL/i) return 0; r=r*i; } return r; } void main() { choice=1; do { printf("\n COMBINATORY ANALYSIS\n\n"); printf(" 1: Factorial n!\n"); printf(" 2: Combination C n,p\n"); printf(" 3: Permutation A n,p\n"); printf(" 0: Quit\n\n"); printf(" Your choice (0 to 3): "); scanf("%d",&choice); printf("\n"); if (choice<0) choice=0; if (choice>3) choice=3; switch(choice) { case 0: goto e100; //quit case 1: { //Factorial n printf(" N = "); scanf("%d",&n); s=Factorial(n,&r1,&r2); printf(" N! = "); if (s>0) printf("%12.0f\n",s); else printf("%12.0f 10^%4.0f\n",r1,r2); break; } case 2: { //Combination n,p printf(" N = "); scanf("%d",&n); printf(" P = "); scanf("%d",&p); s=Cnp(n,p); printf(" Cnp = "); if (s>0) printf("%12.0f\n",s); else printf(" Quantity impossible to evaluate\n"); break; } case 3: { //Permutation n,p printf(" N = "); scanf("%d",&n); printf(" P = "); scanf("%d",&p); s=Anp(n,p); printf(" Anp = "); if (s>0) printf("%12.0f\n",s); else printf(" Quantity impossible to evaluate\n"); } } //end switch } while (choice!=0); e100:;//quit } // end of file combi.cpp