Sum of squares of Fibonacci numbers | PHP Program to find sum of squares of Fibonacci numbers in O ( Log n ) time . ; Create an array for memoization ; Returns n 'th Fibonacci number using table f[] ; Base cases ; If fib ( n ) is already computed ; Applying above formula [ Note value n & 1 is 1 if n is odd , else 0 ] . ; Function to calculate sum of squares of Fibonacci numbers ; Driver Code
< ? php $ MAX = 1000 ; global $ f ; $ f = array_fill ( 0 , $ MAX , 0 ) ; function fib ( $ n ) { if ( $ n == 0 ) return 0 ; if ( $ n == 1 $ n == 2 ) return ( $ f [ $ n ] = 1 ) ; $ k = ( $ n & 1 ) ? ( $ n + 1 ) / 2 : $ n / 2 ; $ f [ $ n ] = ( $ n & 1 ) ? ( fib ( $ k ) * fib ( $ k ) + fib ( $ k - 1 ) * fib ( $ k - 1 ) ) : ( 2 * fib ( $ k - 1 ) + fib ( $ k ) ) * fib ( $ k ) ; return $ f [ $ n ] ; } function calculateSumOfSquares ( $ n ) { return fib ( $ n ) * fib ( $ n + 1 ) ; } $ n = 6 ; echo " Sum ▁ of ▁ Squares ▁ of ▁ Fibonacci ▁ numbers ▁ is ▁ : ▁ " ; echo calculateSumOfSquares ( $ n ) ; ? >