Count of total subarrays whose sum is a Fibonacci Numbers | Function to check whether a number is perfect square or not ; Function to check whether a number is fibonacci number or not ; If 5 * n * n + 4 or 5 * n * n - 5 is a perfect square , then the number is Fibonacci ; Function to count the subarray with sum fibonacci number ; Traverse the array arr [ ] to find the sum of each subarray ; To store the sum ; Check whether sum of subarray between [ i , j ] is fibonacci or not ; Driver Code ; Function Call
function isPerfectSquare ( x ) { var s = parseInt ( Math . sqrt ( x ) ) ; return ( s * s == x ) ; } function isFibonacci ( n ) { return isPerfectSquare ( 5 * n * n + 4 ) || isPerfectSquare ( 5 * n * n - 4 ) ; } function fibonacciSubarrays ( arr , n ) { var count = 0 ; for ( var i = 0 ; i < n ; ++ i ) { var sum = 0 ; for ( var j = i ; j < n ; ++ j ) { sum += arr [ j ] ; if ( isFibonacci ( sum ) ) { ++ count ; } } } document . write ( count ) ; } var arr = [ 6 , 7 , 8 , 9 ] ; var n = arr . length ; fibonacciSubarrays ( arr , n ) ;