Represent K as sum of N | Array to store the N - Bonacci series ; Function to express K as sum of several N_bonacci numbers ; Driver code
let N_bonacci = new Array ( 100 ) ; for ( let i = 0 ; i < 100 ; i ++ ) { N_bonacci [ i ] = 0 ; } function N_bonacci_nums ( n , k ) { N_bonacci [ 0 ] = 1 ; for ( let i = 1 ; i <= 50 ; ++ i ) { for ( let j = i - 1 ; j >= i - k && j >= 0 ; -- j ) N_bonacci [ i ] += N_bonacci [ j ] ; } let ans = [ ] ; for ( let i = 50 ; i >= 0 ; -- i ) if ( n - N_bonacci [ i ] >= 0 ) { ans . push ( N_bonacci [ i ] ) ; n -= N_bonacci [ i ] ; } if ( ans . length == 1 ) ans . push ( 0 ) ; document . write ( ans . length + " " ) ; for ( let i = 0 ; i < ans . length ; ++ i ) document . write ( ans [ i ] + " " ) ; } let n = 21 , k = 5 ; N_bonacci_nums ( n , k ) ;