Changeset fc62b67 in git


Ignore:
Timestamp:
Feb 21, 2008, 4:14:44 PM (16 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'spielwiese', 'fe61d9c35bf7c61f2b6cbf1b56e25e2f08d536cc')
Children:
582c6af89933b00ebd8154123576c0c251aab039
Parents:
ec80a283851d3ae4fdc583529b509ea5d8539051
Message:
*hannes: binomial, factorial, fibonacii return bigint


git-svn-id: file:///usr/local/Singular/svn/trunk@10589 2c84dea3-7e68-4137-9b89-c4e89433aadc
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Singular/LIB/general.lib

    rec80a28 rfc62b67  
    33//eric, added absValue 11.04.2002
    44///////////////////////////////////////////////////////////////////////////////
    5 version="$Id: general.lib,v 1.54 2007-01-08 09:33:55 Singular Exp $";
     5version="$Id: general.lib,v 1.55 2008-02-21 15:14:44 Singular Exp $";
    66category="General purpose";
    77info="
     
    1212 ASCII([n,m]);          string of printable ASCII characters (number n to m)
    1313 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]
    1515 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]
    1717 fibonacci(n[,p]);      nth Fibonacci number [char p]
    1818 kmemory([n[,v]]);      active [allocated] memory in kilobyte
     
    170170///////////////////////////////////////////////////////////////////////////////
    171171
    172 proc binomial (int n, int k, list #)
    173 "USAGE:   binomial(n,k[,p]); n,k,p integers
     172proc binomial (int n, int k)
     173"USAGE:   binomial(n,k); n,k integers
    174174RETURN:  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)
    180176NOTE:    In any characteristic, binomial(n,k) = coefficient of x^k in (1+x)^n
    181177SEE ALSO: prime
     
    183179"
    184180{
    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}
    226195example
    227196{ "EXAMPLE:"; echo = 2;
    228197   binomial(200,100);"";                   //type string, computed in char 0
    229    binomial(200,100,3);"";                 //type string, computed in char 3
    230198   int n,k = 200,100;
    231199   ring r = 0,x,dp;
     
    233201   poly b2 = coeffs((x+1)^n,x)[k+1,1];     //coefficient of x^k in (x+1)^n
    234202   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<=n
    240  //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 cases
    248    { r = (k==0)*r;
    249    }
    250    for (l=1; l<=k; l++ )
    251    {
    252       r=r*(n+1-l)/l;
    253    }
    254    return(r);
    255203}
    256204///////////////////////////////////////////////////////////////////////////////
     
    296244///////////////////////////////////////////////////////////////////////////////
    297245
    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
     246proc factorial (int n)
     247"USAGE:    factorial(n);  n integer
     248RETURN:   factorial(n):   n! of type bigint.
    305249SEE ALSO: prime
    306250EXAMPLE:  example factorial; shows an example
    307251"
    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;
    335254//------------------------------ computation --------------------------------
    336255   for (l=2; l<=n; l++)
     
    338257      r=r*l;
    339258   }
    340    if ( str==1 ) { return(string(r)); }
    341    else { return(r); }
     259   return(r);
    342260}
    343261example
    344262{ "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
     267proc fibonacci (int n)
     268"USAGE:    fibonacci(n);  n,p integers
    354269RETURN:   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
    360271SEE ALSO: prime
    361272EXAMPLE: example fibonacci; shows an example
    362273"
    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;
    390276//------------------------------ computation --------------------------------
    391277   for (ii=3; ii<=n; ii=ii+1)
    392278   {
    393279      h = f+g; f = g; g = h;
    394     }
    395    if ( str==1 ) { return(string(h)); }
    396    else { return(h); }
     280   }
     281   return(h);
    397282}
    398283example
Note: See TracChangeset for help on using the changeset viewer.