Recursive function (help me understand it by pen and paper) - c++

First of all I have to say that I can use recursive functions on easy examples like Fibonacci, but I can't understand how to dry run (solve with pen and paper) this recursion :
#include<iostream>
using namespace std;
int max(int a, int b)
{
if(a>b)return a;
return b;
}
int f(int a, int b)
{
if(a==0)return b;
return max( f(a-1,2*b), f(a-1,2*b+1) );
}
int main()
{
cout<<f(8,0);
}
How do I do this with pen and paper, with say, a = 5 and b = 6?

We have always a depth of a (8)
Each invocations calls itself 2 times, once 2b and once 2b+1 is passed
The greater result of both calls is returned
As 2b + 1 > 2b only the right site of the max call is meaningful (2b + 1)
Now lets do the first iterations mathematically:
2 * b + 1 = 2^1 * b + 2^0
2 * (2^1 * b + 2^0) + 1 = 2^2 * b + 2^1 + 2^0
2 * (2^2 * b + 2^1 + 2^0) + 1 = 2^3 * b + 2^2 + 2^1 + 2^0
2 * (2^3 * b + 2^2 + 2^1 + 2^0) + 1 = 2^4 * b + 2^3 + 2^2 + 2^1 + 2^0
As you can see there is a system behind it. Because b = 0 for the first iteration, we can ignore the left side. The final value is thus:
2^0 + 2^1 + 2^2 + 2^3 + 2^4 + 2^5 + 2^6 + 2^7
=
1 + 2 + 4 + 8 + 16 + 32 + 64 + 128
=
255
If we run the programm we get the exact same value

Just to give some information there are algorithms that use a little more complex parameters, one basic example would be mergesort
Merging is simple:
Take two elements one from each array A and B.
Compare them and place smaller of two (say from A) in sorted list.
Take next element from A and compare with element in hand (from B).
Repeat until one of the array is exhausted.
Now place all remaining elements of non-empty array one by one.
Maybe you can find this doc useful
Or maybe this one

Assuming you want to analyzse the funciton on paper, I'll paste the result for f(1, 2)
f(2, 1) =
max( f(1, 2), f(1, 3) ) =
max ( max(f(0, 4), f(0, 5) , max(f(0, 6), f(0, 7) ) =
max ( max(4, 5) , max(6, 7) ) =
max (5, 7) =
7
Is up to you to follow the computations
Note: I'm also assuming you didn't miss a parenthesis here: 2*b+1

Related

Use overload operators to calculate two class

Overload op +
Constructor
Supposed output:
b= + 8 * x^3 + 6 * x^2 + 4 * x + 2;
c= + 3 * x^2 + 1;
d= + 8 * x^3 + 9 * x^2 + 4 * x + 3
I try to use a for loop inside the overload function + to add the two Polynomial classes up. But the overload function does not work.
The d is supposed to be a function that b and c add up to.

Working with large integer data type, possibly larger than 10^18

What data type or what ways can I store large integers possibly greater than 10^18 and How can I efficiently improve my approach to the problem?
I am currently working on a problem that asks to find the sum of all divisors d(k) given that:
N N
S(N) = ∑ ∑ d(j*i)
i=1 j=1
with the largest value of N = 10^9 and largest divisor (10^9 * 10^9). Stored in:
long long int
The program solves and slows down at N = 10^3 and anything higher takes up to much memory and crashes.
I used a for loop for the values of i and j that calculates the values of d(k) > d(i * j) and store it in a vector:
{d(1 * 1), d(1 * 2), ... , d(i>N * j>N)}
Then a separate function that finds all divisors of d(k) then adds them up:
d(1) = 1
d(2) = 1 + 2 = 3
d(3) = 1 + 3 = 4
d(4) = 1 + 2 + 4 = 7
...
d(i>N * j>N)
S(N) = d(1) + d(2) + d(3) + d(4) + ... + d(i>N * j>N)
Any values of N greater than 10^5 gets displayed as S(N) mod 10^9.

collecting rational, non-symbolic coefficients in SymPy

If I have an expression like the following 2 * a + 2 * b + 1, is there a way to effectively factor out the 2 without substituting it for a symbol?
Edit: My own answer below does not seem to work for rational coefficients, e.g., collect(a / 2 + b / 2 + 1, Rational(1, 2)) returns a / 2 + b / 2 + 1.
I used collect_const.
a, b = symbols('a b')
test = 2 * a + 2 * b + 1
collect_const(test, 2)

Calculating time complexity of a recursive function having a loop inside it

I was working on a simple problem and I came up with a recursive function in C++, below is my function.
void test(int arr[],int n,int x = 0){
cout<<arr[x];
for(int i = x+1;i < n;i++){
test(arr, n, i);
}
}
I wonder what will be the time complexity of the above function if anyone can calculate the time complexity for the above method it will be a great help in improving my function.
You can write its recurrent relation likes the following:
T(n) = T(n-1) + T(n-2) + ... + T(1) + 1
Indeed T'(x) is T(n - x) and T(1) = 1 (The last one in the realtion is is for cout). We can see:
T(2) = T(1) + 1 = 2
T(3) = T(2) + T(1) + 1 = 2 + 1 + 1 = 4
T(4) = 4 + 2 + 1 + 1 = 2^2 + 2^1 + 2^0 + 1 = 8
T(5) = 8 + 4 + 2 + 1 + 1 = 2^3 + 2^2 + 2^1 + 2^0 + 1 = 16
.
.
.
T(n) = 2^{n-2} + 2^{n-1} + ... + 2^0 + 1 = 2^{n-1}
Hence, T(n) = \Theta(2^n).

Permutation of a number's digits

Consider number 194 declared as type int
Is it possible to obtain it's digits permutations like other ints efficiently?
Number: 194
419 int
491 int
914 int
941 int
I am using the next_permutation however it only works with arrays. So I thought it wouldn't be wise to convert int to an int array (?!) then obtain the permutation as an array and convert it to it.
Any suggestions?
Permuting the digits is basically a string-operation, not a (simple) mathematical operation. Converting to an array (string) and then using next_permutation() sounds more sensible than trying to do it mathematically.
Here's the mathematical version - without intermediate values saved:
int a = 194;
int b = (a / 100) * 100 + (a % 10) * 10 + ((a / 10) % 10) * 1; // 149
int c = (a % 10) * 100 + ((a / 10) % 10) * 10 + (a / 100) * 1; // 491
int d = (a % 10) * 100 + (a / 100) * 10 + ((a / 10) % 10) * 1; // 419
int e = ((a / 10) % 10) * 100 + (a / 100) * 10 + (a % 10) * 1; // 914
int f = ((a / 10) % 10) * 100 + (a % 10) * 10 + (a / 100) * 1; // 941
With intermediate values, it's a little easier to see what's going on (except that I generated different assignments for b through f this time).
int a = 194;
int d1 = a / 100;
int d2 = (a / 10) % 10;
int d3 = a % 10;
int a = d1 * 100 + d2 * 10 + d3 * 1; // 194
int b = d1 * 100 + d3 * 10 + d2 * 1; // 149
int c = d2 * 100 + d1 * 10 + d3 * 1; // 914
int d = d2 * 100 + d3 * 10 + d1 * 1; // 941
int e = d3 * 100 + d1 * 10 + d2 * 1; // 419
int f = d3 * 100 + d2 * 10 + d1 * 1; // 491
Use the next_permutation() mechanism; it will generalize to 4-digit and 5-digit and N-digit numbers where this will not.
You'd first have to extract each decimal place's value first: either by converting it to a character array (itoa()) or by writing a small for loop that divides the number by powers of 10. Once you have the digits separated, you can write a loop to generate the permutations.
Getting the permutations of the decimal digits will require you to interact with the number as a decimal, so power-of-2 manipulations are probably not going to help much here.
My suggestion would be:
1. Convert number to string
2. Set up the string as a circular buffer
3. Step through the buffer progressively (each increment of the index into the circular buffer will give you one permutation)
4. Reconstruct the number from the "new" arrangement of the characters representing the digits
5. Repeat for the length of the string.
Unless you are running in a slow/resource-constrained environment, I wouldn't try to overthink the problem beyond this.
Edit:
As pointed out in the comments this doesn't generate all permutations, to do so would require adding another step at the end where the process is repeated but with progressively larger increments to the index variable.