Fibonacci Backend Test
Showing development of backend for the fibonacci portion of the mini-project.
import java.time.Duration;
import java.time.Instant;
public class FibonacciWithGoldenRatio {
public static void main(String[] args) {
Instant timerStart = Instant.now();
int n = 20;
double goldenRatio = (1 + Math.sqrt(5)) / 2;
long fibonacciNumber = calculateFibonacciWithGoldenRatio(n, goldenRatio);
System.out.println("Fibonacci number at position " + n + ": " + fibonacciNumber);
Instant timerStop = Instant.now();
Duration elapsedTime = Duration.between(timerStart, timerStop);
System.out.println("elapsed time: " + elapsedTime.toNanosPart() + " nanoseconds");
}
private static long calculateFibonacciWithGoldenRatio(int n, double goldenRatio) {
double phiToN = Math.pow(goldenRatio, n);
double negativePhiToMinusN = Math.pow(-goldenRatio, -n);
double result = (phiToN - negativePhiToMinusN) / Math.sqrt(5);
return Math.round(result);
}
}
import java.math.BigInteger;
// JAVA program to find value of f(n) where
// f(n) is defined as
// F(n) = F(n-1) + F(n-2) + F(n-3), n >= 3
// Base Cases :
// F(0) = 0, F(1) = 1, F(2) = 1
class MatrixFibo {
static void multiply(int a[][], int b[][])
{
// Creating an auxiliary matrix to store elements of multiplication matrix
int mul[][] = new int[3][3];
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
mul[i][j] = 0;
for (int k = 0; k < 3; k++)
mul[i][j] += a[i][k]
* b[k][j];
}
}
// storing the multiplication result in a[][]
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
// Updating our matrix
a[i][j] = mul[i][j];
}
// Function to compute F raise to power n-2
static int power(int F[][], int n)
{
// Multiply it with initial values
// i.e with F(0) = 0, F(1) = 1
if (n == 1)
return F[0][0] + F[0][1];
power(F, n / 2);
multiply(F, F);
if (n%2 != 0)
multiply(F, M);
// Multiply it with initial values
return F[0][0] + F[0][1] ;
}
// Return n'th term of a series defined
// f(n) is defined as f(n) = f(n-1) + f(n-2) + f(n-3), n>=3
// Base Cases :
// f(0) = 0, f(1) = 1, f(2) = 1
static int findNthTerm(int n)
{
{0, 1, 0}} ;
return power(F, n-2);
}
// Driver code
public static void main (String[] args) {
Instant timerStart2 = Instant.now();
int n = 5;
System.out.println("Fibonacci number at position 5: "
+ findNthTerm(n));
Instant timerStop2 = Instant.now();
Duration elapsedTime2 = Duration.between(timerStart2, timerStop2);
System.out.println("elapsed time: " + elapsedTime2.toNanosPart() + " nanoseconds");
}
System.out.println("GOLDEN RATIO:");
FibonacciWithGoldenRatio.main(null);
System.out.println("MATRIX EXPONENTIATION:");
MatrixFibo.main(null);
| {0, 1, 0}} ;
not a statement
| {0, 1, 0}} ;
';' expected
| return power(F, n-2);
illegal start of type
| return power(F, n-2);
invalid method declaration; return type required
| return power(F, n-2);
<identifier> expected
| return power(F, n-2);
<identifier> expected
import java.time.Duration;
import java.time.Instant;
class FibbonacciForLoop {
public static int FibonacciLoop(int n){
int endResult = 0;
int prevBy2 = 0;
int prevBy1 = 1;
if(n == 0 || n == 1){
endResult = 1;
}
else{
for(int i = 1; i <= n-1; i++){
endResult = prevBy1 + prevBy2;
prevBy2 = prevBy1;
prevBy1 = endResult;
}
}
return endResult;
}
public static void main(){
Instant timerStart = Instant.now();
System.out.println(FibonacciLoop(45));
Instant timerStop = Instant.now();
Duration elapsedTime = Duration.between(timerStart, timerStop);
System.out.println("elapsed time: " + elapsedTime.toNanosPart() + " nanoseconds");
}
}
FibbonacciForLoop.main();
1134903170
elapsed time: 1294000 nanoseconds
import java.time.Duration;
import java.time.Instant;
class FibonacciWhileLoop {
public static int FibonacciWhile(int n){
int endResult = 0;
int prevBy2 = 0;
int prevBy1 = 1;
if(n == 0 || n == 1){
endResult = 1;
}
else{
int i = 1;
do{
endResult = prevBy1 + prevBy2;
prevBy2 = prevBy1;
prevBy1 = endResult;
i = i + 1;
}
while(i <= n-1);
}
return endResult;
}
public static void main(){
Instant timerStart = Instant.now();
System.out.println(FibonacciWhile(45));
Instant timerStop = Instant.now();
Duration elapsedTime = Duration.between(timerStart, timerStop);
System.out.println("elapsed time: " + elapsedTime.toNanosPart() + " nanoseconds");
}
}
FibonacciWhileLoop.main();
1134903170
elapsed time: 1170000 nanoseconds
class FibonacciSwitch{
public static int fiboSwitch(int n){
int endResult = 0;
int prevBy2 = 0;
int prevBy1 = 1;
switch(n){
case 0:
endResult = 1;
break;
case 1:
endResult = 1;
break;
default:
int i = 1;
do{
endResult = prevBy1 + prevBy2;
prevBy2 = prevBy1;
prevBy1 = endResult;
i = i + 1;
}
while(i <= n-1);
break;
}
return endResult;
}
public static void main(){
Instant timerStart = Instant.now();
System.out.println(fiboSwitch(46));
Instant timerStop = Instant.now();
Duration elapsedTime = Duration.between(timerStart, timerStop);
System.out.println("elapsed time: " + elapsedTime.toNanosPart() + " nanoseconds");
}
}
FibonacciSwitch.main();
1836311903
elapsed time: 1070000 nanoseconds