Question

In: Computer Science

(i) T(n) denote the number of distinct ways that a postage of n cents, where n...

(i) T(n) denote the number of distinct ways that a postage of n cents, where n ≥ 4 and n is even, can be made by 4-cent and 6-cent stamps. Give a recursive algorithm (written in Python) to compute T(n) for n ≥ 4 and n is even. Briefly explain why the algorithm is correct but no formal proof is required. NOTE [4,6] is the same as [6,4] so T(10) = 1.
(ii) Now assume we have 10-cent stamps in addition to the previous 2 kinds. Find a recurrence relation, S(n), for the number of distinct ways that a postage of n cents, where n ≥ 4 and n is even, can be made by 4-cent, 6-cent and 10-cent stamps.

Solutions

Expert Solution

1. For the first case, when the allowed stamps are of 4 and 6 cents:
    The recurrence relation mathematically can be defined as follows:
T(0) = 1
T(<0) = 0
T(n) = T(n-4) + T(n-6)
where n = postage amount provided in input.
But to be more specific and remove repetitive ways, it needs to be defined with some conditions for more valid solutions:

Code in python:

# Returns the number of ways we can use
# [4,6] cent stamps to get the sum of postage
def func(stamps, m, postage ):

    # If postage = 0 then there is 1
    # solution (do not include any stamp)
    if postage == 0 :
        return 1

    # If postage< 0 then no
    # solution is possible
    if (postage < 0):
        return 0;

    # If there are no stamps and postage
    # is more than 0, then no
    # solution is possible
    if (m <=0 and postage >= 1):
        return 0

    # func is sum of ways (i)
    # including 4 or 6 (i.e. s[i]) (ii) excluding 4 or 6 (i.e. s[i])
    return func( stamps, m - 1, postage ) + func( stamps, m, postage-stamps[m-1] );

# Driver program to check above code
arr = [4,6]
m = len(arr)
print(func(arr, m, 12))

To prove the solution to be correct:

This type of recurrence is as same as that of the coin change problem solved using dynamic programming. Here by using the stamps array and a pointer to it, we ensure that once we done iterated using the current stamp amount at m position so that no two similar combinations will be there and we can proceed with the next stamp amount. To dry run the above solution:

for input of func(10):


2. As we have used a more generic solution above, hence now we can adjust the soltion code for the case of present stamps of 4,6 ans 10 cents by changing the stamps to have [4,6,10] stamps values, as changes done in driver code below :

# Driver program to check above code
# Returns the number of ways we can use
# [4,6,10] cent stamps to get the sum of postage
arr = [4,6,10]
m = len(arr)
print(func(arr, m, 12))


Related Solutions

(i) T(n) denote the number of distinct ways that a postage of n cents, where n...
(i) T(n) denote the number of distinct ways that a postage of n cents, where n ≥ 4 and n is even, can be made by 4-cent and 6-cent stamps. Find a recurrence relation T(n). NOTE [4,6] is the same as [6,4] so T(10) = 1 so T(n) is NOT T(n-4)+T(n-6) (ii) Now assume we have 10-cent stamps in addition to the previous 2 kinds. Find a recurrence relation, S(n), for the number of distinct ways that a postage of...
Exercise 4. Let P (n) be the statement that a postage of n cents can be...
Exercise 4. Let P (n) be the statement that a postage of n cents can be formed using just 4-cent stamps and 7-cent stamps. The parts of this exercise outline a strong induction proof that P(n)is true for n≥18. a) Show statements P(18), P(19), P(20), and P(21) are true, completing the basis step of the proof. b) What is the inductive hypothesis of the proof? c) What do you need to prove in the inductive step? d) Complete the inductive...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median...
Let An = {ai} n i=1 denote a list of n distinct positive integers. The median mA of An is a value in An such that half the elements in An are less than m (and so, the other half are greater than or equal m). In fact, the median element is said to have a middle rank. (a) Develop an algorithm that uses Sorting to return mA given An. (6%) (b) Now assume that one is given another list...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum...
Let τ (n) denote the number of positive divisors of n and σ(n) denote the sum of the positive divisors of n (as in the notes). (a) Evaluate τ (1500) and σ(8!). (b) Verify that τ (n) = τ (n + 1) = τ (n + 2) = τ (n + 3) holds for n = 3655 and 4503. (c) When n = 14, n = 206 and n = 957, show that σ(n) = σ(n + 1).
Prove that it is possible to make up any postage of n-cents using only 5-cent and...
Prove that it is possible to make up any postage of n-cents using only 5-cent and 9-cent stamps for n ≥ 35.
Prove by induction on n that the number of distinct handshakes between n ≥ 2 people...
Prove by induction on n that the number of distinct handshakes between n ≥ 2 people in a room is n*(n − 1)/2 . Remember to state the inductive hypothesis!
In a sequence of independent flips of a fair coin, let N denote the number of...
In a sequence of independent flips of a fair coin, let N denote the number of flips until there is a run of three consecutive heads. Find P(N ≤ 8). (Should write out transition matrix.)
Let an denote the number of different ways to color the walls of a five-sided room...
Let an denote the number of different ways to color the walls of a five-sided room with n colors if you insist that two walls that meet at a corner must be assigned different colors. (i) compute a1, a2 and a3 directly (ii) Find the formula for an
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected
Let E(n) denote the expected return on asset i and B. denote the corresponding beta. In addition, let E(rm) denote the expected return on the market portfolio and Bm denote the corresponding beta. Define and sketch the Security Market Line (SML). Hint: Use E(rm) - r = 8%, r = 3%, B. = 1.25 and B2 = 0.6.
I have 5 distinct red cards and 4 distinct black cards. a) How many ways can...
I have 5 distinct red cards and 4 distinct black cards. a) How many ways can I choose 1 card? b) How many ways can I choose 1 red card then 1 black card? c) How many ways can I choose 2 cards? Consider a full standard deck of 52 distinct cards. a) How many arrangements of a standard deck of cards are there? That is, what is the total number of possible shuffles? b) How many ways can I...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT