-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfase_1.js
65 lines (48 loc) · 1.54 KB
/
fase_1.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
// Fibonacci
let fibonacci = ['1', '1'];
let index = 2;
let next = '0';
// How much characters we want
let chars = 1000;
// Since Javascript is unable to process a number higher than 2^53 -1 with bigint
// we will use strings and create function to proceed with the
// The greater number must be always to the right (b)
let sum = (a, b) => {
a = a.toString().split('');
b = b.toString().split('');
let a_size = a.length;
let b_size = b.length;
let plus_one = 0;
let result = b;
for (let i = 1; i <= a.length; i++) {
let local_sum = parseInt(a[a_size - i]) + parseInt(b[b_size - i]);
// columnar sum
if (local_sum < 9) {
result[b_size - i] = local_sum + plus_one;
plus_one = 0;
}
else if (local_sum == 9) {
result[b_size - i] = plus_one ? '0' : '9';
plus_one = plus_one ? 1 : 0;
}
else {
local_sum = (local_sum + plus_one).toString();
result[b_size - i] = local_sum[1];
plus_one = 1;
}
}
if (b_size > a_size)
result[0] = parseInt(b[0]) + plus_one;
else if (plus_one)
result.unshift(1);
return result.join('');
}
// We are doing the fibonacci calculation based
// on the number of chars we want
while (next.length < chars) {
next = sum(fibonacci[index - 2], fibonacci[index - 1]);
fibonacci[index] = next;
index++;
}
console.log('position: ' + index);
console.log('fibonacci with ' + chars + ' chars: ' + next);