{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Data Str &amp; Algorithm HW Solutions 8

# Data Str &amp; Algorithm HW Solutions 8 - P 1 = 2 P 2 =...

This preview shows page 1. Sign up to view the full content.

8 Chap. 2 Mathematical Preliminaries 2.13 void allpermute(int array[], int n, int currpos) { if (currpos == (n-1)} { printout(array); return; } for (int i=currpos; i<n; i++) { swap(array, currpos, i); allpermute(array, n, currpos+1); swap(array, currpos, i); // Put back for next pass } } 2.14 In the following, function bitposition(n, i) returns the value (0 or 1) at the i th bit position of integer value n . The idea is the print out the elements at the indicated bit positions within the set. If we do this for values in the range 0 to 2 n 1 , we will get the entire powerset. void powerset(int n) { for (int i=0; i<ipow(2, n); i++) { for (int j=0; j<n; j++) if (bitposition(n, j) == 1) cout << j << " "; cout << endl; } 2.15 Proof : Assume that there is a largest prime number. Call it P n ,the n th largest prime number, and label all of the primes in order
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: P 1 = 2 , P 2 = 3 , and so on. Now, consider the number C formed by multiplying all of the n prime numbers together. The value C + 1 is not divisible by any of the n prime numbers. C + 1 is a prime number larger than P n , a contradiction. Thus, we conclude that there is no largest prime number. ✷ 2.16 Note: This problem is harder than most sophomore level students can handle. Proof : The proof is by contradiction. Assume that √ 2 is rational. By de f ni-tion, there exist integers p and q such that √ 2 = p q , where p and q have no common factors (that is, the fraction p/q is in lowest terms). By squaring both sides and doing some simple algebraic manipula-tion, we get 2 = p 2 q 2 2 q 2 = p 2 Since p 2 must be even, p must be even. Thus,...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online