Fibonacci Sequence - find the number of digits - JavaScript

So, I successfully wrote a Fibonacci sequence to create arraywith a sequence of numbers, but I need to know the length ( how many digits ) the number 500thhas.

I tried the code below, but its search for the length of the scientific notation (22 digits), and not the correct 105, it should return.

Any ideas on how to convert the scientific notation number to an actual integer?

var fiblength = function fiblength(nth) {
    var temparr = [0,1];
    for(var i = 2; i<=nth; i++){
        var prev = temparr[temparr.length-2],
            cur = temparr[temparr.length-1],
            next = prev + cur;
            temparr.push(next);
    }
    var final = temparr[temparr.length-1].toString().length;
    console.log(temparr[temparr.length-1]);
    return final;
};
a = fiblength(500);
console.log(a);
+4
source share
5 answers

Why not use the simple procedure of dividing the number by 10 until the number becomes less.

- , ( def obv )

function getDigits(n) {
   var digits = 0;
   while(n >= 1) {
      n/=10;
      digits += 1;
   }
   return digits;
}

getDigits(200);//3
getDigits(3.2 * 10e20);//=>22
+7

:

function fiblength(n) { 
   return Math.floor((n>1)?n*.2089+.65051:1); 
}

, .

, , N>300, BigNumber. , .

, PHI ( ), :

F(n) = ABS((PHI^n)/sqrt(5))

PHI=1.61803399 ( , )

, , 10 1. D(n) = log10(n) + 1

fiblength

fiblength(n) = D(F(n))// ...

, , liner, .

F(n)

fiblength(n) = D(ABS((PHI^n)/sqrt(5)))

D (n) :

fiblength(n) = log10(ABS((PHI^n)/sqrt(5))) + 1

, log(a/b) = log(a) - log(b)

fiblength(n) = log10(ABS((PHI^n))) - log10(sqrt(5))) + 1

log(a^n) = n * log(a)

fiblength(n) = n*log10(PHI) - log10(sqrt(5))) + 1

, n=0 n=1, 1

function fiblength(n) { 
   return Math.floor((n>1)?n*.2089+.65051:1); 
}

:)

fiblength(500) => 105// .

+2

javascript, 64- . , , , , . , "javascript numbers". , BigNum.

, 500- , , .

function fiblength(nth) {
    var previous = 0, current = 1, temp;
    for(var i = 2; i<=nth; i++){
        temp = current;
        current = previous + current;
        previous = temp;
    }
    return current;
};
+1
source

My final decision

function fiblength(nth) {
    var a = 0, b = 1, c;
    for(var i=2;i<=nth;i++){
        c=b;
        b=a+b;
        a=c;
    }
    return Math.floor(Math.log(b)/Math.log(10))+1;
}
console.log(fiblength(500));

Thanks for the help!!!

0
source

The problem is that the resulting number was converted to a string before any meaningful calculations could be done. Here's how it could be solved in the source code:

var fiblength = function fiblength(nth) {
    var temparr = [0,1];
    for(var i = 2; i<=nth; i++){
        var prev = temparr[temparr.length-2],
            cur = temparr[temparr.length-1],
            next = prev + cur;
            temparr.push(next);
    }
    var x = temparr[temparr.length-1];
    console.log(x);
    var length = 1;
    while (x > 1) {
        length = length + 1;
        x = x/10;
    }
    return length;
};

console.log ( fiblength(500) );
0
source

All Articles