1

As part of a mini-project I'm doing, I need a way to reverse the digits of an integer in PARI/GP (the project is mainly on palindromic numbers and digit sums).

What I'm trying to achieve in code form is fun(13453) giving 35431.

For example, I've tried doing the following:

  • Vecrev(digits(13453)) gives out [3,5,4,3,1] but I don't know how to concatenate each consecutive element in this list.

  • I don't think there is a preset function for this, as rev, reverse, or digitrev are not defined in PARI/GP.

Could someone show me how this can be done? Preferably, loops are undesirable as I want a simple function to put into, say, an if statement or a for loop.

Piotr Semenov
  • 1,761
  • 16
  • 24

2 Answers2

3

Or: fromdigits(Vecrev(digits(13453)))

Andrew
  • 873
  • 4
  • 10
1

You can use Polrev instead of Vecrev. After that, just do subst. Your example will be as follows:

> subst(Polrev(digits(13453), 'x), 'x, 10)
35431
Piotr Semenov
  • 1,761
  • 16
  • 24