There are many ways to use PARI/GP:
57!
40526919504877216755680601905432322134980384796226602145184481280000000000000
2 / 6
1/3
(1+I)^2
2*I
(x+1)^(-2)
1/(x^2 + 2*x + 1)
Mod(2,5)^3
Mod(3, 5)
Mod(x, x^2+x+1)^3
Mod(1, x^2 + x + 1)
a = ffgen([3,5],'a)
a^12 \\ in F_3^5
2*a^4 + 2*a^3 + 2
Pi
3.1415926535897932384626433832795028842
log(2)
0.69314718055994530941723212145817656807
\p100
realprecision = 115 significant digits (100 digits displayed)
log(2)
0.6931471805599453094172321214581765680755001343602552541206800094933936219696947156058633269964186875
exp(%)
2.000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
log(1+x)
x - 1/2*x^2 + 1/3*x^3 - 1/4*x^4 + 1/5*x^5 - 1/6*x^6 + 1/7*x^7 - 1/8*x^8 + 1/9*x^9 - 1/10*x^10 + 1/11*x^11 - 1/12*x^12 + 1/13*x^13 - 1/14*x^14 + 1/15*x^15 + O(x^16)
exp(%)
1 + x + O(x^16)
?
Help topics: for a list of relevant subtopics, type ?n for n in 0: user-defined functions (aliases, installed and user functions) 1: PROGRAMMING under GP 2: Standard monadic or dyadic OPERATORS 3: CONVERSIONS and similar elementary functions 4: functions related to COMBINATORICS 5: NUMBER THEORETICAL functions 6: POLYNOMIALS and power series 7: Vectors, matrices, LINEAR ALGEBRA and sets 8: TRANSCENDENTAL functions 9: SUMS, products, integrals and similar functions 10: General NUMBER FIELDS 11: Associative and central simple ALGEBRAS 12: ELLIPTIC CURVES 13: L-FUNCTIONS 14: MODULAR FORMS 15: MODULAR SYMBOLS 16: GRAPHIC functions 17: The PARI community Also: ? functionname (short on-line help) ?\ (keyboard shortcuts) ?. (member functions) Extended help (if available): ?? (opens the full user's manual in a dvi previewer) ?? tutorial / refcard / libpari (tutorial/reference card/libpari manual) ?? refcard-ell (or -lfun/-mf/-nf: specialized reference card) ?? keyword (long help text about "keyword" from the user's manual) ??? keyword (a propos: list of related functions).
?4
binomial fibonacci hammingweight numbpart numtoperm partitions permorder permsign permtonum stirling
?atan
atan(x): arc tangent of x.
???determinant
algdisc bnfsunit charker charpoly ellheightmatrix ellpadicregulator forsubgroup matdet matdetint matdetmod mathnfmod matrixqz mspolygon nfdetint nfhnfmod polresultant rnfdet See also: Finite abelian groups Pseudo-bases, determinant
V = [1,2,3]
[1, 2, 3]
W = [4,5,6]~
[4, 5, 6]~
M = [1,2,3;4,5,6]
[1 2 3] [4 5 6]
V*W
32
M*W
[32, 77]~
U = [1..10]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
V[2]
2
W[1..2]
[4, 5]~
M[2,2]
5
M[1,]
[1, 2, 3]
M[,2]
[2, 5]~
M[1..2,1..2]
[1 2] [4 5]
factor(91)
[ 7 1] [13 1]
factor(91+I)
[ -1 1] [ 1 + I 1] [ 4 + 5*I 1] [1 + 10*I 1]
factor(x^4+4)
[x^2 - 2*x + 2 1] [x^2 + 2*x + 2 1]
factor((x^4+4)*I)
[x + (-1 - I) 1] [ x + (1 - I) 1] [x + (-1 + I) 1] [ x + (1 + I) 1]
factor((x^4+1)*Mod(1,y^2-2))
[x^2 + Mod(-y, y^2 - 2)*x + 1 1] [ x^2 + Mod(y, y^2 - 2)*x + 1 1]
factor((x^4+4)*Mod(1,13))
[Mod(1, 13)*x + Mod(4, 13) 1] [Mod(1, 13)*x + Mod(6, 13) 1] [Mod(1, 13)*x + Mod(7, 13) 1] [Mod(1, 13)*x + Mod(9, 13) 1]
\p38
realprecision = 38 significant digits
intnum(x=0,1,1/(1+x^2))/Pi
0.25000000000000000000000000000000000000
sumnum(n=1,1/n^2)/Pi^2
0.16666666666666666666666666666666666667
sumalt(n=1,(-1)^n*log(n))
0.22579135264472743236309761494744107198
exp(2*%)
1.5707963267948966192313216916397514427
[n^2|n<-[1..10]]
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
[n^2|n<-[1..10],isprime(n)]
[4, 9, 25, 49]
[a,b] = [1,2];
print("a=",a," b=",b)
a=1 b=2
dist(a,b) = sqrt(a^2+b^2);
dist(1,2)
2.2360679774997896964091736687312762354
In a GP file, line ending terminates the line
unless the they are preceded by =
or \
or are in a section delimited by braces:
cmpz(a,b) = if (abs(a)>abs(b), print(a), print(b))
(a,b)->if(abs(a)>abs(b),print(a),print(b))
cmpz(a,b) =
if (abs(a)>abs(b), print(a), print(b))
(a,b)->if(abs(a)>abs(b),print(a),print(b))
cmpz(a,b) = if (abs(a)>abs(b), \
print(a), \
print(b))
(a,b)->if(abs(a)>abs(b),print(a),print(b))
cmpz(a,b) =
{
if (abs(a)>abs(b),
print(a)
,print(b))
}
(a,b)->if(abs(a)>abs(b),print(a),print(b))
=
sign.my()
.fibo(n)=
{
my(u0=0,u1=1);
for(i=2,n,
[u0,u1]=[u1,u0+u1]);
u1;
}
fibo(100)
354224848179261915075
rho(n)=
{
my(x=2,y=5);
while(gcd(y-x,n)==1,
x=(x^2+1)%n;
y=(y^2+1)%n; y=(y^2+1)%n
);
gcd(n,y-x);
}
rho(2^64+1)
274177
wieferich(n)=
{
forprime(p=2,n,
if(Mod(2,p^2)^(p-1)==1,
return(p)));
}
wieferich2(n)=
{
my(r);
forprime(p=2,n,
if(Mod(2,p^2)^(p-1)==1,r=p;break));
r;
}
wieferich(10000)
1093
wieferich2(10000)
1093
V=vector(10,i,1/i)
[1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10]
[1/i|i<-[1..10]]
[1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/10]
M=matrix(4,4,i,j,i*j)
[1 2 3 4] [2 4 6 8] [3 6 9 12] [4 8 12 16]
Instead of
s3(n)=
{
my(m=sqrtint(n));
for (i=1,m,
for (j=1,m,
for (k=1,m,
if (i^2+j^2+k^2==n,
return([i,j,k])))));
}
s3(12345)
[4, 77, 80]
use forvec
:
s3(n)=
{
my(m=sqrtint(n));
forvec(v=vector(3,i,[1,m]),
if (v*v~==n,
return(v)));
}
s3(12345)
[4, 77, 80]
For a better algorithm, see qfsolve
.
birthday(n)=
{
my(M = Map());
for(i=1,oo,
my(x=random(n), j);
if(mapisdefined(M,x,&j),
return([i,j]));
mapput(M,x,i));
}
birthday(2^30)
[23001, 18881]
In GP, we describe a number field $K$ as $K = Q[x]/f(x)$ where $f \in \mathbb{Z}[x]$ is a monic irreducible polynomial.
f = x^4 - 2*x^3 + x^2 - 5
x^4 - 2*x^3 + x^2 - 5
polisirreducible(f)
1
GP knows cyclotomic polynomials:
g = polcyclo(30)
x^8 + x^7 - x^5 - x^4 - x^3 + x + 1
To perform simple operations in $K = \mathbb{Q}[x]/f(x) = \mathbb{Q}(\alpha)$ where $f(\alpha) = 0$, we can use Mod
:
Mod(x,f)^5
Mod(3*x^3 - 2*x^2 + 5*x + 10, x^4 - 2*x^3 + x^2 - 5)
Interpretation: $\alpha^5 = 3 \alpha^3 - 2 \alpha^2 + 5 \alpha + 10$. We check that the roots of $g$ are 30th roots of unity:
lift(Mod(x,g)^15)
-1
We used lift to make the output more readable.
Sometimes we can find a simpler defining polynomial for the same number field by using polredbest
:
h = x^5 + 7*x^4 + 22550*x^3 - 281686*x^2 - 85911*x + 3821551;
polredbest(h)
x^5 - x^3 - 2*x^2 + 1
Interpretation: $\mathbb{Q}[x]/h(x) \cong \mathbb{Q}[x]/(x^5 - x^3 - 2 x^2 + 1)$.
Most operations on number fields require a precomputation, which is performed by the initialisation function nfinit
.
K = nfinit(f);
K
contains the precomputation of the number field K = \mathbb{Q}[x]/f(x)
.
K.pol
x^4 - 2*x^3 + x^2 - 5
K.sign
[2, 1]
K
has signature (2, 1)
: it has two real embeddings and one pair of conjugate complex embeddings.
K.disc
-1975
K.zk
[1, 1/2*x^2 - 1/2*x - 1/2, x, 1/2*x^3 - 1/2*x^2 - 1/2*x]
w = K.zk[2]
1/2*x^2 - 1/2*x - 1/2
K has discriminant $-1975$, and its ring of integers is $$ \mathbb{Z}_K = \mathbb{Z} + \mathbb{Z}\frac{\alpha^2 - \alpha - 1}{2} + \mathbb{Z}\alpha + \mathbb{Z}\frac{\alpha^3 -\alpha^2 - \alpha}{2} $$