我正试图在O(log_2(n))操作中升至n次幂(2x2)矩阵。 矩阵定义如下:
A = [y1 = 1 y2 = 2] [y3 = 1 y4 = 0]
我认为我的方法有一些错误。关于如何执行此操作的任何建议?
#include <stdio.h>
int fnct(int n)
{
int y1 = 1, y2 = 1, y3 = 1, y4 = 0; //entries of the A matrix
int z1, z2, z3, z4; //entries of the auxiliary matrix
// if n==0 return matrix it-self
if (n == 0)
{
y1 = 1;
y2 = 0;
y3 = 1;
y4 = 1;
return y1, y2, y3, y4;
}
// if n mod 2 == 0 set y1,y2,y3,y4 to the fnct(n/2)
if ((n % 2) == 0)
{
y1, y2, y3, y4 = fnct(n / 2);
z1 = y1 * y2 + y2 * y3;
z2 = y1 * y2 + y2 * y4;
z3 = y3 * y1 + y4 * y3;
z4 = y3 * y2 + y4 * y4;
return z1, z2, z3, z4;
}
else if ((n % 2) == 1)
{
y1, y2, y3, y4 = fnct(n / 2);
z1 = y1 * y1 + y2 * y3;
z2 = y1 * y2 + y2 * y1;
z3 = y3 * y1 + y4 * y3;
z4 = y3 * y2 + y4 * y4;
y1 = z1 + z3;
y2 = z2 + z4;
y3 = z1;
y4 = z2;
return z1, z2, z3, z4;
}
//return of the end of the function
return y1, y2, y3, y4;
}
int main()
{
int n;
printf("Enter n\n");
scanf("%d", &n);
printf("%d\n", fnct(n));
}