Homework #5
CS 107 - Introduction to Scientific Computation
Homework #5

Due: Wednesday 10/2 at the beginning of class
Submit from your .m file directory using the command: 
submit107 hw5  

1. Statistics: Create and submit functions myMin, myMax, myMedian, myMean, and myStdDev. Each should take a floating-point number array of arbitrary dimensions as input, and return the minimum, maximum, median, mean, and standard deviation, respectively. In the case of myMin and myMax, also return the single-dimensional index of the minimum and maximum values, respectively. Each implementation should make use of at least one loop, except myMedian, which can rely on MATLAB's sort function. (Note: Use the biased standard deviation formula where you divide by N (rather than N-1).)

2. Find:  Exercise 8.4.  In addition, handle matrix input as well.  Be sure to reread "Single-Number Indexing of Arrays" on p. 161 and try it out before you begin.  Submit function myfind.  Hint: Note the different output and output dimensions for find([1 0 1 1]) and find ([1 0; 1 1]).

3. Improving primeFactors: Exercise 8.14.  Submit both the modified smallestfactor and primeFactors functions. 

Example 1: Suppose n is 45. The function smallestfactor will try 2 and 3, finding 3 divides 45 evenly.  We add 3 to factors, divide 45 by 3 yielding the new n value 15, and repeat.  In repeating, we again call smallestfactor, again trying 2, then 3, and again find that 3 divides 15 evenly.  We again add 3 to factors, divide 15 by 3 yielding the new n value 5, and repeat.   In repeating, we again call smallestfactor, again trying 2, then 3, then 4, then 5 and find that 5 divides 5 evenly.  We add 5 to factors, divide 5 by 5 yielding the new n value 1, and then terminate the loop.  Notice that the bold steps preceding are unnecessary, wasted computation.  Once we find a candidate factor doesn't divide n evenly, we know it never will.  For this reason, it makes sense to add a parameter to smallestfactor so that we can pick a starting point other than 2.  Add an argument to smallestfactor that defines the first potential factor of the iteration (in place of 2).  Next, change primeFactors so that, on the first call to smallestfactor, 2 is used for this argument, but on subsequent iterations, the argument is set to the previous smallest factor found.  In the preceding example, this would mean that k would start at 2, 3, and 3, for the three iterations.

Example 2: Let's assume, we've correctly modified the algorithm.  Here is a summary of the algorithm's execution with n = 315: n = 315, check for factor k = 2(no), 3(yes), n = 105, next iteration, check for factor k = 3(yes), n = 35, next iteration, check for factor k = 3(no), 4(no), 5(yes), n = 7, next iteration, check for factor k = 5(no), 6(no), 7(yes), n = 1, termination with the list of factors [3, 3, 5, 7].

Hint: This is a really simple modification once you understand what's being requested.  Very little change to the given code will be necessary.

4. Prime List: Given a list length n >= 0, create function primeList to return a vector the first n prime numbers.  Submit function primeList.  Note: primeList(0), primeList(1), and primeList(5) should yield [], [2], and [2, 3, 5, 7, 11], respectively.  You may use any means to test to see if a number is prime, e.g. exercise #3 above.

Homework comment: Note how the author varies between all lowercase names and lower camel case names.  When not seeking to be consistent with the text, I prefer lowerCamelCase.