fibonacci series i logn java
Java
class fibonacci
{
static int fib(int n)
{
int F[][] = new int[][]{{1,1},{1,0}};
if (n == 0)
return 0;
power(F, n-1);
return F[0][0];
}
/* Helper function that multiplies 2 matrices F and M of size 2*2, and
puts the multiplication result back to F[][] */
static 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;
}
/* Helper function that calculates F[][] raise to the power n and puts the
result in F[][]
Note that this function is designed only for fib() and won't work as general
power function */
static void power(int F[][], int n)
{
int i;
int M[][] = new int[][]{{1,1},{1,0}};
// n - 1 times multiply the matrix to {{1,0},{0,1}}
for (i = 2; i <= n; i++)
multiply(F, M);
}
/* Driver program to test above function */
public static void main (String args[])
{
int n = 9;
System.out.println(fib(n));
}
}
/* This code is contributed by Rajat Mishra */
Also in Java:
- Title
- arraylist to array java
- Category
- Java
- Title
- tostring() java example
- Category
- Java
- Title
- convert string into unicode java
- Category
- Java
- Title
- java iterator example
- Category
- Java
- Title
- java bubble sort short circuit
- Category
- Java
- Title
- java get current date without time
- Category
- Java
- Title
- java split for multiple characters
- Category
- Java
- Title
- android foreground push notification
- Category
- Java
- Title
- java create map
- Category
- Java
- Title
- what is java
- Category
- Java
- Title
- alfred workflow
- Category
- Java
- Title
- sum of arraylist java 8
- Category
- Java
- Title
- how to add element to arraylist
- Category
- Java
- Title
- hello world java
- Category
- Java
- Title
- java windowbuilder full screen
- Category
- Java
- Title
- how to check wether the property exist in a object in java script
- Category
- Java
- Title
- set html text android java
- Category
- Java
- Title
- java create clas
- Category
- Java
- Title
- get method of a class which I only have string to
- Category
- Java
- Title
- java setroundingmode
- Category
- Java
- Title
- bukkit java connect player to another server in bungeecord
- Category
- Java
- Title
- hide elements android
- Category
- Java
- Title
- how to initialize an array in java
- Category
- Java
- Title
- context error in android studio
- Category
- Java
- Title
- foreach in java
- Category
- Java
- Title
- java 8 stream group by example
- Category
- Java
- Title
- Android recycleView
- Category
- Java
- Title
- kotlin jsonobject get nested
- Category
- Java
- Title
- how to create a linked list in java
- Category
- Java
- Title
- import java.io.serializable
- Category
- Java
- Title
- tolowercase java
- Category
- Java
- Title
- long input in JAVA
- Category
- Java
- Title
- java comments
- Category
- Java
- Title
- math max java
- Category
- Java
- Title
- java file path linux
- Category
- Java
- Title
- Filebody in java
- Category
- Java
- Title
- java copy array
- Category
- Java
- Title
- java catch multiple exceptions
- Category
- Java
- Title
- reverse shuffle merge
- Category
- Java
- Title
- log4j print stacktrace
- Category
- Java
- Title
- find first non repeating character in a string
- Category
- Java
- Title
- java exceptions for catch
- Category
- Java
- Title
- how to find powers in java
- Category
- Java
- Title
- how to access private vairable in java method
- Category
- Java
- Title
- array in java stack overflow
- Category
- Java
- Title
- how to get binary value in java
- Category
- Java
- Title
- java timestamp
- Category
- Java
- Title
- java comparable
- Category
- Java
- Title
- jbutton open jframe java
- Category
- Java
- Title
- java if
- Category
- Java
- Title
- what is a float java
- Category
- Java
- Title
- intellij replace all
- Category
- Java
- Title
- how to interrupt a void java
- Category
- Java
- Title
- LocalRegistry java rebind() java8
- Category
- Java
- Title
- math min max java
- Category
- Java
- Title
- java date time
- Category
- Java
- Title
- java how to read file extension
- Category
- Java
- Title
- Gson write json to file
- Category
- Java
- Title
- Could not resolve project :app
- Category
- Java
- Title
- jbutton close jframe java
- Category
- Java
- Title
- double parse jtextfield
- Category
- Java
- Title
- inputstream to string java
- Category
- Java
- Title
- android studio java random number generator
- Category
- Java
- Title
- creating the functional interface in java
- Category
- Java
- Title
- spigot cancel repeating task
- Category
- Java
- Title
- convert string to array java
- Category
- Java
- Title
- how to add an image to a gui in java windowbuilder
- Category
- Java
- Title
- 2d array length in java
- Category
- Java
- Title
- java stream collect to string
- Category
- Java
- Title
- how to create a java jframe
- Category
- Java
- Title
- java min function
- Category
- Java
- Title
- hashmap in java
- Category
- Java
- Title
- time conversion solution
- Category
- Java
- Title
- java how to call getReader twice
- Category
- Java
- Title
- initialize scanner java
- Category
- Java
- Title
- even or odd in java
- Category
- Java
- Title
- fragment button nullpointerexception
- Category
- Java
- Title
- how to get the max value of an array java
- Category
- Java
- Title
- Java use Base64
- Category
- Java
- Title
- abstract class java constructor
- Category
- Java
- Title
- absolute value in java
- Category
- Java
- Title
- taking string input in java
- Category
- Java
- Title
- Java loop throug gson JsonElement
- Category
- Java
- Title
- how to initialize main in java
- Category
- Java
- Title
- blurview android github
- Category
- Java
- Title
- java add a list to a list
- Category
- Java
- Title
- gat environment variables java
- Category
- Java
- Title
- java boolean even number
- Category
- Java
- Title
- room insert and return id
- Category
- Java
- Title
- java script zip function
- Category
- Java
- Title
- how to read a csv file java
- Category
- Java
- Title
- java fullscreen jframe
- Category
- Java
- Title
- java stream sorted reverse
- Category
- Java
- Title
- how to change double to int in java
- Category
- Java
- Title
- fibonacci sequence in java recursion
- Category
- Java
- Title
- how to check if a list is empty java
- Category
- Java
- Title
- full shutdown windows 10 cmd
- Category
- Java
- Title
- java read string input
- Category
- Java
- Title
- filter and map multiple fields from java stream
- Category
- Java
- Title
- parallel sorting in java 8
- Category
- Java