Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 21934a6

Browse files
fibonacci-modified using big init
1 parent 9815ea2 commit 21934a6

File tree

1 file changed

+5
-41
lines changed

1 file changed

+5
-41
lines changed

‎fibonacci-modified.js

Lines changed: 5 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -24,52 +24,16 @@ function readLine() {
2424
return inputString[currentLine++];
2525
}
2626

27-
function add(a, b) {
28-
let carry = 0;
29-
let i = 0;
30-
for (i = 0; i < b.length; i++) {
31-
let n = a[i] + b[i] + carry;
32-
a[i] = n % 10;
33-
carry = Math.floor(n / 10);
34-
}
35-
while (carry > 0) {
36-
a[i] = typeof a[i] !== 'undefined' ? a[i] : 0
37-
let n = a[i] + carry;
38-
a[i] = n % 10;
39-
carry = Math.floor(n / 10);
40-
i++;
41-
}
42-
return a;
43-
}
44-
45-
const mul = (b, a) => {
46-
let out = [];
47-
let k = 0, carry = 0;
48-
for (let i = 0; i < a.length; i++) {
49-
for (let j = 0; j < b.length; j++) {
50-
let e = typeof out[k] !== 'undefined' ? out[k] : 0;
51-
let n = (a[i] * b[j]) + carry + e;
52-
out[k] = n % 10;
53-
carry = Math.floor(n / 10);
54-
k++;
55-
}
56-
if (carry > 0) {
57-
out[k] = carry;
58-
carry = 0;
59-
}
60-
k = i + 1;
61-
}
62-
return out;
63-
}
6427
function fibonacciModified(t1, t2, n) {
6528
let hash = {};
66-
hash[1] = [t1];
67-
hash[2] = [t2];
29+
hash[1] = BigInt(t1);
30+
hash[2] = BigInt(t2);
6831
for (let i = 3; i < n + 1; i++) {
69-
hash[i] = add(mul(hash[i - 1].map(x=>x),hash[i - 1].map(x=>x)),hash[i - 2].map(x=>x))
32+
hash[i] = (hash[i - 1]*hash[i - 1])+hash[i - 2];
7033
}
71-
return hash[n].reverse().join('');
34+
return hash[n]
7235
}
36+
7337
function main() {
7438
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
7539

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /