Karim Belabas on Thu, 27 Sep 2007 22:53:21 +0200

 Re: Pari get digit method?

```* Lonnie Cumberland [2007-09-27 22:32]:
> Greetings all,
>
> I am still a little new to using Pari and need to write a small script
> that will allow me to make an integer calculation after which I need
> to get the value of the last digit in the number so that I can take an
> action based upon that value which would be between 0 and 9.
>
> Does someone know how I might be able to do this?

last_digit(n) = n % 10

More generally:

\\ coeff of 10^k
kth_digit(n, k) = (n \ 10^k) % 10

[ generalization to different bases left to the reader ]

> I would like to use a switch-case to take action based upon the value.

There is no 'switch' in PARI, you will have to live with if().

You can also experiment with more advanced ideas like

case1() = 1
case2() = 2
case3() = 3
case4() = 4
\\ ...
fun(n) = eval( Str("case", n % 10, "()") )

But it is probably overkill in the case you describe.

Cheers,

K.B.
--
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]

```