Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 134

Introdução ao Sagemath

Obter o sagemath:

http://www.sagemath.org/

Acesso à cloud:

https://cloud.sagemath.com/

Para cálculos pontuais:

http://sagecell.sagemath.org/

L=[1..10] L
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[n for n in range(0,10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[n for n in (0..10)]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
for n in L: print 2*n
2 4 6 8 10 12 14 16 18 20
[2*n for n in L]
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
for n in range(10,20): if not(n%4 == 2): print n
[n for n in range(10,20) if not(n%4==2)]
[11, 12, 13, 15, 16, 17, 19]
L=[]; k=1 # L e' a lista vazia while k<=10: L=L+[2*k] k+=1 L
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
S=0 ; n=10 for k in [1..n] : S = S+(2*k)*(2*k+1) S
1650
def somaDuarte(n): S=0 ; for k in [1..n] : S = S+(2*k)*(2*k+1) return S
somaDuarte(10) var('n k') ; res = sum(2*k*(2*k+1),k,1,n) res
1650 (n, k) 4/3*n^3 + 3*n^2 + 5/3*n
pretty_print(res)
43n3+3n2+53n\renewcommand{\Bold}[1]{\mathbf{#1}}\frac{4}{3} \, n^{3} + 3 \, n^{2} + \frac{5}{3} \, n

O problema de Siracusa:

http://en.wikipedia.org/wiki/Collatz_conjecture

 

nNn\in \mathbb{N}

un+1=un2 u_{n+1}= \frac{u_n}{2}  se unu_n é par, e un+1=3un+1  u_{n+1}=3u_n + 1  se unu_n é ímpar.

u = 6 ; n = 0 while u != 1 : if u % 2 == 0 : u = u/2 else : u = 3*u+1 n = n+1 n
8
not(is_odd(8))
True
def siracusa(u): n = 0 while u != 1 : if u % 2 == 0 : u = u/2 else : u = 3*u+1 n = n+1 return n
siracusa(60)
19
siracusa(10)
6
[siracusa(k) for k in range(2,300)]
[1, 7, 2, 5, 8, 16, 3, 19, 6, 14, 9, 9, 17, 17, 4, 12, 20, 20, 7, 7, 15, 15, 10, 23, 10, 111, 18, 18, 18, 106, 5, 26, 13, 13, 21, 21, 21, 34, 8, 109, 8, 29, 16, 16, 16, 104, 11, 24, 24, 24, 11, 11, 112, 112, 19, 32, 19, 32, 19, 19, 107, 107, 6, 27, 27, 27, 14, 14, 14, 102, 22, 115, 22, 14, 22, 22, 35, 35, 9, 22, 110, 110, 9, 9, 30, 30, 17, 30, 17, 92, 17, 17, 105, 105, 12, 118, 25, 25, 25, 25, 25, 87, 12, 38, 12, 100, 113, 113, 113, 69, 20, 12, 33, 33, 20, 20, 33, 33, 20, 95, 20, 46, 108, 108, 108, 46, 7, 121, 28, 28, 28, 28, 28, 41, 15, 90, 15, 41, 15, 15, 103, 103, 23, 116, 116, 116, 23, 23, 15, 15, 23, 36, 23, 85, 36, 36, 36, 54, 10, 98, 23, 23, 111, 111, 111, 67, 10, 49, 10, 124, 31, 31, 31, 80, 18, 31, 31, 31, 18, 18, 93, 93, 18, 44, 18, 44, 106, 106, 106, 44, 13, 119, 119, 119, 26, 26, 26, 119, 26, 18, 26, 39, 26, 26, 88, 88, 13, 39, 39, 39, 13, 13, 101, 101, 114, 26, 114, 52, 114, 114, 70, 70, 21, 52, 13, 13, 34, 34, 34, 127, 21, 83, 21, 127, 34, 34, 34, 52, 21, 21, 96, 96, 21, 21, 47, 47, 109, 47, 109, 65, 109, 109, 47, 47, 8, 122, 122, 122, 29, 29, 29, 78, 29, 122, 29, 21, 29, 29, 42, 42, 16, 29, 91, 91, 16, 16, 42, 42, 16, 42, 16, 60, 104, 104, 104, 42, 24, 29, 117, 117, 117, 117, 117, 55, 24, 73, 24, 117]
def fact1 (n) : res = 1 for k in [1..n] : res = res*k return res
fact1(10)
3628800
def fact2(n): if n==0 : return 1 else : return n*fact2(n-1)
fact2(10)
3628800

Voltando às listas...

L=[0,11,22,33,44,55]
L[-1]; L[-2]; L[-3]
55 44 33
L[2:4]; L[-4:4]; L[2:-2]
[22, 33] [22, 33] [22, 33]
L[:4]; L[4:]
[0, 11, 22, 33] [44, 55]
L[2:6] = [12,13,14]
L
[0, 11, 12, 13, 14]
L=L + [1,2,3] L
[0, 11, 12, 13, 14, 1, 2, 3]
L=[0..20] L
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
L[1:20:3]
[1, 4, 7, 10, 13, 16, 19]
[[x,y] for x in [1..2] for y in [6..9]]
[[1, 6], [1, 7], [1, 8], [1, 9], [2, 6], [2, 7], [2, 8], [2, 9]]
L=[1,8,5,2,9] ; L.reverse() ; L
[9, 2, 5, 8, 1]
L.sort() ; L
[1, 2, 5, 8, 9]
L.sort(reverse=True) ; L
[9, 8, 5, 2, 1]

Definindo estruturas int mod

n=12^31+1; p=next_prime(n); p
2848515765597237675947403497177093
2^(p-1)
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_66.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("Ml4ocC0xKQ=="),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmpavKuuw/___code___.py", line 3, in <module> exec compile(u'_sage_const_2 **(p-_sage_const_1 )' + '\n', '', 'single') File "", line 1, in <module> File "integer.pyx", line 1986, in sage.rings.integer.Integer.__pow__ (sage/rings/integer.c:14002) RuntimeError: exponent must be at most 2147483647
Zp=IntegerModRing(p); Zp
Ring of integers modulo 2848515765597237675947403497177093
Zp.is_field()
True
a=Zp(2)
1/a
1424257882798618837973701748588547
a^(p-1) # Pequeno Teorema de Fermat
1
n=35 Zn=IntegerModRing(n)
Zn.is_field()
False
b=Zn(15)
1/b
Traceback (most recent call last): File "<stdin>", line 1, in <module> File "_sage_input_76.py", line 10, in <module> exec compile(u'open("___code___.py","w").write("# -*- coding: utf-8 -*-\\n" + _support_.preparse_worksheet_cell(base64.b64decode("MS9i"),globals())+"\\n"); execfile(os.path.abspath("___code___.py"))' + '\n', '', 'single') File "", line 1, in <module> File "/tmp/tmptq2_sH/___code___.py", line 3, in <module> exec compile(u'_sage_const_1 /b' + '\n', '', 'single') File "", line 1, in <module> File "element.pyx", line 1813, in sage.structure.element.RingElement.__div__ (sage/structure/element.c:15221) File "coerce.pyx", line 804, in sage.structure.coerce.CoercionModel_cache_maps.bin_op (sage/structure/coerce.c:7326) File "element.pyx", line 1811, in sage.structure.element.RingElement.__div__ (sage/structure/element.c:15200) File "integer_mod.pyx", line 2442, in sage.rings.finite_rings.integer_mod.IntegerMod_int._div_ (sage/rings/finite_rings/integer_mod.c:22867) ZeroDivisionError: Inverse does not exist.
gcd(b,n)
5
gcd(8,n)
1
a=Zn(8)
a^(euler_phi(n))
1
2.powermod(n-1,n)
9
def listaP(m): lista=[n for n in range(2,m) if is_prime(n)] return lista
listaP(50)
[2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47]
P=Primes(); P
Set of all prime numbers: 2, 3, 5, 7, ...
gemeos=[[n,n+2] for n in range(2,200) if n in P and n+2 in P]; gemeos
[[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]]
def gemeos(m): P=Primes() lista=[[n,n+2] for n in range(2,m) if n in P and n+2 in P] return lista, len(lista)
gemeos(20000)
([[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], [1019, 1021], [1031, 1033], [1049, 1051], [1061, 1063], [1091, 1093], [1151, 1153], [1229, 1231], [1277, 1279], [1289, 1291], [1301, 1303], [1319, 1321], [1427, 1429], [1451, 1453], [1481, 1483], [1487, 1489], [1607, 1609], [1619, 1621], [1667, 1669], [1697, 1699], [1721, 1723], [1787, 1789], [1871, 1873], [1877, 1879], [1931, 1933], [1949, 1951], [1997, 1999], [2027, 2029], [2081, 2083], [2087, 2089], [2111, 2113], [2129, 2131], [2141, 2143], [2237, 2239], [2267, 2269], [2309, 2311], [2339, 2341], [2381, 2383], [2549, 2551], [2591, 2593], [2657, 2659], [2687, 2689], [2711, 2713], [2729, 2731], [2789, 2791], [2801, 2803], [2969, 2971], [2999, 3001], [3119, 3121], [3167, 3169], [3251, 3253], [3257, 3259], [3299, 3301], [3329, 3331], [3359, 3361], [3371, 3373], [3389, 3391], [3461, 3463], [3467, 3469], [3527, 3529], [3539, 3541], [3557, 3559], [3581, 3583], [3671, 3673], [3767, 3769], [3821, 3823], [3851, 3853], [3917, 3919], [3929, 3931], [4001, 4003], [4019, 4021], [4049, 4051], [4091, 4093], [4127, 4129], [4157, 4159], [4217, 4219], [4229, 4231], [4241, 4243], [4259, 4261], [4271, 4273], [4337, 4339], [4421, 4423], [4481, 4483], [4517, 4519], [4547, 4549], [4637, 4639], [4649, 4651], [4721, 4723], [4787, 4789], [4799, 4801], [4931, 4933], [4967, 4969], [5009, 5011], [5021, 5023], [5099, 5101], [5231, 5233], [5279, 5281], [5417, 5419], [5441, 5443], [5477, 5479], [5501, 5503], [5519, 5521], [5639, 5641], [5651, 5653], [5657, 5659], [5741, 5743], [5849, 5851], [5867, 5869], [5879, 5881], [6089, 6091], [6131, 6133], [6197, 6199], [6269, 6271], [6299, 6301], [6359, 6361], [6449, 6451], [6551, 6553], [6569, 6571], [6659, 6661], [6689, 6691], [6701, 6703], [6761, 6763], [6779, 6781], [6791, 6793], [6827, 6829], [6869, 6871], [6947, 6949], [6959, 6961], [7127, 7129], [7211, 7213], [7307, 7309], [7331, 7333], [7349, 7351], [7457, 7459], [7487, 7489], [7547, 7549], [7559, 7561], [7589, 7591], [7757, 7759], [7877, 7879], [7949, 7951], [8009, 8011], [8087, 8089], [8219, 8221], [8231, 8233], [8291, 8293], [8387, 8389], [8429, 8431], [8537, 8539], [8597, 8599], [8627, 8629], [8819, 8821], [8837, 8839], [8861, 8863], [8969, 8971], [8999, 9001], [9011, 9013], [9041, 9043], [9239, 9241], [9281, 9283], [9341, 9343], [9419, 9421], [9431, 9433], [9437, 9439], [9461, 9463], [9629, 9631], [9677, 9679], [9719, 9721], [9767, 9769], [9857, 9859], [9929, 9931], [10007, 10009], [10037, 10039], [10067, 10069], [10091, 10093], [10139, 10141], [10271, 10273], [10301, 10303], [10331, 10333], [10427, 10429], [10457, 10459], [10499, 10501], [10529, 10531], [10709, 10711], [10859, 10861], [10889, 10891], [10937, 10939], [11057, 11059], [11069, 11071], [11117, 11119], [11159, 11161], [11171, 11173], [11351, 11353], [11489, 11491], [11549, 11551], [11699, 11701], [11717, 11719], [11777, 11779], [11831, 11833], [11939, 11941], [11969, 11971], [12041, 12043], [12071, 12073], [12107, 12109], [12161, 12163], [12239, 12241], [12251, 12253], [12377, 12379], [12539, 12541], [12611, 12613], [12821, 12823], [12917, 12919], [13001, 13003], [13007, 13009], [13217, 13219], [13337, 13339], [13397, 13399], [13679, 13681], [13691, 13693], [13709, 13711], [13721, 13723], [13757, 13759], [13829, 13831], [13877, 13879], [13901, 13903], [13931, 13933], [13997, 13999], [14009, 14011], [14081, 14083], [14249, 14251], [14321, 14323], [14387, 14389], [14447, 14449], [14549, 14551], [14561, 14563], [14591, 14593], [14627, 14629], [14867, 14869], [15137, 15139], [15269, 15271], [15287, 15289], [15329, 15331], [15359, 15361], [15581, 15583], [15641, 15643], [15647, 15649], [15731, 15733], [15737, 15739], [15887, 15889], [15971, 15973], [16061, 16063], [16067, 16069], [16139, 16141], [16187, 16189], [16229, 16231], [16361, 16363], [16451, 16453], [16631, 16633], [16649, 16651], [16691, 16693], [16829, 16831], [16901, 16903], [16979, 16981], [17027, 17029], [17189, 17191], [17207, 17209], [17291, 17293], [17387, 17389], [17417, 17419], [17489, 17491], [17579, 17581], [17597, 17599], [17657, 17659], [17681, 17683], [17747, 17749], [17789, 17791], [17837, 17839], [17909, 17911], [17921, 17923], [17957, 17959], [17987, 17989], [18041, 18043], [18047, 18049], [18059, 18061], [18119, 18121], [18131, 18133], [18251, 18253], [18287, 18289], [18311, 18313], [18521, 18523], [18539, 18541], [18911, 18913], [18917, 18919], [19079, 19081], [19139, 19141], [19181, 19183], [19211, 19213], [19379, 19381], [19421, 19423], [19427, 19429], [19469, 19471], [19541, 19543], [19697, 19699], [19751, 19753], [19841, 19843], [19889, 19891], [19961, 19963], [19991, 19993]], 342)