/* Magma Lecture 2 */ /* This is how you make a comment. It is good to annotate your code with comments for those who have to read it. */ /* Let's begin by looking at some basic control structures. */ for i in [1..10] do i; end for; /* We all know this will count to 10, right? */ /* So how would we do this using repeat or while? */ i := 0; while i le 9 do i := i + 1; i; end while; i := 0; repeat i := i + 1; i; until i eq 10; /* Note here a subtle difference. After using the above while / repeat codes, i still exists. */ /* This is not so after the for loop. For makes its own iterator i, which is deleted after the loop finishes.*/ /* More simple programs: */ c := 0; for i in [1..10] do c := c + 1; end for; c; /* &&&&&&&&&&&&& */ c := 0; ss := 100; for i in [1..ss] do if i mod 2 eq 0 then c := c + 1; end if; end for; c/ss; /* &&&&&&&&&&&&& */ i := 0; repeat i := i + 1; until i gt 10; i; /* &&&&&&&&&&&&& */ repeat i := Random(0,100); until i mod 20 eq 0; i; /* The program we wrote together in class */ for j in [1..100] do G := SymmetricGroup(j); c := 0; n := 100; ss := 100000; for i in [1..ss] do m := Random(G); if Order(m) mod 2 eq 0 then c := c + 1; end if; end for; RealField(5)!c/ss; end for; /* Results: as n->infinity, the fraction of elements of even order in Sn goes to 1. */ /* Writing output to a file. */ SetOutputFile("C:/Users/anotherghost/Documents/out.txt" : Overwrite := true); for i in [1..10] do i; end for; UnsetOutputFile();