Skip to content

Fibonacci Modified #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
prabaprakash opened this issue Jul 30, 2020 · 2 comments
Closed

Fibonacci Modified #4

prabaprakash opened this issue Jul 30, 2020 · 2 comments

Comments

@prabaprakash
Copy link
Owner

big add
const add = (a, b) => {
    let carry = 0;
    let i = 0;
    for (i = 0; i < b.length; i++) {
        let n = a[i] + b[i] + carry;
        a[i] = n % 10;
        carry = Math.floor(n / 10);
    }
    while (carry > 0) {
        a[i] = typeof a[i] !== 'undefined' ? a[i] : 0
        let n = a[i] + carry;
        a[i] = n % 10;
        carry = Math.floor(n / 10);
        i++;
    }
    return a;
}
// big mul
const mul = (b, a) => {
    let out = [];
    let k = 0, carry = 0;
    for (let i = 0; i < a.length; i++) {
        for (let j = 0; j < b.length; j++) {
            let e = typeof out[k] !== 'undefined' ? out[k] : 0;
            let n = (a[i] * b[j]) + carry + e;
            out[k] = n % 10;
            carry = Math.floor(n / 10);
            k++;
        }
        if (carry > 0) {
            out[k] = carry;
            carry = 0;
        }
        k = i + 1;
    }
    return out;
}
@prabaprakash
Copy link
Owner Author

the above multiply functions operation needs optimization

@prabaprakash
Copy link
Owner Author

solved by predefined bigint in js

function fibonacciModified(t1, t2, n) {
    let hash = {};
    hash[1] = BigInt(t1);
    hash[2] = BigInt(t2);
    for (let i = 3; i < n + 1; i++) {
        hash[i] = (hash[i - 1] * hash[i - 1]) + hash[i - 2];
    }
    return hash[n]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant