Fibonacci program using DP

package org.wesome.dsalgo;

public class Fibonacci {
    static int fibonacci(int n) {
        /* Declare an array to store Fibonacci numbers. */
        /* Although Fibonacci number starts from 1 to n but we need n+1 to store n = 0 case */
        int[] fibonacciArr = new int[n + 1];
        int i;
        /* 0th and 1st Fibonacci number are already known */
        fibonacciArr[0] = 0;
        fibonacciArr[1] = 1;
        for (i = 2; i <= n; i++) {
        /* with each iteration, add the previous value to it and store */
            fibonacciArr[i] = fibonacciArr[i - 1] + fibonacciArr[i - 2];
        }
        return fibonacciArr[n];
    }
}
package org.wesome.dsalgo;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import static org.wesome.dsalgo.Fibonacci.fibonacci;

public class StringReverseTest {
    @Test
    void fibonacciTest1() {
        int n = 9;
        int fibonacci = fibonacci(n);
        Assertions.assertEquals(34, fibonacci);
    }

    @Test
    void fibonacciTest2() {
        int n = 14;
        int fibonacci = fibonacci(n);
        Assertions.assertEquals(377, fibonacci);
    }
}
plugins {
    id 'java'
}

group = 'org.wesome'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = JavaVersion.VERSION_1_8

repositories {
    mavenCentral()
}
dependencies {
    testImplementation('org.junit.jupiter:junit-jupiter:5.6.2')
}

test {
    useJUnitPlatform()
}

 

follow us on