CoCalc
Shared2017-02-13-204055.ipynbOpen in CoCalc
Description: Jupyter notebook 2017-02-13-204055.ipynb
Author: Lucas PRABEL
Views : 5

Prise en main du logiciel Sage

Plot

plot(floor(x), (x, 1, 10), exclude=[1..10])
plot(x**2,xmin=-5,xmax=5,ymin=-5,ymax=5,color='purple',legend_label='x**2')
plot([sin(x), tan(x), 1-x^2], legend_label='automatic') # AUTOMATIC S'APPLIQUE A CE QUI EST ENTRE CROCHETS !

Tester une propriété

is_even(3)
False
is_prime(71)
True
is_prime_power(1872)
False

Symboles, expressions et variables

diff(x**3,x)
3*x^2
integrate(x**3,x)
1/4*x^4
integrate(exp(x),x)
e^x
var('y') integrate(exp(x),y)
y*e^x
integral(ln(x),x,0,1)
-1
integral(exp(-x^2),x,-oo,+oo)
sqrt(pi)
factor(4*x+4*y+2*x**2*y+9*x**2+2*x**3+9*x*y)
(2*x + 1)*(x + y)*(x + 4)
numerical_approx(pi,digits=200)
3.1415926535897932384626433832795028841971693993751058209749445923078164062862089986280348253421170679821480865132823066470938446095505822317253594081284811174502841027019385211055596446229489549303820
var('n') sum(1/n**2,n,1,+oo)
1/6*pi^2
sum(1/factorial(n),n,0,+oo)
e

Quelques objets

Z7 = IntegerModRing(7)
b = Z7(10)
b
3
ZZ
Integer Ring
QQ
Rational Field
a = Z7(2)
a
2
type(a)
<type 'sage.rings.finite_rings.integer_mod.IntegerMod_int'>
a**-1
4
a*4
1
Z9 = IntegerModRing(9)
b = Z9(2)
a == b
False
10//3
3
s = [2^i for i in [1..10]] ; s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
s+s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
4*s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024]
s.append(pi)
s
[2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, pi]
L = [] f = x*exp(x) for k in [1..6]: f = diff(f) L += [factor(f)] print(L)
[(x + 1)*e^x, (x + 2)*e^x, (x + 3)*e^x, (x + 4)*e^x, (x + 5)*e^x, (x + 6)*e^x]
parent(5)
Integer Ring
parent (5/2)
Rational Field
QQ
Rational Field
parent(x)
Symbolic Ring

Un peu de programmation

def factorielle(n): res = 1 for k in [2..n]: res = res*k return res
for k in [1..10]: print(factorielle(k))
1 2 6 24 120 720 5040 40320 362880 3628800
def factorielle2(n): if n==0 or n==1 : return 1 return n*factorielle2(n-1)
for k in [1..10]: print(factorielle2(k))
1 2 6 24 120 720 5040 40320 362880 3628800
for k in [1..10]: print(factorial(k))
1 2 6 24 120 720 5040 40320 362880 3628800
next_prime(11)
13
def liste_premiers(N): a = 2 L = [] while a <= N : L += [a] a = next_prime(a) return L
liste_premiers(100)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
L = [] for k in [2..1000]: if is_prime(k) and is_prime(k+2) : L += [(k,k+2)] L
[(3, 5), (5, 7), (11, 13), (17, 19), (29, 31), (41, 43), (59, 61), (71, 73), (101, 103), (107, 109), (137, 139), (149, 151), (179, 181), (191, 193), (197, 199), (227, 229), (239, 241), (269, 271), (281, 283), (311, 313), (347, 349), (419, 421), (431, 433), (461, 463), (521, 523), (569, 571), (599, 601), (617, 619), (641, 643), (659, 661), (809, 811), (821, 823), (827, 829), (857, 859), (881, 883)]
def fib(n): if n in [0,1] : return 1 else : return fib(n-1)+fib(n-2)
@cached_function def fibb(n): if n in [0,1] : return 1 else : return fibb(n-2)+fibb(n-1)
fib(30)
1346269
fibb(100)
573147844013817084101
def fib2(n): a, b = 1,1 for i in [1..n-1]: a, b = b+a,a return a
for k in [0..10]: print(k,fib2(k))
(0, 1) (1, 1) (2, 2) (3, 3) (4, 5) (5, 8) (6, 13) (7, 21) (8, 34) (9, 55) (10, 89)
X = [(k,fibb(k+1)/fibb(k)) for k in [1..30]] point(X)
N(lim(fibb(k+1)/fibb(k),k=+oo))
1.61803398874965
def syracuse(u0): X = [(0,u0)] k = 0 uk = u0 while uk !=1: if uk%2 == 0 : uk = uk/2 else : uk = 3*uk+1 k += 1 X += [(k,uk)] return X
point(syracuse(127))