# PARI/GP

Contents

## PARI/GP Etymology

PARI has nothing to do with the French capital. The name is a pun about the project’s early stages when the authors started to implement a library for “Pascal ARIthmetic” in the PASCAL programming language. They quickly switched to C.1

For the benefit of non-native French speakers, here’s a slightly expanded explanation: Blaise Pascal (1623-1662) was a famous French mathematician and philosopher who was one of the founders of probability and devised one of the first “arithmetic machines”. He once proposed the following “proof” of the existence of God for the unbelievers: whether He exists or not I lose nothing by believing in Him, whereas if He does and I misbehave… This is the so-called “pari de Pascal” (Pascal’s Wager).

The first version of GP was originally called GPC, for Great Programmable Calculator. For some reason, the trailing C was eventually dropped.2

## PARI/GP Defaults

### realbitprecision

gp > default(realbitprecision)
%1 = 128

### realprecision

gp > default(realprecision)
%1 = 38
gp > default(realprecision, 100)
gp > default(realprecision)
%3 = 115

To do: Why does default(realprecision, 100) set realprecision to 115?

### timer

gp > default(timer)
%1 = 0
gp > default(timer, 1)
gp > default(timer)
%3 = 1

## PARI/GP Functions

### digits($$x$$, {$$b$$ = $$10$$})

3.6.34 digits($$x$$, {$$b$$ = $$10$$}). Outputs the vector of the digits of |$$x$$| in base $$b$$, where $$x$$ and $$b$$ are integers ($$b$$ = 10 by default). See fromdigits for the reverse operation.

gp > digits(123)
%1 = [1, 2, 3]
$$\space$$
gp > digits(10)
%2 = [1, 0]
$$\space$$
gp > digits(10, 2) \\ base 2
%3 = [1, 0, 1, 0]

By convention, 0 has no digits.

gp > digits(0)
%1 = []

### divisors($$x$$, {flag = 0})

3.8.26 divisors(x, {flag = 0}). Creates a row vector whose components are the divisors of x. The factorization of x (as output by factor) can be used instead. If flag = 1, return pairs [d; factor(d)].

gp > divisors(12)
%1 = [1, 2, 3, 4, 6, 12]
$$\space$$
gp > divisors(12, 1) \\ include their factorization
%2 = [[1, matrix(0,2)], [2, Mat([2, 1])], [3, Mat([3, 1])], [4, Mat([2, 2])], [6, [2, 1; 3, 1]], [12, [2, 2; 3, 1]]]
$$\space$$
gp > divisors(x^4 + 2*x^3 + x^2) \\ also works for polynomials
%3 = [1, x, x^2, x + 1, x^2 + x, x^3 + x^2, x^2 + 2*x + 1, x^3 + 2*x^2 + x, x^4 + 2*x^3 + x^2]

### floor($$x$$)

3.6.36 floor($$x$$). Floor of $$x$$. When $$x$$ is in $$\mathbb{R}$$, the result is the largest integer smaller than or equal to $$x$$. Applied to a rational function, floor($$x$$) returns the Euclidean quotient of the numerator by the denominator.

### frac($$x$$)

3.6.37 frac($$x$$). Fractional part of $$x$$. Identical to $$x$$ floor($$x$$). If $$x$$ is real, the result is in $$[0, 1[$$.

### fromdigits($$x$$, {$$b$$ = $$10$$})

3.6.38 fromdigits($$x$$, {$$b$$ = 10}). Gives the integer formed by the elements of $$x$$ seen as the digits of a number in base $$b$$ ($$b$$ = 10 by default). This is the reverse of digits.

### length($$x$$)

3.6.40 length($$x$$). Length of $$x$$; #$$x$$ is a shortcut for length($$x$$). This is mostly useful for

• vectors: dimension (0 for empty vectors),
• lists: number of entries (0 for empty lists),
• matrices: number of columns,
• character strings: number of actual characters (without trailing \0, should you expect it from C char*).
gp > length(“a string”)
%1 = 8
gp > gp > #”a string”
%2 = 8
gp > length([3, 2, 1])
%3 = 3
gp > #[3, 2, 1]
%4 = 3
gp > length([])
%5 = 0
gp > #[]
%6 = 0
gp > length(matrix(2, 5))
%7 = 5
gp > #matrix(2, 5)
%8 = 5
gp > L = List([1, 2, 3, 4]); length(L)
%9 = 4
gp > L = List([1, 2, 3, 4]); #L
%10 = 4

### precision($$x$$, {$$n$$})

3.6.49 precision($$x$$, {$$n$$}).

### random({$$n$$ = $$2^{31}$$})

3.6.50 random({$$n$$ = $$2^{31}$$}).

### round($$x$$, {&$$e$$})

3.6.52 round($$x$$, {&$$e$$}).

### simplify($$x$$)

3.6.55 simplify($$x$$).

### truncate($$x$$, {&$$e$$})

3.6.58 truncate($$x$$, {&$$e$$}).

### variables({$$x$$})

3.6.62 variables({$$x$$}).

## Footnotes

1. GP Man Page“. PARI/GP.
2. GP Man Page“. PARI/GP.