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
def solve ( N , K ) : NEW_LINE INDENT Array = [ 0 ] * ( N + 1 ) NEW_LINE DEDENT ' NEW_LINE INDENT if ( N <= K ) : NEW_LINE INDENT print ( "1" ) NEW_LINE return NEW_LINE DEDENT i = 0 NEW_LINE sm = K NEW_LINE for i in range ( 1 , K + 1 ) : NEW_LINE INDENT Array [ i ] = 1 NEW_LINE DEDENT Array [ i + 1 ] = sm NEW_LINE for i in range ( K + 2 , N + 1 ) : NEW_LINE INDENT Array [ i ] = sm - Array [ i - K - 1 ] + Array [ i - 1 ] NEW_LINE sm = Array [ i ] NEW_LINE DEDENT print ( Array [ N ] ) NEW_LINE DEDENT N = 4 NEW_LINE K = 2 NEW_LINE solve ( N , K ) NEW_LINE