Answer the question
In order to leave comments, you need to log in
Fibonacci without using BigInt in javascript?
Hello, I'm learning Js and faced such a problem. You need to write a function that takes an integer natural number from 500 to 25000 and finds the Fibonacci number (hereinafter fib) by serial number and everything would be fine, but it does not accept Node 8.1.3 and BigInt numbers. It is also necessary that the function, having received the number of fibs, breaks it into intervals of 25 digits and displays the last one (it can be less than 25) and also counts the number of each digit from 0 to 9 in the number and displays more of it. the code came out like this:
function aroundFib(n) {
let a = 1n;
let b = 1n;
for (let i = 3; i <= n; i++) {
let c = a + b;
a = b;
b = c;
}
let str = ''
let obj = {
0: 0,
1: 0,
2: 0,
3: 0,
4: 0,
5: 0,
6: 0,
7: 0,
8: 0,
9: 0,
}
for(let number of String(b)){
str += number;
obj[number]++;
if(str.length > 25){
str = number
}
}
let max = 0
let count = 0;
for(let [key, value] of Object.entries(obj)){
if(value > max){
max = value;
count = key;
}
}
return `Last chunk ${str}; Max is ${max} for digit ${count}`
}
Answer the question
In order to leave comments, you need to log in
Without BigInt - in any way. The numbers are getting big. If there is no library, you will have to write long arithmetic yourself. Just store the digits of the number in an array and the length next to it. You will only need to implement addition, well, this is done in a column in one cycle. Store the low-order carry in a variable. You can either search for the BigInt sources, or look here
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question