Print the sequence of size N in which every term is sum of previous K terms | Function to generate the series in the form of array ; Pick a starting point ; Computing the previous sum ; Loop to print the series ; Driver code
def sumOfPrevK ( N , K ) : NEW_LINE INDENT arr = [ 0 ] * N ; NEW_LINE prevsum = 0 ; NEW_LINE arr [ 0 ] = 1 ; NEW_LINE for i in range ( N - 1 ) : NEW_LINE INDENT if ( i < K ) : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] + prevsum ; NEW_LINE prevsum = arr [ i + 1 ] ; NEW_LINE DEDENT else : NEW_LINE INDENT arr [ i + 1 ] = arr [ i ] + prevsum - arr [ i + 1 - K ] ; NEW_LINE prevsum = arr [ i + 1 ] ; NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT print ( arr [ i ] , end = " ▁ " ) ; NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 8 ; NEW_LINE K = 3 ; NEW_LINE sumOfPrevK ( N , K ) ; NEW_LINE DEDENT