-
Notifications
You must be signed in to change notification settings - Fork 0
/
Problem1.1.java
54 lines (47 loc) · 1.28 KB
/
Problem1.1.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
/*******************************************************
Program Number: 1
Purpose/Description: Sub-linear function returning nth Fibonaci number
Author: Nicolas Dabdoub
Due date: <01/30/15>
Certification:
I hereby certify that this work is my own and none of it is the
work of any other person.
< Nicolas Daboub>
*******************************************************/
public class Problem1
{
void multiply(int[][] F, int [][]M)
{
int x = F[0][0]*M[0][0] + F[0][1]*M[1][0];
int y = F[0][0]*M[0][1] + F[0][1]*M[1][1];
int z = F[1][0]*M[0][0] + F[1][1]*M[1][0];
int w = F[1][0]*M[0][1] + F[1][1]*M[1][1];
F[0][0] = x;
F[0][1] = y;
F[1][0] = z;
F[1][1] = w;
}
void pow( int F[][], int n)
{
if (n == 0 || n == 1)
return;
int M[][] = {{1,1},{1,0}};
pow(F,n/2);
multiply(F,F);
if (n%2 != 0)
multiply (F,M);
}
int fib (int n)
{
int F [][] = {{1,1},{1,0}};
if (n == 0)
return 0;
pow(F, n-1);
return F[0][0];
}
public static void main(String[] args)
{
Problem1 n = new Problem1 ();
System.out.println(n.fib(18));
}
}