21 double bfloor(
double value,
int places)
26 if ( places < 0 ) places = 0;
28 for ( i=0; i<places; i++ ) mult *= 10;
30 return floor(mult*value)/mult;
39 double bround(
double value,
int places)
44 if ( places < 0 ) places = 0;
46 for ( i=0; i<places; i++ ) mult *= 10;
48 return floor(mult*value + 0.5)/mult;
71 error_show(
"Error in factorial", __FILE__, __LINE__);
72 cerr <<
"Factorial of %d exceeds maximum value of a double!" << n << endl;
76 if ( n <= 1 )
return 1;
78 if ( n > 50 )
return exp(lgamma(n+1.0L));
80 for ( fn = 1; n > 1; n-- ) fn *= n;
101 if ( n <= 0 )
return nc;
103 if ( n > 50 ) nc = floor(0.5 + exp(lgamma(n+1.0L) - lgamma(r+1.0L) - lgamma(n-r+1.0L)));
128 while ( i<k && a[i] <= a[k] ) i++;
129 while ( j>k && a[j] >= a[k] ) j--;
132 if ( i == k || j==k ) {
154 long divisor, pn[1024], i;
157 cout <<
"DEBUG prime_factors: for number " << number <<
": ";
159 for ( n=0; number > 1; n++ ) {
161 if ( divisor < 1 )
break;
166 if ( n < 1 )
return NULL;
168 long* prime =
new long[n];
169 for ( i=0; i<n; i++ ) prime[i] = pn[i];
172 for ( i=0; i<n; i++ ) cout <<
" " << pn[i];
194 cout <<
"Zero and one are neither composite nor prime numbers!" << endl;
198 if ( number%2 == 0 )
return 2;
199 if ( number%3 == 0 )
return 3;
203 double max = sqrt((
double)number);
204 for( i=5; i<=max; i+=inc, inc=6-inc)
205 if ( number%i == 0 )
return i;
225 for ( i=len-2; i>=0 && s[i] >= s[i+1]; i-- ) ;
226 if ( i < 0 )
return 0;
228 for ( j=len-1; j>=0 && s[i] >= s[j]; j-- ) ;
231 for ( i++, j=len-1; i<j; i++, j-- ) s = s.
swap(i, j);
234 cout <<
"DEBUG next_permutation: " << s << endl;
253 return 0.5*log((1 + value + vmin)/(1.0 - value + vmin));
268 double betacf(
double a,
double b,
double x)
271 double aa, c, d, del, h, qab, qam, qap;
272 double fpmin(1.0e-30);
282 if (fabs(d) < fpmin) d=fpmin;
285 for (m=1; m<=maxit;m++){
287 aa=m*(b-m)*x/((qam+m2)*(a+m2));
289 if (fabs(d) < fpmin) d=fpmin;
291 if (fabs(c) < fpmin) c=fpmin;
294 aa = -(a+m)*(qab+m)*x/((a+m2)*(qap+m2));
296 if (fabs(d) < fpmin) d=fpmin;
298 if (fabs(c) < fpmin) c=fpmin;
302 if (fabs(del-1.0) < eps)
break;
304 if (m > maxit) cerr <<
"Error: a or b too big, or maxit too small in betacf" << endl;
320 double betai (
double a,
double b,
double x)
324 if(x < 0.0 || x > 1.0) cerr <<
"Error: Bad x in routine betai" << endl;
325 if(x == 0.0 || x == 1.0) bt=0.0;
327 bt=exp(lgamma(a+b)-lgamma(a)-lgamma(b)+a*log(x)+b*log(1.0-x));
328 if(x < (a+1.0)/(a+b+2.0))
329 return bt*
betacf(a,b,x)/a;
331 return 1.0-bt*
betacf(b,a,1.0-x)/b;
348 double t, vfg(v[0]), vbg(v[1]), dvfg(1), dvbg(1), dt(0.001), sumfg, sumbg;
350 for ( i=0; i<n && vfg == vbg; i++ );
352 while ( fabs(dvfg) > dt || fabs(dvbg) > dt ) {
356 for ( i=0; i<n; i++ ) {
365 if ( nfg ) sumfg /= nfg;
366 if ( nbg ) sumbg /= nbg;