Use the tree and table method to calculate that $ Theta $ Time complexity for $ π (π) = 4π (π – 5) + 7 $. Accept $ T (N) = $ 7 for all $ 0 leq N leq 4 $. Accept $ N $ is a multiple of $ 5 $. Complete the following table and finish the calculations outside.

# Tag: Recursion

## Predictability – Improve the calculation of binary recursion

I'm trying to write a program for the infamous egg drop puzzle with recursion in Python. If you do not know the problem, you can find it here:

https://code.google.com/codejam/contest/dashboard?c=32003#s=p2

One solution to this puzzle would be to use a recursive function that returns the maximum allowable floors of a building $ Solvable (F, D, B) $ be true, represented as such, as a function $ f $::

$ f (D, B) $ $ = $ $ 1 $ $ + $ $ f (D – 1, $ $ B – 1) $ $ + $ $ f (D – $ 1, $ B) $

… Where $ D $ is the number of drops remaining and $ B $ is the number of allowed breaks. This solution uses how an egg can either break open $ f (D – 1, $ $ B – 1) $ Floors below and $ f (D – $ 1, $ B) $ Floors above.

As you can see, this leads to the formation of a binary recursive function, as shown above. Combined with the fact that we know that $ f (1, B) $ $ = $ $ 1 $ for all values ββof $ B $, and how $ f (D, 1) $ $ = $ $ D $ for all values ββof $ D $This problem should not be a problem for a program that can handle recursion very well. However, Python has the talent to be unable to process recursion and some other languages.

As such, I would like to know if the maximum floors are returned by function $ f $ can be determined for a range of values, where $ 1 <= D, B <= $ 2,000,000,000 in python.

Here is a list of techniques I've tried so far without success:

1. Memoization (caching values ββof $ f (D, B) $)

2. Since we know that $ f (d, b) $ to the $ d <= b $ corresponds $ f (d, b) $ Where $ d = b $With $ d $ kept constant, we can reduce the number of cached pairs of $ d $ and $ b $

3. When $ d = b $, $ f (d, b) $ is equivalent to $ 2 ^ d – $ 1This eliminates the need for binary recursion for $ d = b $

## Primitive recursion of two functions $ g, h $

What function is created when using primitive recursion? $ PR (g, h) $ to

$ begin {align} g: mathbb {N} to mathbb {N}, quad g (n) & = mathrm {zero} _1 (n) \

h: mathbb {N} ^ 3 to mathbb {N}, quad h (n, m, l) & = P_2 ^ {(3)} (n, m, l) + mathrm {succ} circ P_2 ^ {(3)} (n, m, l) + P_3 ^ {(3)} (n, m, l) end {align} $Where $ P_i ^ j: mathbb {N} ^ j to mathbb {N} $, $ P_i ^ j (x_1, dots, x_j) = x_i $ for all $ 1 leq i leq j $,$ j in mathbb {N} $, $ j geq 1 $ and $ PR (x, y) $ is the primitive recursion: Let $ k geq 0 $, $ g: mathbb {N} ^ k to mathbb {N} $ and $ h: mathbb {N} ^ {k + 2} to mathbb {N} $, for all $ x_i, t in mathbb {N} $, then $ f: mathbb {N} ^ {k + 1} to mathbb {N} $ is defined by

I) $ f (x_1, points, x_k, 0): = g (x_1, points, x_k) $

ii) $ f (x_1, points, x_k, t + 1) = h (x_1, points, x_k, t, f (x_1, points, x_k, t)) $

My idea: $ f (x, 0) = g (x) = null_1 (x) = 0 $, $ f (x, t + 1) = h (x, t, f (x, t)) = t + t + 1 + f (x, t) = 2t + 1 + f (x, t) $, but what is $ f (x, t) $ and how do I derive that?

## Recursion – Problems with the implementation of the recursive function when evaluating R-fishermen

I'm trying to do fisher scoring on simulated i.i.d. Poisson data, but I'm getting a stack overflow error. I made a few simple prints of the function and found that the estimate does not change after the first iteration.

```
fs_pois <- function(data, true, guess) {
if (abs(guess-true) < 0.01) return(guess)
else return(fs_pois(data, true, guess + (mean(data)*exp(-guess))-1))
}
data <- rpois(100,lambda=0.34)
fs_pois(data, 0.34, 0.36)
```

Does anyone know what's going on?

Very appreciated

## Recursion – recursive sentence for a string

Given the definition:

```
Let Ξ£ be the alphabet defined as follows: Ξ£ = {e, l, v}
We now define the set of strings P according to the following:
BASIS STEP: If x β Ξ£ then x β P
RECURSIVE STEP: If w β P and x β Ξ£, then xwx β P
```

How to show that string

levelis in P.

I understand what is being asked and that I need to use the recursive step to generate the target string. But I just don't know exactly how to do it efficiently and accurately. Any help with an explanation is greatly appreciated.

## Turing machines – Why doesn't the recursion theorem prove that there is an undecidable finite set?

I have created something similar to Sipser's proof of the undecidability of $ A_ {TM} $ (Theorem 6.5), which "proves" the undecidability of a set that must be finite. It's probably wrong, but I can't figure out why for my life.

$ A_R: = { langle M rangle | M = R land M text {accepts "foobar"} } $

Accept $ D $ decides $ A_R $, $ R: = $ With each entry:

- Get your own description from the recursion sentence $ langle R rangle $
- run $ D $ on $ langle R rangle $
- Do the opposite of what $ D $ says. If it declines, accept it. If it accepts, decline.
$ R $ contradicts something $ D $ says about $ R $, So $ D $ cannot be a decision maker. (i.e. if $ D $ accepted $ R $. $ R $ should accept "foobar", however $ R $ rejects all strings. If $ D $ Rejects $ R $. $ R $ should reject "foobar", however $ R $ accepts all strings).

But because $ A_R $ can only contain $ R $. $ A_R = Emptyset lor A_R = { langle R rangle } $, In any case, it is finite $ A_R $ is decidable.

So what's wrong with the first argument? A few ideas come to mind:

- I lose an important detail by making an informal argument.
- Something strange in the recursive relationship between building $ A_R $ and referencing $ D $, (This could possibly be avoided by limiting it to the subset of R-length TMs that we could "guess" before creating R – and it would still be finite)
- Logic gadgets (a la liar paradox)

But I just don't see exactly where the problem is.

## scala – Which is better for assigning values ββto function cards: tail recursion from foreach?

I want to assign functions from my function library (`Map`

called as `chain`

) to enter a string `str`

, That also works (`Twitter.removeRT`

, …) are regular expressions that replace substrings in the input `str`

, I think it is better to save these regex functions in Map as in my example.

## So the code:

Schwanzrekursionsvariante

```
def filterTwitter2 (str: String): String = {
@scala.annotation.tailrec
def recFilter(str: String, chain: Map(String, (String) => String)): String = {
chain.headOption match {
case Some(v) =>
val filteredString = v._2(str)
recFilter(filteredString, chain.tail)
case None => str
}
}
val chain = Map(String,(String) => String)(
"f1"-> Twitter.removeRT,
"f2"-> Twitter.removeNickName,
"f3"-> Twitter.removeURL,
"f6"-> Emoticons.removePunctRepetitions,
"f7"-> Emoticons.removeHorizontalEmoticons,
"f9"-> Emoticons.normalizeEmoticons,
"f10"-> Beautify.removeCharRepetitions,
"f12"-> Beautify.removeNSpaces
)
recFilter(str, chain)
}
```

`foreach`

variant

```
def filterTwitter (str: String): String = {
var tmp = str
val chain = Map(String,(String) => String)(
"f1"-> Twitter.removeRT,
"f2"-> Twitter.removeNickName,
"f3"-> Twitter.removeURL,
"f6"-> Emoticons.removePunctRepetitions,
"f7"-> Emoticons.removeHorizontalEmoticons,
"f9"-> Emoticons.normalizeEmoticons,
"f10"-> Beautify.removeCharRepetitions,
"f12"-> Beautify.removeNSpaces
)
chain.foreach {
case (name, func) => tmp = func(tmp)
}
tmp
}
```

## So the questions:

- Is it normal to store functions in the map? What can be better for that?
- Which is better: tail recursion variant or variant with
`foreach`

? - Maybe there is a better solution to this problem?

## Algorithms – coverage interval with minimum sum intervals – DP recursion depth problem

**History:**

This year I took part in a programming contest where we had some interesting problems. I have had problems with some of them and as the next round approaches I want to clarify them.

**The problem in a nutshell**

We are given `N`

weighted intervals and a `(A,B)`

Interval. We must **Home page** the `(A,B)`

Interval with the given so that we **Minimize the total weight** (and then the number of intervals required). We have to print the sum, the number of intervals and then the intervals ourselves. If we can't cover `(A, B)`

we have to specify this as a special value (-1).

**First thoughts**

If we sort the intervals by starting time, we can do a simple 0-1 Knapsack-like DP and solve the problem. Even if the priorities were exchanged (minimizing the number as the sum), simple greed would do it.

**The limits**

Basically all intervals **Start and end are in the area 1-1 000 000**, and

**N <= 100,000**,

**My approach**

I wrote a recursive algorithm like that `0-1 Knapsack`

one in Python, in which the last selected interval was saved, so that the selection list can be restored from the DP array later. It was a `(current_interval, last_covered_day) -> (cost, last_selected_interval, last_covered_day')`

-like function. I used one `dict`

As a DP array, a normal array that is so big would violate memory restrictions and filling it up would also increase runtime (at least I thought so – but a 1000000 * 100000 array would certainly do it!). I wrote the function as recursive so it doesn't fill the entire DP array and is faster and more memory efficient.

**The problem with that**

Simple, I have `RecursionError: maximum recursion depth exceeded`

s for larger data sets – 100,000 deep recursion was simply too much. I've read on GeeksForGeeks since then that it should be possible to increase it, but I'm still not sure if it would be safe. My recursive function cannot be optimized either, so that would not work either.

**So my questions**

Is there any way to solve this problem without DP?

If no, filling out a full table is an option with them *high* Limits?

Maybe we can come up with another DP approach that doesn't use such large tables?

Is it safe to only increase the recursion depth limits for such problems?

## Recursion – How many repeated steps in a Fibonacci recursion function

How can I calculate how many repeated calls occur in a fib recursive function?

fib (n):

if n = 0: ret 0

if n = 1: ret 1

ret fib (n – 1) + fib (n – 2)

ex) if n = 5, how often fib (3), fib (2), fib (1) are repeated **total** ? We know that fib (5) and fib (4) are only called once.

## How can I solve a square root recursion equation using the recursion tree method?

$ T (n) = sqrt {n} T ( frac {n} {2}) + sqrt {n} $

I am trying to solve this question using the recursion tree method. Do we have a way to draw a recursion tree for this equation?

I just don't want to use a master or extended master set