I have a coding recursion question in c: Implement a recursive function: m(n) =m
ID: 3809293 • Letter: I
Question
I have a coding recursion question in c:
Implement a recursive function: m(n) =m(n-1)*m(n-1) + c, m(0) = c, where c={-1, 1}, which is a complex number, and the addition and multiplication are complex number operations. Your program will print out m(10).
Essentially this program should use the formula m(n) = m(n-1)*m(n-1) + c and produce and output. The output will be a larger number (a complex number). The input is n which should be a int. The multiplication and the addition have to be adjusted for complex number operations.
I I can understand that I dont understand how to use a recursion function to put it all together.
Explanation / Answer
#include <stdio.h>
struct complex
{
int a;
int b;
};
struct complex c;
struct complex mult(struct complex cmplx)
{
struct complex ret;
ret.a = cmplx.a*cmplx.a - cmplx.b*cmplx.b;
ret.b = 2*cmplx.a*cmplx.b;
return ret;
}
struct complex add(struct complex num1, struct complex num2)
{
struct complex result;
result.a = num1.a + num2.a;
result.b = num1.b + num2.b;
return result;
}
struct complex m(int n)
{
if (n == 0)
{
return c;
}
else
{
return add(mult(m(n-1)), c);
}
}
int main()
{
int n = 10;
c.a = -1;
c.b = 1;
struct complex c = m(n);
printf("m(%d) = %d", n, c.a);
if (c.b >= 0)
{
printf(" + ");
}
printf("%d*i ", c.b);
return 0;
}
Sample run
m(10) = 1482976887-1545101541*i
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.