{"size": 787, "ext": "js", "max_stars_repo_path": "javascript/problem-25/lib/fibonacci.js", "max_stars_repo_name": "jeremybuis/project-euler", "max_stars_repo_stars_event_min_datetime": null, "max_stars_repo_stars_event_max_datetime": null, "max_issues_repo_path": "javascript/problem-25/lib/fibonacci.js", "max_issues_repo_name": "jeremybuis/project-euler", "max_issues_repo_issues_event_min_datetime": "2016-07-22T18:52:46.000Z", "max_issues_repo_issues_event_max_datetime": "2016-07-22T18:52:51.000Z", "max_forks_repo_path": "javascript/problem-25/lib/fibonacci.js", "max_forks_repo_name": "jeremybuis/project-euler", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": null, "max_issues_count": 1, "max_forks_count": null, "avg_line_length": 23.1470588235, "max_line_length": 87, "alphanum_fraction": 0.5997458704}
var bigint = require('bigint');

//cant use bigint with this since it deals in real numbers - stops working at term 1475
var closedFibonacci = (function() {
  var sqrtFive = Math.sqrt(5);

  var posPhi = (1 + sqrtFive)/2;
  var negPhi = (1 - sqrtFive)/2;

  var numbers = {};
  
  return function(n) {
    if (numbers[n] === undefined || numbers[n] === null) {

      var coefficent = 1/sqrtFive;
      var firstTerm = Math.pow(posPhi, n);
      var secondTerm = Math.pow(negPhi, n);

      var number = Math.round(coefficent * (firstTerm - secondTerm));
      
      numbers[n] = number;
    }
    return numbers[n];
  };

})();

function fib(n) {
  return function(n, a, b) {
    return n>0 ? arguments.callee(n-1,b,a.add(b)) : a;
  }(n, bigint(0), bigint(1));
}

module.exports = fib;