Changeset f937e2 in git for Singular/LIB/general.lib


Ignore:
Timestamp:
Jun 7, 1999, 7:25:07 PM (24 years ago)
Author:
Hans Schönemann <hannes@…>
Branches:
(u'jengelh-datetime', 'ceac47cbc86fe4a15902392bdbb9bd2ae0ea02c6')(u'spielwiese', 'a800fe4b3e9d37a38c5a10cc0ae9dfa0c15a4ee6')
Children:
445434af88451a04abc772d0ad55d79c18a769fa
Parents:
47a6aecfda7eb8cdd122babe0e3446aafc41cf0d
Message:
*greuel: binomial, factorial, fibonacci


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

Legend:

Unmodified
Added
Removed
  • Singular/LIB/general.lib

    r47a6aec rf937e2  
    1 // $Id: general.lib,v 1.10 1999-04-19 11:02:10 obachman Exp $
     1// $Id: general.lib,v 1.11 1999-06-07 17:25:07 Singular Exp $
    22//system("random",787422842);
    33//GMG, last modified 30.9.98
    44///////////////////////////////////////////////////////////////////////////////
    55
    6 version="$Id: general.lib,v 1.10 1999-04-19 11:02:10 obachman Exp $";
     6version="$Id: general.lib,v 1.11 1999-06-07 17:25:07 Singular Exp $";
    77info="
    88LIBRARY:  general.lib   PROCEDURES OF GENERAL TYPE
     
    140140///////////////////////////////////////////////////////////////////////////////
    141141
    142 proc binomial (int n, int k, list #)
    143 "USAGE:   binomial(n,k[,p/s]); n,k,p integers, s string
    144 RETURN:  binomial(n,k);    binomial coefficient n choose k of type int
    145                            (machine integer, limited size! )
    146          binomial(n,k,p);  n choose k in char p of type string
    147          binomial(n,k,s);  n choose k of type number (s any string), computed
    148                            in char of basering if a basering is defined
     142proc binomial (def n, int k, list #)
     143"USAGE:   binomial(n,k[,p]); n,k,p integers
     144RETURN:  binomial(n,k); binomial coefficient n choose k,
     145         of type number if n is of type number (computed in char(basering)),
     146         of type int if n is of type int (machine integer, limited size!)
     147         binomial(n,k,p);  n choose k in char p, of type string
    149148EXAMPLE: example binomial; shows an example
    150149"
    151150{
    152    if ( size(#)==0 ) { int rr=1; }
     151   if ( size(#)==0 )
     152   {
     153      if ( typeof(n)=="number" ) { number rr=1; }
     154      else { int rr=1; }
     155   }
    153156   if ( typeof(#[1])=="int") { ring bin = #[1],x,dp; number rr=1; }
    154    if ( typeof(#[1])=="string") { number rr=1; }
    155    if ( size(#)==0 or typeof(#[1])=="int" or typeof(#[1])=="string" )
     157   if ( size(#)==0 or typeof(#[1])=="int" )
    156158   {
    157159      def r = rr;
     
    170172{ "EXAMPLE:"; echo = 2;
    171173   int b1 = binomial(10,7); b1;
    172    binomial(37,17,0);
    173    ring t = 31,x,dp;
    174    number b2 = binomial(37,17,""); b2;
    175 }
    176 ///////////////////////////////////////////////////////////////////////////////
    177 
    178 proc factorial (int n, list #)
    179 "USAGE:   factorial(n[,string]);  n integer
    180 RETURN:  factorial(n); string of n! in char 0
    181          factorial(n,s);  n! of type number (s any string), computed in char of
    182          basering if a basering is defined
     174   binomial(137,17,0);
     175   ring t = 0,x,dp;
     176   number b2 = binomial(number(137),17); b2;
     177}
     178///////////////////////////////////////////////////////////////////////////////
     179
     180proc factorial (def n)
     181"USAGE:   factorial(n);  n = integer
     182RETURN:  factorial(n); n! in char 0, of type string if n is of type int
     183         n! of type number, computed in char(basering) if n is of type number
    183184EXAMPLE: example factorial; shows an example
    184185"
    185186{
    186    if ( size(#)==0 ) { ring R = 0,x,dp; poly r=1; }
    187    if ( typeof(#[1])=="string" ) { number r=1; }
    188    if ( size(#)==0 or typeof(#[1])=="string" )
    189    {
    190       int l;
    191       for (l=2; l<=n; l=l+1)
    192       {
    193          r=r*l;
    194       }
    195       if ( size(#)==0 ) { return(string(r)); }
    196       return(r);
    197    }
     187   int t,l;
     188   if ( typeof(n)=="number" ) { number r=1; }
     189   else { ring R = 0,x,dp; number r=1; t=1; }
     190   for (l=2; l<=n; l=l+1)
     191   {
     192      r=r*l;
     193   }
     194   if ( t==1 ) { return(string(r)); }
     195   return(r);
    198196}
    199197example
     
    201199   factorial(37);
    202200   ring r1 = 32003,(x,y,z),ds;
    203    number p = factorial(37,""); p;
    204 }
    205 ///////////////////////////////////////////////////////////////////////////////
    206 
    207 proc fibonacci (int n, list #)
    208 "USAGE:   fibonacci(n[,string]);  (n integer)
    209 RETURN:  fibonacci(n); string of nth Fibonacci number,
     201   number p = factorial(number(37)); p;
     202}
     203///////////////////////////////////////////////////////////////////////////////
     204
     205proc fibonacci (def n)
     206"USAGE:   fibonacci(n);  (n integer)
     207RETURN:  fibonacci(n); nth Fibonacci number,
    210208            f(0)=f(1)=1, f(i+1)=f(i-1)+f(i)
    211          fibonacci(n,s);  nth Fibonacci number of type number (s any string),
    212          computed in characteristic of basering if a basering is defined
     209         of type string if n is of type int
     210         of type number computed in char(basering) if n is of type number
    213211EXAMPLE: example fibonacci; shows an example
    214212"
    215213{
    216    if ( size(#)==0 ) { ring fibo = 0,x,dp; number f=1; }
    217    if ( typeof(#[1])=="string" ) { number f=1; }
    218    if ( size(#)==0 or typeof(#[1])=="string" )
    219    {
    220       number g,h = 1,1; int ii;
    221       for (ii=3; ii<=n; ii=ii+1)
    222       {
    223          h = f+g; f = g; g = h;
    224       }
    225       if ( size(#)==0 ) { return(string(h)); }
    226       return(h);
    227    }
     214   int ii,t;
     215   if ( typeof(n)=="number" ) { number f,g,h=1,1,1; }
     216   else { ring fibo = 0,x,dp; number f,g,h=1,1,1; t=1; }
     217   for (ii=3; ii<=n; ii=ii+1)
     218   {
     219      h = f+g; f = g; g = h;
     220    }
     221   if ( t==1 ) { return(string(h)); }
     222   return(h);
    228223}
    229224example
     
    231226   fibonacci(37);
    232227   ring r = 17,x,dp;
    233    number b = fibonacci(37,""); b;
     228   number b = fibonacci(number(37)); b;
    234229}
    235230///////////////////////////////////////////////////////////////////////////////
Note: See TracChangeset for help on using the changeset viewer.