Count of consecutive Fibonacci pairs in the given Array | Function to find the previous fibonacci for the number N ; Function to find the next fibonacci number for the number N ; Function to check that a Number is a perfect square or not ; Function to check that a number is fibonacci number or not ; N is Fibinacci if one of ( 5 * n * n + 4 ) or ( 5 * n * n - 4 ) is a perferct square ; Function to count the fibonacci pairs in the array ; Loop to iterate over the array to choose all pairs of the array ; Condition to check if both the number of pair is a fibonacci number ; Condition to check if both the number form consecutive fibonacci numbers ; Driver Code
function previousFibonacci ( n ) { var a = n / ( ( 1 + Math . sqrt ( 5 ) ) / 2.0 ) ; return Math . round ( a ) ; } function nextFibonacci ( n ) { var a = n * ( 1 + Math . sqrt ( 5 ) ) / 2.0 ; return Math . round ( a ) ; } function isPerfectSquare ( x ) { var s = Math . sqrt ( x ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return ( isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ) ; } function countFibonacciPairs ( arr , n ) { var res = 0 ; for ( var i = 0 ; i < n ; i ++ ) for ( var j = i + 1 ; j < n ; j ++ ) if ( isFibonacci ( arr [ i ] ) && isFibonacci ( arr [ j ] ) ) { var prevFib = previousFibonacci ( arr [ i ] ) ; var nextFib = nextFibonacci ( arr [ i ] ) ; if ( prevFib == arr [ j ] nextFib == arr [ j ] ) { res ++ ; } } return res ; } var a = [ 3 , 5 , 8 , 11 ] ; var n = a . length ; document . write ( countFibonacciPairs ( a , n ) ) ;