{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CSC375 Homework2 George Corser 2009-01-21

CSC375 Homework2 George Corser 2009-01-21 - CSC 375...

This preview shows pages 1–3. Sign up to view the full content.

CSC 375 Homework2 George Corser 2009 January 21 2.8 long fact(int n) { // compute n! without recursion // to fit n! in a long variable, require n <=12 Assert((n >= 0) & (n long product = 1; for (int i=n; i>0; i--) product = product * i; return product; } 2.9 // Randomly permute the n values of an array, recursively template<class Elem> void permute(Elem array[], int n) { if (n>0) { swap(array, n-1, Random(n)); permute(array, n-1); } } 2.12 No. In theory, if double variables behaved like true real numbers, the function would never terminate because real numbers can be split in half an infinite number of times. Yes . In practice, the function will terminate because eventually the function will reach the limit of the precision of a double . 2.15 Assume the contrary, that there are a finite number of prime numbers. If that were true, then there would be a smallest and a largest prime number. Let’s call the smallest prime number P 1 and the largest P n . If we multiply all the primes and add 1 to that product, we would get a number N, where N = P 1 *P 2 *P 3 * … *P n + 1. N divided by any number, prime or otherwise, would leave a remainder of at least 1. Since N cannot be evenly divided by any number, N is prime. Since this contradicts our original assumption, we can conclude that the number of primes is infinite . 2.17 The three expressions below are equal because they are all equivalent summations—that is, summations of the same numbers, 1 through n. ∑ = n i i 1 = 1 + 2 + … + (n–1) + n (terms in ascending order) = n + (n-1) + … + 2 + 1 (terms in descending order) ∑ = + - n i i n 1 ) 1 ( = n-1+1 +n-2+1 +… + n-n+1 = n + (n-1) + … + 2 + 1 ∑ = - n i i n 0 ) ( = n-0 + n-1 + … + n-(n-1) + n-n = n + (n-1) + … + 2 + 1 + 0 2.19

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Eq. 2.7 : = n i 1 1/2 i = 1 – 1/2 n S(n) = 1 – 1/2 n BC: n=1. = 1 1 i 1/2 i = 1/2 1 = ½ S(n) = 1 – 1/2 1 = 1 – ½ = ½ base case IH: S(n-1) = - = 1 1 n i 1/2 i = 1 – 1/2 n-1 induction hypothesis IS: S(n) = S(n-1) + 1/2 n induction step = 1 – 1/2 n-1 + 1/2 n = 1 – 2/2 n + 1/2 n = 1 – 1/2 n proved by induction – result is true for n 2.20 Eq. 2.8 : = n i 0 2 i = 2 n+1 – 1 S(n) = 2 n+1 – 1 BC: n=1. = 1
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 4

CSC375 Homework2 George Corser 2009-01-21 - CSC 375...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online