Karim Belabas on Mon, 03 Oct 2005 11:30:25 +0200 |
[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]
Re: Help to convert a function |
* Quique Becerra [2005-10-02 17:30]: > Hi people > Can any1 help me to convert a function to PARI ? > It's made in c++, and need help to make > this function work in PARI > > --------------------------------------- > int quick_exp (int a, int b) > { > int z, x, resul; > z = b; > x = a; > resul = 1; > while(z > 0) > { > if(z % 1 == 1) I assume you mean z % 2 here. Otherwise, you may as well return 1 directely. > resul = resul * x; > x = x*x; > z = z/z; I assume you mean z /= 2 here. > } > return(resul); > } > --------------------------------------- In GP: quick_exp(a, b) = a^b This is not exactly equivalent to the above since it does not take overflow into account. If you really intend overflow to take place, then quick_exp(a, b, bits_in_long = 32) = lift(Mod(a, 2^bits_in_long)^b) is suitable. If by "converting to PARI" you mean "converting to a C function using the PARI library" then (assuming pari-2.2.10 is used): long quick_exp(long a, long b) { pari_sp av = avma; long c = itos( gpow(stoi(a), stoi(b), 0) ); avma = av; return c; } or possibly (this one does overflow) ulong quick_exp(ulong a, ulong b) { return upowuu(a, b); } For a direct translation, see leftright_pow(). Karim. -- Karim Belabas Tel: (+33) (0)5 40 00 26 17 Universite Bordeaux 1 Fax: (+33) (0)5 40 00 69 50 351, cours de la Liberation http://www.math.u-bordeaux.fr/~belabas/ F-33405 Talence (France) http://pari.math.u-bordeaux.fr/ [PARI/GP]