Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 98487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T00:12:19+00:00 2026-05-11T00:12:19+00:00

I’ve become interested in algorithms lately, and the fibonacci sequence grabbed my attention due

  • 0

I’ve become interested in algorithms lately, and the fibonacci sequence grabbed my attention due to its simplicity.

I’ve managed to put something together in javascript that calculates the nth term in the fibonacci sequence in less than 15 milliseconds after reading lots of information on the web. It goes up to 1476…1477 is infinity and 1478 is NaN (according to javascript!)

I’m quite proud of the code itself, except it’s an utter monster.

So here’s my question: A) is there a faster way to calculate the sequence? B) is there a faster/smaller way to multiply two matrices?

Here’s the code:

//Fibonacci sequence generator in JS //Cobbled together by Salty m = [[1,0],[0,1]]; odd = [[1,1],[1,0]]; function matrix(a,b) {     /*          Matrix multiplication         Strassen Algorithm         Only works with 2x2 matrices.     */     c=[[0,0],[0,0]];     c[0][0]=(a[0][0]*b[0][0])+(a[0][1]*b[1][0]);     c[0][1]=(a[0][0]*b[0][1])+(a[0][1]*b[1][1]);     c[1][0]=(a[1][0]*b[0][0])+(a[1][1]*b[1][0]);     c[1][1]=(a[1][0]*b[0][1])+(a[1][1]*b[1][1]);     m1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);     m2=(a[1][0]+a[1][1])*b[0][0];     m3=a[0][0]*(b[0][1]-b[1][1]);     m4=a[1][1]*(b[1][0]-b[0][0]);     m5=(a[0][0]+a[0][1])*b[1][1];     m6=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);     m7=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);     c[0][0]=m1+m4-m5+m7;     c[0][1]=m3+m5;     c[1][0]=m2+m4;     c[1][1]=m1-m2+m3+m6;     return c; } function fib(n) {     mat(n-1);     return m[0][0]; } function mat(n) {     if(n > 1) {         mat(n/2);         m = matrix(m,m);     }     m = (n%2<1) ? m : matrix(m,odd); } alert(fib(1476)); //Alerts 1.3069892237633993e+308 

The matrix function takes two arguments: a and b, and returns a*b where a and b are 2×2 arrays. Oh, and on a side note, a magical thing happened…I was converting the Strassen algorithm into JS array notation and it worked on my first try! Fantastic, right? 😛

Thanks in advance if you manage to find an easier way to do this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T00:12:20+00:00Added an answer on May 11, 2026 at 12:12 am

    Don’t speculate, benchmark:

    edit: I added my own matrix implementation using the optimized multiplication functions mentioned in my other answer. This resulted in a major speedup, but even the vanilla O(n^3) implementation of matrix multiplication with loops was faster than the Strassen algorithm.

    <pre><script>  var fib = {};  (function() {     var sqrt_5  = Math.sqrt(5),         phi     = (1 + sqrt_5) / 2;      fib.round = function(n) {         return Math.floor(Math.pow(phi, n) / sqrt_5 + 0.5);     }; })();  (function() {     fib.loop = function(n) {         var i = 0,             j = 1;          while(n--) {             var tmp = i;             i = j;             j += tmp;         }          return i;     }; })();  (function () {     var cache = [0, 1];      fib.loop_cached = function(n) {         if(n >= cache.length) {             for(var i = cache.length; i <= n; ++i)                 cache[i] = cache[i - 1] + cache[i - 2];         }          return cache[n];     }; })();  (function() {     //Fibonacci sequence generator in JS     //Cobbled together by Salty     var m;     var odd = [[1,1],[1,0]];      function matrix(a,b) {         /*             Matrix multiplication             Strassen Algorithm             Only works with 2x2 matrices.         */         var c=[[0,0],[0,0]];         var m1=(a[0][0]+a[1][1])*(b[0][0]+b[1][1]);         var m2=(a[1][0]+a[1][1])*b[0][0];         var m3=a[0][0]*(b[0][1]-b[1][1]);         var m4=a[1][1]*(b[1][0]-b[0][0]);         var m5=(a[0][0]+a[0][1])*b[1][1];         var m6=(a[1][0]-a[0][0])*(b[0][0]+b[0][1]);         var m7=(a[0][1]-a[1][1])*(b[1][0]+b[1][1]);         c[0][0]=m1+m4-m5+m7;         c[0][1]=m3+m5;         c[1][0]=m2+m4;         c[1][1]=m1-m2+m3+m6;         return c;     }      function mat(n) {         if(n > 1) {             mat(n/2);             m = matrix(m,m);         }         m = (n%2<1) ? m : matrix(m,odd);     }      fib.matrix = function(n) {         m = [[1,0],[0,1]];         mat(n-1);         return m[0][0];     }; })();  (function() {     var a;      function square() {         var a00 = a[0][0],             a01 = a[0][1],             a10 = a[1][0],             a11 = a[1][1];          var a10_x_a01 = a10 * a01,             a00_p_a11 = a00 + a11;          a[0][0] = a10_x_a01 + a00 * a00;         a[0][1] = a00_p_a11 * a01;         a[1][0] = a00_p_a11 * a10;         a[1][1] = a10_x_a01 + a11 * a11;     }      function powPlusPlus() {         var a01 = a[0][1],             a11 = a[1][1];          a[0][1] = a[0][0];         a[1][1] = a[1][0];         a[0][0] += a01;         a[1][0] += a11;     }      function compute(n) {         if(n > 1) {             compute(n >> 1);             square();             if(n & 1)                 powPlusPlus();         }     }      fib.matrix_optimised = function(n) {         if(n == 0)             return 0;          a = [[1, 1], [1, 0]];         compute(n - 1);          return a[0][0];     }; })();  (function() {     var cache = {};     cache[0] = [[1, 0], [0, 1]];     cache[1] = [[1, 1], [1, 0]];      function mult(a, b) {         return [             [a[0][0] * b[0][0] + a[0][1] * b[1][0],                 a[0][0] * b[0][1] + a[0][1] * b[1][1]],             [a[1][0] * b[0][0] + a[1][1] * b[1][0],                 a[1][0] * b[0][1] + a[1][1] * b[1][1]]         ];     }      function compute(n) {         if(!cache[n]) {             var n_2 = n >> 1;             compute(n_2);             cache[n] = mult(cache[n_2], cache[n_2]);             if(n & 1)                 cache[n] = mult(cache[1], cache[n]);         }     }      fib.matrix_cached = function(n) {         if(n == 0)             return 0;          compute(--n);          return cache[n][0][0];     }; })();  function test(name, func, n, count) {     var value;      var start = Number(new Date);     while(count--)         value = func(n);     var end = Number(new Date);      return 'fib.' + name + '(' + n + ') = ' + value + ' [' +         (end - start) + 'ms]'; }  for(var func in fib)     document.writeln(test(func, fib[func], 1450, 10000));  </script></pre> 

    yields

    fib.round(1450) = 4.8149675025003456e+302 [20ms] fib.loop(1450) = 4.81496750250011e+302 [4035ms] fib.loop_cached(1450) = 4.81496750250011e+302 [8ms] fib.matrix(1450) = 4.814967502500118e+302 [2201ms] fib.matrix_optimised(1450) = 4.814967502500113e+302 [585ms] fib.matrix_cached(1450) = 4.814967502500113e+302 [12ms] 

    Your algorithm is nearly as bad as uncached looping. Caching is your best bet, closely followed by the rounding algorithm – which yields incorrect results for big n (as does your matrix algorithm).

    For smaller n, your algorithm performs even worse than everything else:

    fib.round(100) = 354224848179263100000 [20ms] fib.loop(100) = 354224848179262000000 [248ms] fib.loop_cached(100) = 354224848179262000000 [6ms] fib.matrix(100) = 354224848179261900000 [1911ms] fib.matrix_optimised(100) = 354224848179261900000 [380ms] fib.matrix_cached(100) = 354224848179261900000 [12ms] 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.