More on Problem Solving

Factorial Computation

      Given a number n, compute the factorial of n

 

      Assume n >= 0

 

      What is factorial?

      0! = 1, 1! = 1, 2! = 1*2 = 2

      3! = 1*2*3 = 6

      4! = 1*2*3*4* = 24

 

      n! = 1*2*...*(n-1)*n, for n>=1

 The algorithm

       Observation: For n>=1, n! is (n-1)! multiplied by n

 

       Strategy: Given n, compute n! by successively computing 1!, 2!, etc. till n!

 

Algorithm Fact:

Input n

Output Fact

 

 1.  initialize fact to  1 and index to 1

 2.  do  while (index  < = n) steps 2.1 and 2.2
              2.1 fact = fact * index
              2.2 index = index + 1

end Fact

Analysis

      Is the solution correct?

 

      Loop invariant: At the beginning of each iteration,

   fact holds the partial product 1 * . . . * (index-1)

 

      When the loop terminates, index = (n+1)

    fact then holds  (1 * ... * n)

 

      Does the loop terminate?

   There is a bound function: (n + 1 index)

   The bound function always >= 0

    It decreases in each iteration

 

Efficiency Analysis

      How many operations?

    n multiplications

 

      Can we do better?

 

      Yes, there is a method using (log n) operations

Extracting the digits

Problem: Given a number, list its digits of the number from right to left (How about left to right?)

 

Example:

       If given number is 50865 then the program                        should output

                           5,6,8,0,5

 

       If the input is 0 then output is 0

 

       If the input is 7680 the output is 0,8,6,7

Solution?

One Strategy

      Divide the given number by 10. The remainder is the last digit

 

      Divide the quotient by 10. The new remainder is the next last digit

 

      Divide the new quotient by 10. The newer remainder is the next last digit

 

      and so on.

 

      Till no more digits

 

The Algorithm

Algorithm Digit_Extract:

Input N

Output List

 

      quotient = N

      do while (quotient > 0) 2.1, 2.2, 2.3
        2.1 divide the quotient by 10
        2.2 obtain the remainder and the new quotient
        2.3 Add the remainder to List
end Digit_Extract

Analysis of the Algorithm

      Is the algorithm correct?

 

      Does the algorithm terminate?

 

      How many operations?

From Digits to Numbers

      This is the inverse problem

       Given a list of digits from, left to right, of a number, obtain the number

 

      Example: 

   Given 4,7,8 the required number is 478

   Given 0,8,0,5,6, the required number is 8056

 

      Aside: The alternate problem, where the digits are given from right to left order is, less easy.

 

 

Solution Strategy

      Observation:  If the list of digits are 4,7,8 then

              4 ´ (102) + 7 ´101 + 8 ´100 = 478

     the required number

 

      This suggests the following strategy:

     Take the first number

     Multiply by 10 and add it to the next number

     Multiply the resulting number by 10 and add it

      to    the next    number

     and so on

 The algorithm

Algorithm Form_no:

Input: List

Output: d

 

       d = the first digit in List

       do while (next_digit in List) step 2.1

           2.1 d = 10*d + next_digit

end Form_no

 

       Is the algorithm correct?

 

       Does it terminate?

 

       How many number of operations?

Another Problem

Problem: Reversing the Digits of an integer

 

Examples:

      Input:  58902

      Output: 20985

 

      Input: 4300

      Output: 34

Solution Strategy

      Here is a high level strategy:

     Extract the list of digits from the number

     Obtain the new number from the list

 

      How do you extract the digits?

   Use the earlier algorithm

 

      How do you obtain the new number?

   Use the previous algorithm

 

      Are the digits reversed?

Top Down Strategy

      The above solution follows the top-down methodology

 

        Break the problem into two or more sub problems

        Obtain the solutions for the sub problems

        Combine the solutions to get the solution for the

         original problem

 

      Many Large problems can be solved using top-down approach

 

      Obtain the algorithm for the problem using the two previous algorithm

The algorithm for Digit Reversal

Algorithm Digit_Reversal

Input: N, the number to be reversed

Output: M, the reversed number

 

      q = N

      M = 0

      Do while (q > 0) steps 3.1,3.2,3.3

       3.1 rem = remainder of q by 10

       3.2 M = M*10 + rem

       3.3 q = quotient of q by 10

end Digit_Reversal

Analysis of the algorithm

      Is the algorithm correct?

 

      Does it terminate?

 

      How many number of iterations?

 

      How many number of operations?