In [1]:
2*5
Out[1]:
10
In [2]:
3+4
Out[2]:
7
In [3]:
2^5
Out[3]:
32
In [4]:
sin(0)
Out[4]:
0
In [5]:
sin(1.323423)
Out[5]:
0.969558927852858
In [6]:
sin(pi) # uses radians 
Out[6]:
0
In [7]:
pi
Out[7]:
pi
In [9]:
N(pi, 100) # numerical approximation 
Out[9]:
3.1415926535897932384626433833
In [10]:
20/5
Out[10]:
4
In [11]:
20/3
Out[11]:
20/3
In [12]:
20/3+27/3
Out[12]:
47/3
In [18]:
1/3+5/3
Out[18]:
2
In [13]:
20/3 + 14/19
Out[13]:
422/57
In [14]:
a = 35 # new variable, assign value 35
In [17]:
a+5
Out[17]:
40
In [16]:
a^3 
Out[16]:
42875
In [19]:
# example of a block of code
# only prints out last statement
34+8
b=45
b+7
Out[19]:
52
In [20]:
print(34+8)
b=45
b+7
42
Out[20]:
52
In [21]:
# plotting functions
plot(x^3)
Out[21]:
In [22]:
plot(z^3)
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-22-09049c299b28> in <module>()
----> 1 plot(z**Integer(3))

NameError: name 'z' is not defined
In [23]:
z= var('z') # tells sage that z is a symbolic variable
plot(z^3)
Out[23]:
In [24]:
plot(sin(x))
Out[24]:
In [27]:
plot(sin(x), (-6,6), aspect_ratio=1, color='red')
Out[27]:
In [28]:
p1 = plot(sin(x), (-6,6), aspect_ratio=1, color='red')
# assigns the whole plot to p1 variable
In [29]:
p1
Out[29]:
In [30]:
p2 = plot(x^3)
In [32]:
p1+p2
Out[32]:
In [33]:
show(p1+p2, aspect_ratio=2)
In [34]:
# calculus
diff(x^3)
Out[34]:
3*x^2
In [36]:
x^2+2*x+1 # need * in 2*x to tell sage that we're multiplying
Out[36]:
x^2 + 2*x + 1
In [38]:
(x^2+2*x) + (3*x^2)
Out[38]:
4*x^2 + 2*x
In [40]:
expand((3*x^2 + 7)*(4*x+11))
Out[40]:
12*x^3 + 33*x^2 + 28*x + 77
In [41]:
factor(x^2+2*x+1)
Out[41]:
(x + 1)^2
In [42]:
factor(x^6-1)
Out[42]:
(x^2 + x + 1)*(x^2 - x + 1)*(x + 1)*(x - 1)
In [43]:
# integer/number theory stuff
In [44]:
factor(6)
Out[44]:
2 * 3
In [45]:
factor(24)
Out[45]:
2^3 * 3
In [48]:
factor(123812312312321332723209121)
Out[48]:
13 * 28463 * 10040929 * 33324674280971
In [53]:
# this will take too long
# can interrupt with stop bottom above
factor(10^500-1)
/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/repl/ipython_kernel/__main__.py:1: RuntimeWarning: cypari2 leaked 730024 bytes on the PARI stack
  from ipykernel.kernelapp import IPKernelApp
---------------------------------------------------------------------------
KeyboardInterrupt                         Traceback (most recent call last)
<ipython-input-53-0ca387978e4b> in <module>()
----> 1 factor(Integer(10)**Integer(500)-Integer(1))

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/arith/misc.py in factor(n, proof, int_, algorithm, verbose, **kwds)
   2592     if isinstance(n, Integer):
   2593         return m(proof=proof, algorithm=algorithm, int_=int_,
-> 2594                  verbose=verbose, **kwds)
   2595 
   2596     # Polynomial or other factorable object

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/rings/integer.pyx in sage.rings.integer.Integer.factor (build/cythonized/sage/rings/integer.c:25512)()
   4022         if algorithm == 'pari':
   4023             from sage.rings.factorint import factor_using_pari
-> 4024             F = factor_using_pari(n, int_=int_, debug_level=verbose, proof=proof)
   4025             F.sort()
   4026             return IntegerFactorization(F, unit=unit, unsafe=True,

/opt/sagemath-8.9/local/lib/python2.7/site-packages/sage/rings/factorint.pyx in sage.rings.factorint.factor_using_pari (build/cythonized/sage/rings/factorint.c:6401)()
    343             pari.set_debug_level(debug_level)
    344 
--> 345         p, e = n.__pari__().factor(proof=proof)
    346         if int_:
    347             return [(int(p[i]), int(e[i])) for i in range(len(p))]

cypari2/gen.pyx in cypari2.gen.Gen.factor()

KeyboardInterrupt: 
In [52]:
factor(10^500)
Out[52]:
2^500 * 5^500
In [54]:
factor(2^19-1)
Out[54]:
524287
In [55]:
%%time
factor(2^1273-1)
CPU times: user 17.5 s, sys: 15 ms, total: 17.5 s
Wall time: 17.6 s
Out[55]:
7639 * 272423 * 524287 * 193707721 * 761838257287 * 1010009364091859253946415882096915728247316963909668502131231962730629328112490054000200587148472755783548706757385217843090477620400711906456373617417113648349531726049904957160259747323084716670346629639738294532363180676161254571893455289289168546575712395096989246115213097149677953936893821176229778487061896303166426347880400358979822044582847
In [56]:
divisors(24)
Out[56]:
[1, 2, 3, 4, 6, 8, 12, 24]
In [57]:
#lists
In [58]:
list1 = [17,9,11,37]
In [59]:
list1[0]
Out[59]:
17
In [60]:
list1[1]
Out[60]:
9
In [62]:
list1[3]
Out[62]:
37
In [63]:
list2 = [4,5,"computer"] # "computer" is a string
In [64]:
list1 + list2
Out[64]:
[17, 9, 11, 37, 4, 5, 'computer']
In [65]:
list1[0]=3
In [66]:
list1
Out[66]:
[3, 9, 11, 37]
In [67]:
list1[1:3] # slice of list, includes element 1, but not element 3
Out[67]:
[9, 11]
In [68]:
list1[1]
Out[68]:
9
In [69]:
list1[2]
Out[69]:
11
In [ ]: