**2-1.** What value is returned by the following function? Express your answer as a function of n. Give the worst-case running time using the Big Oh notation.

1 2 3 4 5 6 7 |
function mystery(n) r := 0 for i := 1 to n − 1 do for j := i + 1 to n do for k := 1 to j do r := r + 1 return(r) |

**Solution:** To find out what value is returned just represent this function mathematically and simplify it.

The complexity is

**2-10.** Prove that

**Solution:** : For

: For

**2-34.** Assume that Christmas has n days. Exactly how many presents did my “true love” send me? (Do some research if you do not understand this question.)

**Solution:** I made this table of the first three days:

1 2 3 4 |
Day Presents \sum of Presents 1 1 1 2 1 + 2 1 + 1 + 2 3 1 + 2 + 3 1 + 1 + 2 + 1 + 2 + 3 |

We can break this down into sub steps. On the ith day we get presents.

The total amount of presents is:

We can be simplified as:

**2-39.** Prove the following identities on \logarithms:

(a) Prove that

(b) Prove that

(c) Prove that

(d) Prove that

**Solution:**

(a) The first proof is straight forward:

(b) The trick here is to see that . Therefore we can use the identity from (a):

(c) Here you try to form around a variable (z) to get the right term:

(d) The last one is quite easy. Now we have just to on the equation and use the identity from (b) and we get:

**2-44.** We have 1,000 data items to store on 1,000 nodes. Each node can store copies of exactly three different items. Propose a replication scheme to minimize data loss as nodes fail. What is the expected number of data entries that get lost when three random nodes fail?

**Solution:** My first idea was a kind of a binary tree which then evolved into this:

The idea is to save on each node the value of the corresponding left and right nodes and of course the main value. In the last nodes we got some free space were we can save *item10* because it isn’t backed up yet.

So what happens if three nodes fall out? There are various scenarios.

1. 3 corresponding nodes fall out, e.g. Node 1, 2 and 3. Then Item 11 and 12 are completely lost. (Remember Item10 is saved further down again)

2. A node and its parent fall out, e.g. Node 1 and 2. Here we just lose Item 11.

3. A random node falls out, e.g. Node 3. No problem whatsoever. We can retrieve Item12 from Node 1.

We got about 500 free storages in the last row of nodes which could be used to backup half of the total items again. Which would reduce our loss further

**2-46.** You have a 100-story building and a couple of marbles. You must identify the lowest floor for which a marble will break if you drop it from this floor. How fast can you find this floor if you are given an infinite supply of marbles? What if you have only two marbles?

**Solution:** The first case with infinite supply of marbles is very easy. We just do a binary search on the story building, i.e. we need about 7 marbles.

The second case is a bit more demanding. I would start to try to minimize the possible interval as much as possible by starting with a marble in the middle of the whole interval, i.e. at the 50th story.

If it’s broken we have to work our way up from 1 to at max 50.

If it’s still alive we can cut the next interval into half till we find our floor.

**2-50.** A Ramanujam number can be written two different ways as the sum of two cubes—i.e. , there exist distinct a, b, c, and d such that . Generate all Ramanujam numbers where a, b, c, d < n.
**Solution:**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
def RamanujamNumbersBF(n): numbers = [] for a in xrange(0, n): for b in xrange(0, n): for c in xrange(0, n): for d in xrange(0, n): if a != b and a != c and a != d and b != c and b != d and c != d: if a ** 3 + b ** 3 == c ** 3 + d ** 3: numbers.append((a, b, c, d)) return numbers def RamanujamNumbersDP(n): numbers = [] Ds = dict() # Init List for d in xrange(0, n ** 3): Ds[d] = False # Fill list for d in xrange(0, n): Ds[d**3] = d for a in xrange(0, n): for b in xrange(0, n): for c in xrange(0, n): if a != b and a != c and b != c: d = a ** 3 + b ** 3 - c ** 3 if a != d and b != d and c != d and d >= 0 and d < n ** 3: if Ds[d] != False: numbers.append((a, b, c, Ds[d])) return numbers print "Brute Force" print RamanujamNumbersBF(50) print "Dynamic Programming" print RamanujamNumbersDP(50) |

The DP approach works must faster than raw BF which you can see quite fast because complexity of BF is and DP only takes

2-44 you must prove it mathematically why having 3 copies in 3 different random buckets is the best solution. forget about the implementation. the question is not how you would implement it.