Yess ... For
For example :-
F(n) = | 1 1 |^n
| 1 0 |
For calculating a^n ... use the Exponential method Here a will be
your Matrix ..
int result (int a,int n)
{
int x=1,y=a;
while(n>0)
{
if(n%2==1) x=(x*y); /* Instead of X=1 i(n case of
ODD ) substiute with the UNIT MATIX and A will be the Matrix mentioned
above .*/
if (n/=2) y=(y*y);
}
return x; //This will be from the matrix A(1,0)
}
Concept is like this :--
a^n = Can be written as (a^2)^(n/2) For n is even . [ Reason:- For
n/2 in my code or n=n>>1; in Rishabh code ]
= Can be written as a.(a^2)^(n-1/2) For n is odd [ Reason :-
x=(x*y) for the odd number for getting the alone ]
--
You received this message because you are subscribed to the Google Groups
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/algogeeks?hl=en.