S
S
Somewhere Intech2019-12-15 15:49:02
JavaScript
Somewhere Intech, 2019-12-15 15:49:02

Fibonacci: Is it possible to find the Fibonacci sequence number ( x ) from a number ( y )?

The question is closer to schoolchildren
Need to implement the findX function

const Fibonacci = function(){
  const A = 5 ** 0.5, 
        B = (1 + A) / 2, 
        Y = x => Math.round(B ** x / A);
  this.findY = x => Y(x)
  this.findX = y => 
}

let f = new Fibonacci ();
for (i=0;i<10;i++){
  console.log(i+ ':' +f.findX(i))
}

Decision

Math.Fibonacci = new function(cacheSize = 10){
  var _current = 0, _cache = [], C = cacheSize
  const A = 5 ** 0.5, 
    B = (1 + A) / 2,
    LNb = Math.log( B ),
    Y = x => cache.add( [ x, Math.round( B ** x / A ) ] )[ 1 ],
    R = [ 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 ],
    X = y => Math.log( A * y ) / LNb,
    cache = {
      add: ( xy ) => {
        if ( _cache.length == C ) _cache = _cache.slice(1,)
        _cache.push(xy)
        return xy
      },
      get: n => n in R ? R[n] : ( ( _i = _cache.findIndex(e=>e[0]==n)) != -1 ? _cache[_i][1] : null ),
      find: x => ( _i = R.indexOf(x) ) != -1 ? _i : ( ( _i = _cache.findIndex(e=>e[1]==x)) != -1 ? _cache[_i][0] : null )
    }

  this.get = n => ( _value = cache.get( n ) ) !== null ? _value : Y( n );

  this.find = x => {
    if ( ( _value = cache.find( x ) ) !== null ) return _value
    let a = this.adjacent( x );
    return a[0][1] == x ? a[0][0] : ( a[1][1] == x ? a[1][0] : -1 )
  }
  
  this.adjacent = y => {
    let x = X(y), 
      x1 = Math.floor(x), 
      x2 = Math.ceil(x), 
      y1 = Y(x1),
      y2 = Y(x2);
    if ( y1 == y ) return [ cache.add( [x1,y1] ), [x1,y1] ]
    else if ( y2 == y ) return [ cache.add( [x2,y2] ), [x2,y2] ]
    else return [ cache.add( [ x1, Y(x1) ] ), cache.add( [ x2, Y(x2) ] ) ]
  }
  
  this.setCacheSize = l => l > 0 ? C = l : false
  
  this.next = _ => Y( ++_current )
  this.prev = _ => Y( --_current )
  this.current = _ => Y( _current )
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2019-12-16
@john36allTa

Not a schoolboy, but everything seems to be simple.
You have y = B^x/A
Given y, hence x= log_B(A*y) = ln(A*y) / ln(B).
It will need to be rounded up. The formula may fail on small numbers.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question