Nth term of a recurrence relation generated by two given arrays | JavaScript 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 ; Given Array F [ ] and C [ ] ; Given N and K ; Function Call
let mod = 1e9 + 7 ; function NthTerm ( F , C , K , n ) { let ans = new Uint8Array ( n + 1 ) ; for ( let i = 0 ; i < K ; i ++ ) ans [ i ] = F [ i ] ; for ( let i = K ; i <= n ; i ++ ) { for ( let j = i - K ; j < i ; j ++ ) { ans [ i ] += ans [ j ] ; ans [ i ] %= mod ; } } document . write ( ans [ n ] + " " ) ; } let F = [ 0 , 1 ] ; let C = [ 1 , 1 ] ; let K = 2 ; let N = 10 ; NthTerm ( F , C , K , N ) ;