• + 0 comments

    My solution Js, using memoization and closures.

    function fibonacciModified(t1, t2, n) {
        // Write your code here
        return memoized(t1, t2)(n);
    }
    
    const memoized = (t1, t2) => {
        const obj = { 1: BigInt(t1),
                      2: BigInt(t2) };
        return function fib(n) {
            if (obj[n]) {
                return obj[n]; 
            } else {
                if (n === 1) {
                    return BigInt(t1);
                } else if (n === 2) {
                    return BigInt(t2);
                } else {
                    obj[n] = BigInt(fib(n - 2)) + BigInt(fib(n - 1) ** BigInt(2));
                    return obj[n];
                }
            }
        } 
    # };