
Number Theory Learning Module 2 — Prime Numbers and Integer Factorization in Sage 1 1 Objectives. • Learn how to test for primality and generate prime numbers and in Sage. • Learn how to work with factorizations in Sage. • Introduce lists in Sage. • Do examples of mathematical experiments. As usual, this should be read as a tutorial, in front of the computer, trying the commands as you read them. 2 Prime Numbers The Sage function that checks if a number is prime or composite is called is_prime: is_prime(129) is_prime(101) Try the following, to check that Sages uses the same conventions we made when defining prime numbers : is_prime(1) is_prime(0) is_prime(-2) Sage can decide primality of pretty large numbers. For example, it will quickly determine that the 10th Fermat number, 210 F10 2 1, is not prime: is_prime(2ˆ2ˆ10+1) 215 F10 is an integer with 309 digits. Now, let’s try to check if F15 2 1 is prime: is_prime(2ˆ2ˆ15+1) Don’t be alarmed if Sage takes more than a few seconds to produce any output. This number has 9865 digits, and is taxing Sage a little bit. We often need to generate all primes up to a given bound. This is done by the function prime_range: plist = prime_range(50) print plist This will output: r2;3;5;7;11;13;17;19;23;29;31;37;41;43;47s The range does not have to start at 1: prime_range(2000,2500) The prime_range function returns a Sage data structure called a list. Lists are denoted by a sequence of Sage objects enclosed by square brackets. Lists are a very important structure, and have to be well understood to take full advantage of Sage. Elements of a list can be accessed using index notation: Created by L. Felipe Martins. [email protected] License: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ Number Theory Learning Module 2 — Prime Numbers and Integer Factorization in Sage 2 plist[2] returns 5. Indexes in Sage (as in most modern computer languages) start at 0, so plist[2] returns the third list entry, not the second. This may take a while to get used to, but eventually becomes second nature. We compute the length of a list with the function len: len(plist) This returns 15, which is the number of primes smaller than 50. Now suppose that we want to know the number of primes up to 1000. We can do this in a single statement, without using a variabale to store the intermediate result: len(prime_range(1000)) This shows that there are 168 primes smaller than 1000. There is an important point to be emphasized concerning ranges in Sage. For example, enter: prime_range(19) This outputs r2;3;5;7;11;13;17s Notice that the upper limit of the range (19) is not included in the list, even though it is a prime number. This is a convention that is enforced throughout Sage: The upper limit of a range is never included in the range. Lists are a very powerful construct, and will be studied in more detail in Section4. The functions next_prime and previous_prime return the first prime above and below its argument: next_prime(10000) previous_prime(10000} Finally, applications in cryptography usually require the generation of ”random” primes, which is done with the function random_prime: random_prime(10000) Go ahead and execute the command above several times. Each time it generates a different prime in the interval r0;10000q. To generate a random prime with, say, 30 digits, use: random_prime(10ˆ30,10ˆ31) Computers, being deterministic machines, can’t really generate perfectly random numbers. Computer-generated se- quences that approximate randomness are called pseudorandom number generators. So, to be exact, we should say that random_prime generates pseudorandom primes in a given range. 3 Integer Factorization The Sage function factor returns the prime factorization of an integer: factor(326025) factor(64184207) factor(3883843) We can access the terms of the factorization using index notation: Created by L. Felipe Martins. [email protected] License: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ Number Theory Learning Module 2 — Prime Numbers and Integer Factorization in Sage 3 factors = factor(2282600) print factors first_factor = factors[0] print first_factor second_factor = factors[1] print second_factor This produces the following output: 23 ¤ 52 ¤ 101 ¤ 113 p2;3q p5;2q Each factor is represented as a pair pp;eq, where p is a prime and e is the corresponding exponent. The elements of the pair can also be accessed using index notation: print first_factor[0] print first_factor[1] The first statement prints the prime in the first term of the factorization (2) and the second the exponent (3). If we only need to know what are the prime factors of a number, we can use the prime_factors function: prime_factors{2282600} 4 Lists One of the advantages of using software like Sage is that it becomes much simpler to perform mathematical experi- ments. In this section we explore the use of Sage’s lists to test conjectures about integer numbers. A Sage list is a sequence of comma-separated objects enclosed between [ and ]. A list can be created by explicitly enumerating its elements: list0 = [2,5,-3,0,2] print list0 This outputs: r2;5;¡3;0;2s Another basic method of generating lists is to use range: list1 = range(7) print list1 list2 = range(9,15) print list2 list3 = range(0,21,3) print list3 The last example shows how to use the third argument to specify a step size for the range. Notice, in all examples, that the upper limit is never included in the range. Another way to generate a list in to use list comprehensions. These have the following form: [ expression for variable in list ] Here is an example: Created by L. Felipe Martins. [email protected] License: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ Number Theory Learning Module 2 — Prime Numbers and Integer Factorization in Sage 4 list4 = [nˆ2 for n in range(11)] print list4 The expression defining list4 is interpreted as: The list of n2, where n is an integer in the range r0;11q. In other words, it generates a list of the squares of the integers from 0 to 10 (inclusive). There is another form of list comprehension that will be very useful to us: [ expression for variable in list if condition ] Suppose, for example, that we want to generate a list of the primes up to 100 that leave remainder 1 when divided by 4, that is, primes of the form 4n 1 for some integer n. This is how to do it: [p for p in prime_range(100) if p%4 == 1] Recall that p%4 is the remainder of p divided by 4. To test if this is equal to 1, we use ==. (We can’t use = because this represents assignment.) Let’s also compute the list of primes up to 100 that have the form 4n 3: [p for p in prime_range(100) if p%4==3] Notice that there are more primes of the form 4n 3 than of the form 4n 1. We can investigate whether this pattern holds for bounds different than 100. The actual list of primes does not have to be displayed each time, all we need is its length (that is, the number of entries). We can use the following: pmax = 1000 primes_list = prime_range(pmax+1) n1 = len([p for p in primes_list if p%4 == 1]) n3 = len([p for p in primes_list if p%4 == 3]) print "Remainder 1: " + str(n1) print "Remainder 3: " + str(n3) This code introduces some techniques for improving organization and efficiency: • We assign the largest number in consideration (1000) to a variable. This makes it easier to change this value when experimenting. • The list of primes is also precomputed and stored in the variable primes_list. This makes the code more efficient, since otherwise the list has to be computed twice. • We store the number of primes of form 4n 1 and 4n 3, respectively, in the variables n1 and n3. • We make Sage print a nice message explaining what is each computed value. The function str is used to convert a numerical value into a string, and + here means string concatenation. Try different values for pmax and compare the values you get. Can you find a value of pmax for which the number of integers of form 4n 1 is greater than the number of primes of form 4n 3? Created by L. Felipe Martins. [email protected] License: http://creativecommons.org/licenses/by-nc-sa/3.0/us/ Number Theory Learning Module 2 — Prime Numbers and Integer Factorization in Sage 5 5 Problems 1. Create list comprehensions defining the following sequences, and test them in Sage: (a) The sequence of numbers n3 for 1 ¤ n ¤ 10. (b) The sequence of integers n for 1 ¤ n ¤ 30 that are composite. Hint: The operator not represents negation in Sage. (c) The sequence of prime numbers p ¤ 200 that leave remainder 1 when divided by 3. (d) The sequence of prime numbers p ¤ 200 such that 2p ¡ 1 is prime. Note: The list you will obtain repre- sents all Mersenne primes discovered from around 500BCE (by the ancient greeks) to 1876 (by the french mathematician Edouard´ Lucas). Today, the largest known examples of prime numbers are of this form. 2. The Sieve of Eratosthenes is a method to identify all primes in a range of integers. In each step, the multiples of the primes 2, 3, 5, 7, .
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages6 Page
-
File Size-