Sum of Fibonacci Numbers in a range | Function to return the nth Fibonacci number ; Function to return the required sum ; To store the sum ; Calculate the sum ; Driver code
< ? php function fib ( $ n ) { $ phi = ( 1 + sqrt ( 5 ) ) / 2 ; return ( int ) round ( pow ( $ phi , $ n ) / sqrt ( 5 ) ) ; } function calculateSum ( $ l , $ r ) { $ sum = 0 ; for ( $ i = $ l ; $ i <= $ r ; $ i ++ ) $ sum += fib ( $ i ) ; return $ sum ; } $ l = 4 ; $ r = 8 ; echo calculateSum ( $ l , $ r ) ; ? >