Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Big O notation wikipedia , lookup
Line (geometry) wikipedia , lookup
List of prime numbers wikipedia , lookup
Halting problem wikipedia , lookup
Mathematics of radio engineering wikipedia , lookup
Elementary mathematics wikipedia , lookup
Factorization of polynomials over finite fields wikipedia , lookup
CS 173: Discrete Structures, Spring 2014 Homework 8 This homework contains 3 problems worth a total of 34 points. It is due on Wednesday, April 16, at 11:59 PM. 1. Induction with Inequalities [12 points] Use strong induction to prove that 1 23 + 1 33 +···+ 1 n3 ≤ 5 8 − 1 n for all integers n ≥ 2. 2. Smallest Multiple [16 points] This problem is based on Problem 5 at http://projecteuler.net. It asks, “2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder. What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?” We will analyze two algorithms which find the smallest positive number that is divisible by all integers between 1 and n. Hints: You may assume (without proof) that log n! ∼ n log n and log log n ≪ log n. You can also use the fact that the prime number theorem gives an asymptotic approximation for the number of primes less than or equal to any real number x as lnxx . (a) (6 points) The first algorithm is inspired by the idea that we’re looking for the LCM of the set 2, ..., n. We have an easy method to calculate the LCM. Recall that the Euclidean algorithm computes the GCD of two numbers and that lcm(a, b) = ab . gcd(a, b) Euclid’s algorithm can “get lucky” and run very quickly on some inputs (e.g., a = 2b), but for many inputs, it is slower. A good approximation for how it behaves in most cases is O(log(min(a, b))). Consider the following solution: 01 Foo(n) 02 lcm = 1 03 for i = 2 to n 04 lcm = lcm*i/Euclid(lcm,i) 05 return lcm What is the big-O running time of Foo? Briefly justify your answer. (b) (6 points) The second algorithm is inspired by prime factorization. We must only keep track of the maximum prime exponents among the set. For example, LCM(36, 15, 40) = LCM(22 · 32 , 3 · 5, 23 · 5) = 23 · 32 · 5 = 360. Computing the prime factorization for each number 2, ..., n is slow. Instead, we can just go hunting for maximum powers. Consider the example of finding the smallest multiple of 2, ..., 10. The prime numbers less than 10 are 2, 3, 5, 7. The maximum power of 1 2 that is less than 10 is 23 = 8. The maximum power of 3 that is less than 10 is 32 = 9. The maximum powers of 5 and 7 are 51 and 71 , respectively. Thus, the smallest multiple of the whole set is then 23 · 32 · 51 · 71 = 2520. We want one more piece. We only needed to check maximum powers of primes, so we could use a list of primes to check. Assume we use the simple Sieve of Eratosthenes1 , which you may assume has running time O(n log log n). Consider the following pseudocode: 01 Bar(n) 02 A = new array of size n 03 lcm = 1 √ 04 for i = 2 to n 05 if A(i) 06 for j = i2 , j not exceeding n, increment j by i 07 A(j) = false 08 lcm = lcm*floor(log(n)/log(i)) 09 return lcm Notice that line 06 can start at i2 because all earlier values are already crossed off, since they are multiples of some number smaller than i. Assume the language is such that loop in lines 06-07 will leave j as the highest multiple of i which is less than or equal to n (some languages would leave it one higher, and we would have to subtract). What is the big-O running time of Bar ? Briefly justify your answer. (c) (4 points) Which of the two algorithms is faster? Briefly justify your answer. 1 http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes 2 3. Largest subarray [6 points] For this problem, we will measure the size of a subarray by the sum of its elements. For example, consider the array A=[5 -1 0 3 2]. The largest subarray of length 1 is [5]. The subarrays of length 2 are [5 -1], [-1 0], [0 3], and [3 2], the largest of which is [3,2]. The largest subarray of length 3 is [0 3 2]. The largest subarray of length 4 is [5 -1 0 3 2]. The following algorithm takes an array as input and returns the sum of its largest subarray: 01 Baz(A) 02 big = −∞ 03 for i = 1 to length(A) 04 for j = 1 to length(A) − i + 1 05 sum = 0 06 for k = j to j + i − 1 07 sum = sum + A(k) 08 if sum > big 09 big = sum 10 return big For an input of length n, how many times is line 7 executed? Your answer should include both an exact answer and a big-O answer, as well as a brief justification/work. 3