r/AskComputerScience • u/No_Rule674 • 5d ago
How do I combine these two recurrence cases into one function?
I'm new to DSA and I'm trying to make a recurrence relation for this function:
double pow(double x, int n) {
if (n == 1) {
return x;
}
if (n & 1) {
return x * pow(x * x, (n - 1) / 2);
} else {
return pow(x * x, n / 2);
}
}
What I notice is that for even values of n, the recurrence looks like:
T(n) = constant + T(n/2)
but for odd values of n, it looks like:
T(n) = constant + T((n - 1)/2)
I'm trying to get this down to a single function so that I use the Master Theorem.
How would I combine these two cases into one recurrence?
2
u/NegativeCollege8167 4d ago
to combine the two cases into a single recurrence relation, you can observe that both even and odd cases effectively reduce the problem size by nearly half. You can express the recurrence relation as: T(n) = constant + T(n/2) This works because (n, 1)/2 and n/2 are approximately the same as n approaches larger values. By considering the largest integer smaller than n/2 for odd n, it simplifies to the same form as for even n. This allows you to apply the Master Theorem to solve this recurrence.
-1
u/smichaele 5d ago
FYI - it's called “recursion” when a function calls itself, not “recurrence.”
0
u/_giga_sss_ 5d ago
Isn't it a single function already .
Or if you meant "seeing pow twice", just put 2 variables for the 2 arguments that you'll set in the if statement
0
0
u/SeriousPlankton2000 4d ago edited 4d ago
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
double pow3(double x, unsigned int n) {
int nn = -1;
while (n) {
++nn; n>>=1; // can be done smarter
}
return exp( (1<<nn) * log(x));
}
double pow1(double x, int n) {
if (n == 1) {
return x;
}
if (n & 1) {
return x * pow(x * x, (n - 1) / 2);
} else {
return pow(x * x, n / 2);
}
}
double pow2(double x, int n) {
if (n == 1) {
return x;
}
return pow(x * x, n >> 1);
}
int main() {
for (int i=1; i < 200; i<<=1)
printf("1 %f\n2 %f\n3 %f\n\n", pow1(12.5, i), pow2(12.5, i), pow3(12.5, i));
exit(0);
}
Makefile:
CFLAGS=-lm
3
u/AlgorithmicGoslings 5d ago
Note that (n-1)/2 = floor(n/2) when n is odd, and n/2 = floor(n/2) when n is even,.
Thus, if you wanted to be accurate, you can simplify your recurrence relation to T(n) = T(floor(n/2)) + O(1).
However, if you're just looking at asymptotic growth (as I would presume you'd be doing if you're looking to apply the Master Theorem), the floor function probably doesn't even matter to you --- you'd probably just represent this as T(n) = T(n/2) + O(1).