Changeset fc62b67 in git
- Timestamp:
- Feb 21, 2008, 4:14:44 PM (15 years ago)
- Branches:
- (u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a657104b677b4c461d018cbf3204d72d34ad66a9')
- Children:
- 582c6af89933b00ebd8154123576c0c251aab039
- Parents:
- ec80a283851d3ae4fdc583529b509ea5d8539051
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Singular/LIB/general.lib
rec80a28 rfc62b67 3 3 //eric, added absValue 11.04.2002 4 4 /////////////////////////////////////////////////////////////////////////////// 5 version="$Id: general.lib,v 1.5 4 2007-01-08 09:33:55Singular Exp $";5 version="$Id: general.lib,v 1.55 2008-02-21 15:14:44 Singular Exp $"; 6 6 category="General purpose"; 7 7 info=" … … 12 12 ASCII([n,m]); string of printable ASCII characters (number n to m) 13 13 absValue(c); absolute value of c 14 binomial(n,m[,../..]); n choose m (type int), [type string/type number]14 binomial(n,m[,../..]); n choose m (type int), [type bigint] 15 15 deleteSublist(iv,l); delete entries given by iv from list l 16 factorial(n[,../..]); n factorial (=n!) (type int), [type string/number]16 factorial(n[,../..]); n factorial (=n!) (type int), [type bigint] 17 17 fibonacci(n[,p]); nth Fibonacci number [char p] 18 18 kmemory([n[,v]]); active [allocated] memory in kilobyte … … 170 170 /////////////////////////////////////////////////////////////////////////////// 171 171 172 proc binomial (int n, int k , list #)173 "USAGE: binomial(n,k [,p]); n,k,pintegers172 proc binomial (int n, int k) 173 "USAGE: binomial(n,k); n,k integers 174 174 RETURN: binomial(n,k); binomial coefficient n choose k 175 @* - of type string (computed in characteristic 0) 176 @* binomial(n,k,p); n choose k, computed in characteristic 0 or prime(p) 177 @* - of type number if a basering, say R, is present and p=0=char(R) 178 or if prime(p)=char(R) 179 @* - of type string else 175 @* - of type bigint (computed in characteristic 0) 180 176 NOTE: In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n 181 177 SEE ALSO: prime … … 183 179 " 184 180 { 185 int str,p; 186 //---------------------------- initialization ------------------------------- 187 if ( size(#) == 0 ) 188 { str = 1; 189 ring bin = 0,x,dp; 190 number r=1; 191 } 192 if ( size(#) > 0 ) 193 { 194 p = (#[1]!=0)*prime(#[1]); 195 if ( defined(basering) ) 196 { 197 if ( p == char(basering) ) 198 { number r=1; 199 } 200 else 201 { str = 1; 202 ring bin = p,x,dp; 203 number r=1; 204 } 205 } 206 else 207 { str = 1; 208 ring bin = p,x,dp; 209 number r=1; 210 } 211 } 212 //-------------------------------- char 0 ----------------------------------- 213 if ( p==0 ) 214 { 215 r = binom0(n,k); 216 } 217 //-------------------------------- char p ----------------------------------- 218 else 219 { 220 r = binomp(n,k,p); 221 } 222 //-------------------------------- return ----------------------------------- 223 if ( str==1 ) { return(string(r)); } 224 else { return(r); } 225 } 181 bigint l; 182 bigint r=1; 183 bigint kk=k; 184 bigint nn=n; 185 if ( k > n-k ) 186 { k = n-k; } 187 if ( k<=0 or k>n ) //trivial cases 188 { r = (k==0)*r; } 189 for (l=1; l<=kk; l=l+1 ) 190 { 191 r=r*(nn+1-l)/l; 192 } 193 return(r); 194 } 226 195 example 227 196 { "EXAMPLE:"; echo = 2; 228 197 binomial(200,100);""; //type string, computed in char 0 229 binomial(200,100,3);""; //type string, computed in char 3230 198 int n,k = 200,100; 231 199 ring r = 0,x,dp; … … 233 201 poly b2 = coeffs((x+1)^n,x)[k+1,1]; //coefficient of x^k in (x+1)^n 234 202 b1-b2; //b1 and b2 should coincide 235 }236 ///////////////////////////////////////////////////////////////////////////////237 238 static proc binom0 (int n, int k)239 //computes binomial coefficient n choose k in basering, assume 0<k<=n240 //and char(basering) = 0 or n < char(basering)241 {242 int l;243 number r=1;244 if ( k > n-k )245 { k = n-k;246 }247 if ( k<=0 or k>n ) //trivial cases248 { r = (k==0)*r;249 }250 for (l=1; l<=k; l++ )251 {252 r=r*(n+1-l)/l;253 }254 return(r);255 203 } 256 204 /////////////////////////////////////////////////////////////////////////////// … … 296 244 /////////////////////////////////////////////////////////////////////////////// 297 245 298 proc factorial (int n, list #) 299 "USAGE: factorial(n[,p]); n,p integers 300 RETURN: factorial(n): n! (computed in characteristic 0), of type string. 301 @* factorial(n,p): n! computed in characteristic 0 or prime(p) 302 @* - of type number if a basering is present and 0=p=char(basering) 303 or if prime(p)=char(basering) 304 @* - of type string else 246 proc factorial (int n) 247 "USAGE: factorial(n); n integer 248 RETURN: factorial(n): n! of type bigint. 305 249 SEE ALSO: prime 306 250 EXAMPLE: example factorial; shows an example 307 251 " 308 { int str,l,p; 309 //---------------------------- initialization ------------------------------- 310 if ( size(#) == 0 ) 311 { str = 1; 312 ring bin = 0,x,dp; 313 number r=1; 314 } 315 if ( size(#) > 0 ) 316 { 317 p = (#[1]!=0)*prime(#[1]); 318 if ( defined(basering) ) 319 { 320 if ( p == char(basering) ) 321 { number r=1; 322 } 323 else 324 { str = 1; 325 ring bin = p,x,dp; 326 number r=1; 327 } 328 } 329 else 330 { str = 1; 331 ring bin = p,x,dp; 332 number r=1; 333 } 334 } 252 { 253 bigint r=1; 335 254 //------------------------------ computation -------------------------------- 336 255 for (l=2; l<=n; l++) … … 338 257 r=r*l; 339 258 } 340 if ( str==1 ) { return(string(r)); } 341 else { return(r); } 259 return(r); 342 260 } 343 261 example 344 262 { "EXAMPLE:"; echo = 2; 345 factorial(37);""; //37! of type string (as long integer) 346 ring r1 = 0,x,dp; 347 number p = factorial(37,0); //37! of type number, computed in r1 348 p; 349 } 350 /////////////////////////////////////////////////////////////////////////////// 351 352 proc fibonacci (int n, list #) 353 "USAGE: fibonacci(n[,p]); n,p integers 263 factorial(37);""; //37! (as long integer) 264 } 265 /////////////////////////////////////////////////////////////////////////////// 266 267 proc fibonacci (int n) 268 "USAGE: fibonacci(n); n,p integers 354 269 RETURN: fibonacci(n): nth Fibonacci number, f(0)=f(1)=1, f(i+1)=f(i-1)+f(i) 355 @* - computed in characteristic 0, of type string 356 @* fibonacci(n,p): f(n) computed in characteristic 0 or prime(p) 357 @* - of type number if a basering is present and p=0=char(basering) 358 or if prime(p)=char(basering) 359 @* - of type string else 270 @* - computed in characteristic 0, of type bigint 360 271 SEE ALSO: prime 361 272 EXAMPLE: example fibonacci; shows an example 362 273 " 363 { int str,ii,p; 364 //---------------------------- initialization ------------------------------- 365 if ( size(#) == 0 ) 366 { str = 1; 367 ring bin = 0,x,dp; 368 number f,g,h=1,1,1; 369 } 370 if ( size(#) > 0 ) 371 { 372 p = (#[1]!=0)*prime(#[1]); 373 if ( defined(basering) ) 374 { 375 if ( p == char(basering) ) 376 { number f,g,h=1,1,1; 377 } 378 else 379 { str = 1; 380 ring bin = p,x,dp; 381 number f,g,h=1,1,1; 382 } 383 } 384 else 385 { str = 1; 386 ring bin = p,x,dp; 387 number f,g,h=1,1,1; 388 } 389 } 274 { 275 bigint f,g,h=1,1,1; 390 276 //------------------------------ computation -------------------------------- 391 277 for (ii=3; ii<=n; ii=ii+1) 392 278 { 393 279 h = f+g; f = g; g = h; 394 } 395 if ( str==1 ) { return(string(h)); } 396 else { return(h); } 280 } 281 return(h); 397 282 } 398 283 example
Note: See TracChangeset
for help on using the changeset viewer.