K | Function that finds the Nth element of K - Fibonacci series ; If N is less than K then the element is '1' ; first k elements are 1 ; ( K + 1 ) th element is K ; find the elements of the K - Fibonacci series ; subtract the element at index i - k - 1 and add the element at index i - i from the sum ( sum contains the sum of previous ' K ' elements ) ; set the new sum ; Driver code ; get the Nth value of K - Fibonacci series
< ? php function solve ( $ N , $ K ) { $ Array = array_fill ( 0 , $ N + 1 , NULL ) ; if ( $ N <= $ K ) { echo "1" . " STRNEWLINE " ; return ; } $ i = 0 ; $ sum = $ K ; for ( $ i = 1 ; $ i <= $ K ; ++ $ i ) { $ Array [ $ i ] = 1 ; } $ Array [ $ i ] = $ sum ; for ( $ i = $ K + 2 ; $ i <= $ N ; ++ $ i ) { $ Array [ $ i ] = $ sum - $ Array [ $ i - $ K - 1 ] + $ Array [ $ i - 1 ] ; $ sum = $ Array [ $ i ] ; } echo $ Array [ $ N ] . " STRNEWLINE " ; } $ N = 4 ; $ K = 2 ; solve ( $ N , $ K ) ; ? >