This solution is faster for large N, slower for small N. jsben.ch/mOl4l
/** * @param {number} N * @return {number} */ const pow = function (base, n) { if (n==0) return 1 if (n==1) return base if (n&1) return base*pow(base, n-1) else { const half = pow(base, Math.floor(n/2)) return half*half } } var fib = function(N) { const sqrt5 = 2.23606797749979 const num = 1.618033988749895 const num2 = -0.6180339887498948 return Math.round( ( pow(num, N) - pow(num2, N) ) / sqrt5 ) }; fib(NUM);
Using precomputed values for constants and using matrix expo for calculating ab (which is log(n))
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
This solution is faster for large N, slower for small N.
jsben.ch/mOl4l
Using precomputed values for constants and using matrix expo for calculating ab (which is log(n))