Find nth Fibonacci number using Golden ratio | Approximate value of golden ratio ; Fibonacci numbers upto n = 5 ; Function to find nth Fibonacci number ; Fibonacci numbers for n < 6 ; Else start counting from 5 th term ; driver code
let PHI = 1.6180339 ; let f = [ 0 , 1 , 1 , 2 , 3 , 5 ] ; function fib ( n ) { if ( n < 6 ) return f [ n ] ; let t = 5 , fn = 5 ; while ( t < n ) { fn = Math . round ( fn * PHI ) ; t ++ ; } return fn ; } let n = 9 ; document . write ( n + " " + fib ( n ) + " " ) ;