Nth term of a recurrence relation generated by two given arrays | Python3 program for the above approach ; Function to calculate Nth term of general recurrence relations ; Stores the generated sequence ; Current term is sum of previous k terms ; Print the nth term ; Driver code ; Given Array F [ ] and C [ ] ; Given N and K ; Function call
mod = 1e9 + 7 NEW_LINE def NthTerm ( F , C , K , n ) : NEW_LINE INDENT ans = [ 0 ] * ( n + 1 ) NEW_LINE i = 0 NEW_LINE while i < K : NEW_LINE INDENT ans [ i ] = F [ i ] NEW_LINE i += 1 NEW_LINE DEDENT i = K NEW_LINE while i <= n : NEW_LINE INDENT j = i - K NEW_LINE while j < i : NEW_LINE INDENT ans [ i ] += ans [ j ] NEW_LINE ans [ i ] %= mod NEW_LINE j += 1 NEW_LINE DEDENT i += 1 NEW_LINE DEDENT print ( int ( ans [ n ] ) ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT F = [ 0 , 1 ] NEW_LINE C = [ 1 , 1 ] NEW_LINE K = 2 NEW_LINE N = 10 NEW_LINE NthTerm ( F , C , K , N ) NEW_LINE DEDENT