Skip to content

Commit 7412d50

Browse files
authored
Merge pull request #561 from trinity97/master
Added O(logn) algorithm to find fibonacci term
2 parents 1738bd8 + c7c56fe commit 7412d50

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

NthFibonacci/NthFibonacci.java

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStream;
4+
import java.io.InputStreamReader;
5+
import java.util.StringTokenizer;
6+
7+
public class NthFibonacci {
8+
public static void main(String[] args) throws IOException {
9+
Reader.init(System.in);
10+
System.out.println("Enter the number of term to be printed:");
11+
long n = Reader.nextLong();
12+
System.out.println(smartfib(n)[1][0]);
13+
}
14+
15+
public static long[][] smartfib(long n){
16+
long a[][]=new long[2][2];
17+
a[0]= new long[]{1, 1};
18+
a[1]= new long[]{1,0};
19+
if (n==0){
20+
a[1][0]=0;
21+
return a;
22+
}
23+
if (n==1){
24+
return a;
25+
}
26+
if (n%2==1){
27+
return product(square(smartfib(((n-1)/2))),a);
28+
}
29+
return square(smartfib((n/2)));
30+
}
31+
public static long[][] square(long a[][]){
32+
long b[][]=new long[2][2];
33+
for (int i=0;i<2;i++){
34+
for (int j=0;j<2;j++){
35+
long sum=0;
36+
for(int k=0;k<2;k++){
37+
sum+=(a[i][k]*a[k][j]);
38+
}
39+
b[i][j]=sum;
40+
}
41+
}
42+
return b;
43+
}
44+
public static long[][] product(long a[][],long b[][]){
45+
long c[][]=new long[2][2];
46+
for (int i=0;i<2;i++){
47+
for (int j=0;j<2;j++){
48+
long sum=0;
49+
for(int k=0;k<2;k++){
50+
sum+=(a[i][k]*b[k][j]);
51+
}
52+
c[i][j]=sum;
53+
}
54+
}
55+
return c;
56+
}
57+
}
58+
59+
class Reader {
60+
static BufferedReader reader;
61+
static StringTokenizer tokenizer;
62+
63+
static void init(InputStream input) {
64+
reader = new BufferedReader(
65+
new InputStreamReader(input) );
66+
tokenizer = new StringTokenizer("");
67+
}
68+
69+
static String next() throws IOException {
70+
while ( ! tokenizer.hasMoreTokens() ) {
71+
tokenizer = new StringTokenizer(
72+
reader.readLine() );
73+
}
74+
return tokenizer.nextToken();
75+
}
76+
77+
static int nextInt() throws IOException {
78+
return Integer.parseInt( next() );
79+
}
80+
81+
static double nextDouble() throws IOException {
82+
return Double.parseDouble( next() );
83+
}
84+
static long nextLong() throws IOException {
85+
return Long.parseLong( next() );
86+
}
87+
}

NthFibonacci/README.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
## The Nth Fibonacci Term
2+
### Algorithm
3+
4+
The Fibonacci Sequence is the series of numbers:
5+
6+
`1, 1, 2, 3, 5, 8, 13, 21, 34, ...`
7+
8+
The next number is found by adding up the two numbers before it.
9+
10+
* The 2 is found by adding the two numbers before it (1+1)
11+
* The 3 is found by adding the two numbers before it (1+2),
12+
* And the 5 is (2+3),
13+
* and so on!
14+
15+
### Output of the program
16+
* `Enter the number of term to be printed: 10`
17+
* `Output: 55`

0 commit comments

Comments
 (0)