/* Comments start with slash star and end with star slash */ > P:={ n: n in [1..1000] | IsPrime(n) }; > P; { 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 } > #P; 168 > /* So there are 168 primes less 1000 */ > Factorization(2700); [ <2, 2>, <3, 3>, <5, 2> ] > /* This is a sequence of ordered pairs. > In each pair, the first term is a prime number and the second term tells > us the power of that prime in the prime factorization of 2700. > So 2700 must be 2 squared times 3 cubed times 5 squared. */ > 2^2*3^3*5^2; 2700 > /* as expected! > > Note that one can always use an "assignment command" to assign a value > to an identifier. For example: */ > facs:=Factorization(2700); > /* I have assigned the value of Factorization(2700) to the identifier facs. > Magma has worked it out, but will not print out the value until asked to. */ > facs; [ <2, 2>, <3, 3>, <5, 2> ] > /* You can get the 1st, 2nd and 3rd terms of this sequence with facs[1], facs[2] > and facs[3], and since (for example) facs[2] is a pair you can also type > things like facs[1][1], facs[3][2], etc. */ > facs[3]; <5, 2> > facs[3][1]; 5 > facs[3,2]; 2 > /* facs[3,2] is an alternative syntax for facs[3][2] */ > Factorization(768); [ <2, 8>, <3, 1> ] > 2^8*3; 768 > Factorization(100000020000001); [ <11, 2>, <909091, 2> ] > 11^2*909091^2; 100000020000001 > Factorization(2^11-1); [ <23, 1>, <89, 1> ] > 23*89; 2047 > 2^11-1; 2047 > Factorization(2^(2^5)+1); [ <641, 1>, <6700417, 1> ] > 641*6700417 eq 2^(2^5)+1; true > 2^(2^5)+1; 4294967297 > n:=Random(P); > n; 599 > n:=Random(P); > n; 653 > n:=Random(P); > n; 653 > n:=Random(P); > n; 661 > n:=Random(P); > n; 587 > n:=Random(P); > n; 929 > /* Each time you ask magma to choose a random element of a set it will > do so. Of course it actually uses some rule to get these pseudo-random > elements, but they satisfy any objective test for randomness that anyone > has been able to devise. It was quite remarkable that I got 653 twice > in a row above. It is the first time that has ever happened to me. But > the probability of it happening was 1/168. */ > b,q:=IsDivisibleBy(2^n-2,n); > b,q; true 488483903957661135045658271107882220718912739061828041382417991914431949007066535266\ 48393168903144716188452509464488873943237214203863259562179863743198575475685631773899102\ 97184992926210328255053629455836930084084198519397740950530109381505430020213910960368323\ 003279288745790 > /* The "IsDivisibleBy" function returns two values. The first value it returns > is either "true" or "false", depending on whether or not the first argument > is divisible by the second. If the first returned value is "true", the > second returned value is the number of times the second argument goes into > the first. If the first returned value is false then it doesn't actually > return any second value -- I lied! */ > q*n eq 2^n - 2; true > /* as expected! */ > b,q:=IsDivisibleBy(7,4); > b; false > q; >> q; ^ User error: Identifier 'q' has not been declared or assigned > /* Let's check Fermat's Little Theorem for some other values of a, and for > our current value of n (which is still 929): */ > b,q:=IsDivisibleBy(3^n-3,n); > b,q; true 189509035186074144933386356504616749376524825326681498925272110801763059608759118951\ 37991199897914729058107346815600601078472242076996743929561770078692962821517826065886869\ 48653761539895659253839575005705297445998343847221102595078345932352466907975980470032116\ 96959405207978329395694830334234488999020297271560279003863086431768301945880314202323296\ 92067159880671969052539250553973986276578193727072096440349845432431327279526238975707072\ 0 > b,q:=IsDivisibleBy(4^n-4,n); > b,q; true 221674751191491564852237286769545292769645006769140842697743989859055876840951527584\ 98520875686287194260557425581544691323014551559862164129508550342776002718656712444340248\ 15437369300962095809366866300915793677382264219302680325464613317829549639223997945579324\ 79795956618723180287300550768194945964974260630079988694781712613844842534097801620250041\ 10837522493522243396005296057341467900463308219328485323578350402878581899466274080950435\ 67312968302448130974062317661868690856701978751381578349342404889254644266685690568301644\ 9414864685260152821645672060 > b,q:=IsDivisibleBy(18^n-18,n); > b,q; true 151405456498726346324847021617637283190055089726671710746774814963405322637002405272\ 71502226920337120919252893976486090003779950662472182221223490390633090079028717012815167\ 58929393664913946957082398705955342015128312523204734967215378682703573020805494242010691\ 87878780082383062388913829646500274366938553234313736960421008414304157400873240419601370\ 39112327110070358818373500627465607442999994756933445563415293475191734818723193361970844\ 76146935328902032303514978884468696751026585552791783484003116961408682637937453501810118\ 11658601157495592140115862823513687285855103357516119682106678389832160931097624715382654\ 57953193375026375372872399331706438171858278237290802821874393218377491161996445180407779\ 47889694856214788846346269594696995438242916155396272940208821584882146898806075423451589\ 28125611647483757558368701017583543767520503390694078193830133402589889935288650709993541\ 89313323378565406812921089420208104504299486239256022255348760355265062230373590437425958\ 60696535189538422089751183651561133827340136771958629307263000762513917052634427341019308\ 99760081391870838894709107843589676351876959033606848213371688136816771384922931081588687\ 982238415150 > b,q:=IsDivisibleBy(199^n-199,n); > b,q; true 463972532336001369301041225748637228609264379972613728679630170920836794745959553308\ 60520116420883924551739359871442621515518918869260408112041684984137714077598303994148346\ 45514879496064145508679912765954332081275770047045238783503691564543394267489014714372641\ 88465563551826714380966580565521492213719092339365462473039562891002811402882394402217401\ 54841954365510741987461776722146336790188421997022857335128540583288812102033696284325678\ 73827181051683095397976763626715030463983142581078918926618792734660578372580551082121412\ 13797927189479266561851416599006099669150154660018024564942229952545741396728786534910766\ 11813528273348592900225978549894170798125154147945985136444801228367944486060177927742224\ 55816226808723971227546024186365419623238168989164755390324888956337598222210321585276381\ 11965367351714524627087527774787777620229479145901048652676771158173935017526390617516446\ 60626224093192009652664969329869314719919301040684096511557258368835852172313298893281839\ 63055801519766897473686294691036445141288937756261749302970191605483011963945901781955563\ 25970912073558237372771198705910043033716943622992063904451052932487052333169682770836869\ 74509325877159615618133858930643447630089946379509678925999172455073850236069141865952193\ 41766888415520683378687256819571840105547613493189874291500604352870710164429862991284833\ 16817066830043172668252098572123375790887561021058109438800965698104773091784043859062924\ 10902707124151809762257075420419344846516894609473132615775411220062251754045967012685203\ 97964241510531477934367192976312133533189915285958566169511140851741787403753637922456386\ 28075810771346725663953350613004500779756767169542803994240834224682497076611640702906757\ 11281963281349714825743150635522981611241763637677881152487987126669471592557786215049041\ 03394111380384233339551074059537901018480367648843651171086728132309614472923318026555746\ 18510868024470314111420409135372771525869239776478134930952230734219588741522305420594533\ 13109674907846328475748672203860287967556994073740816286595155638417321942286087601752978\ 81800795029966283964908284662887485105667913318722224318268626521826090155782097456960064\ 00 > /* You can see that magma has no trouble at all rapidy performing calculations > like this that involve enormous numbers. Some calculations with large numbers > are harder. It will be important for us to understand why some calculations > are quick and easy and others are not. We will come back to this ... */ > 929*q eq 199^929-199; true > /* as expected! Note that in statements like this in the magma language, > eq means equal > ne means not equal > gt means greater than > ge means greater or equal > lt means less than > le means less or equal > You might wish that the desgigners of magma had decided to use = for equal, > and > for greater than, etc., -- but they wanted to reserve these symbols > for use in other kinds of expressions. It is a decision they made. */ > for n in P do for> IsDivisibleBy(199^n-199,n); for> end for; true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true true > /* How many times was the word "true" printed here? > The command IsDivisibleBy(199^n-199,n) was executed once for each > n in the set P. So "true" was printed 168 times. */ > seqP:=[ n: n in [1..1000] | IsPrime(n) ]; > seqP; [ 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, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997 ] > /* Note that seqP is not the same as P. One is a sequence and the other is > a set: they cannot be equal. In a sequence the order of the terms matters, > in a set the order does not matter. For example: */ > {1,2,3} eq {3,1,2}; true > [1,2,3] eq [3,1,2]; false > /* And if you ask magma whether P equal seqP magma will politely inform > you that you have asked a silly question. P and seqP are different types > of things -- so they can't possible be equal! */ > P eq seqP; >> P eq seqP; ^ Runtime error in 'eq': Bad argument types Argument types given: SetEnum[RngIntElt], SeqEnum[RngIntElt] > seqP[73]; 367 > &+seqP; 76127 > &*seqP; 19590340644999083431262508198206381046123972390589368223882605328968666316379870661851951648789482321596\ 22955911543601914918952972521526672829228299085264902336273139240401793914201095826139363495947148375719\ 67216722434100671185162276611331351924888489899148921571883086798968751374395193389039680949055497503864\ 07106033836586660683539201011635917900039904495065203299749542985993134669814805318474080581207891125910 > H:="HELLO!"; > s:="week1.txt"; > s[4]; k > H[6]; ! > H*s; HELLO!week1.txt > #H; 6 > #(s*H); 15 > StringToCode("A"); 65 > StringToCode("ABCDefg123**!!~"); 65 > /* The StringToCode function can be applied to any string, but it only > returns the code number of the first character in the string. The code > that Magma uses depends on the computer system on which magma is > installed; however, in this country the code is at least going to be > consistent with ASCII (the American Standard Code for Information Interchange) > which assigns 128 code numbers to the letters of the alphabet (upper and > lower case), numerals, various punctuation marks and the like. In > particular, in ASCII an upper case A gets the number 65. */ > intseq:=[StringToCode((H*s)[i]): i in [1..#(H*s)]]; > intseq; [ 72, 69, 76, 76, 79, 33, 119, 101, 101, 107, 49, 46, 116, 120, 116 ] > /* 72 for the H, 69 for the E, 76 for L, etc. etc., up to 116 for t. */ > [CodeToString(i): i in intseq]; [ H, E, L, L, O, !, w, e, e, k, 1, ., t, x, t ] > /* This is a sequence of strings. Each term of the sequence is a > string of length 1. Now let us concatenate them: */ > &*[CodeToString(i): i in intseq]; HELLO!week1.txt > GCD(1152,1296); 144 > /* If we divide 1152 and 1296 both by 144 then we will be left with > two numbers that do not have any common factor. */ > 1152 div 144; 8 > 1296 div 144; 9 > GCD(8,9); 1 > /* OK, I lied. Of course 8 and 9 do have a common factor: 1 is a common > factor. But they do not have any nontrivial common factor. */ > a:=Random(100000000); > b:=Random(100000000); > a; 90121319 > b; 211449 > GCD(a,b); 1 > a:=Random(100000000); > b:=Random(100000000); > GCD(a,b); 1 > a:=Random(100000000); > b:=Random(100000000); > GCD(a,b); 1 > a:=Random(100000000); > b:=Random(100000000); > GCD(a,b); 3 > /* Most pairs of numbers seem to have no (nontrivial) common factor. */ > a:=Random(10^10000); > b:=Random(10^10000); > GCD(a,b); 31 > /* magma did this almost instantaneously. But a and b are huge! 10000 digits each! > I do not even want to print them out -- it would make this file too big. > But please note that finding gcd's is something that can be done EXTREMELY > RAPIDLY AND EFFICIENTLY. The Euclidean Algorithm is a wonderful algorithm. */ > EuclidGCD:=function(a,b); function> while b ne 0 do function|while> r:= a mod b; function|while> a:=b; function|while> b:=r; function|while> end while; function> return a; function> end function; > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 1 > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 2 > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 1 > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 1 > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 1 > a:=Random(10^10000); > b:=Random(10^10000); > EuclidGCD(a,b); 4 > UnsetLogFile();