Question: The following algorithm, CountMultiples, takes as input a list of n 1 distinct positive integers and returns the number of list elements that are a
The following algorithm, CountMultiples, takes as input a list of n 1 distinct positive integers and returns the number of list elements that are a multiple of some prior list element. For example, on the input 2, 6, 5, 9, 8, 3, 15, the algorithm would return 3 because there are 3 list elements that are a multiple of some prior list element:


3. The following algorithm, CountMultiples, takes as input a list of n 2 1 distinct positive integers and returns the number of list elements that are a multiple of some prior list element. For example, on the input 2,6,5,9, 8,3,15, the algorithm would return 3 because there are 3 list elements that are a multiple of some prior list elen . 6 is a multiple of 2 8 is a multiple of 2 . 15 is a multiple of 5 (and it is a multiple of 3) procedure CountMultiples(a1,.. , an: n2 1) 1. if n-1 then return 0 2. count 0 3. m-Count Multiples(ai,.... an-1) .fori 1 to n 1 5.if a, divides an then return m+1 6. return m (a) Prove that CountMultiples is correct. Proof: We can prove that CountMultiples is correct for any input of size n, using strong induction on n. For the base case, when n-1, CountMultiples returns 0 in line 1 because weak (regular) is correct oW Sl e as the inductive hypothesis that CountMultiples is correct on all inputs of size and we will show that it is correct on all inputs of size n, where n >1. Let an be an arbitrary input of size n. 3. The following algorithm, CountMultiples, takes as input a list of n 2 1 distinct positive integers and returns the number of list elements that are a multiple of some prior list element. For example, on the input 2,6,5,9, 8,3,15, the algorithm would return 3 because there are 3 list elements that are a multiple of some prior list elen . 6 is a multiple of 2 8 is a multiple of 2 . 15 is a multiple of 5 (and it is a multiple of 3) procedure CountMultiples(a1,.. , an: n2 1) 1. if n-1 then return 0 2. count 0 3. m-Count Multiples(ai,.... an-1) .fori 1 to n 1 5.if a, divides an then return m+1 6. return m (a) Prove that CountMultiples is correct. Proof: We can prove that CountMultiples is correct for any input of size n, using strong induction on n. For the base case, when n-1, CountMultiples returns 0 in line 1 because weak (regular) is correct oW Sl e as the inductive hypothesis that CountMultiples is correct on all inputs of size and we will show that it is correct on all inputs of size n, where n >1. Let an be an arbitrary input of size n
Step by Step Solution
There are 3 Steps involved in it
To prove the correctness of the CountMultiples algorithm and analyze its time complexity well proceed step by step a Prove that CountMultiples is corr... View full answer
Get step-by-step solutions from verified subject matter experts
