Spt function or Smallest Parts Function of a given number | variable to store spt function of a number ; Function to add value of frequency of minimum element among all representations of n ; find the value of frequency of minimum element ; calculate spt ; Recursive function to find different ways in which n can be written as a sum of at one or more positive integers ; if sum becomes n , consider this representation ; start from previous element in the representation till n ; include current element from representation ; call function again with reduced sum ; backtrack - remove current element from representation ; Function to find the spt function ; Using recurrence find different ways in which n can be written as a sum of at 1 or more positive integers ; Driver Code
var spt = 0 ; function printVector ( arr ) { var min_i = 1000000000 ; for ( var i = 0 ; i < arr . length ; i ++ ) min_i = Math . min ( min_i , arr [ i ] ) ; var freq = 0 ; for ( var i = 0 ; i < arr . length ; i ++ ) { if ( arr [ i ] == min_i ) freq ++ ; } spt += freq ; } function findWays ( arr , i , n ) { if ( n == 0 ) printVector ( arr ) ; for ( var j = i ; j <= n ; j ++ ) { arr . push ( j ) ; findWays ( arr , j , n - j ) ; arr . pop ( ) ; } } function spt_function ( n ) { var arr = [ ] ; findWays ( arr , 1 , n ) ; document . write ( spt ) ; } var N = 4 ; spt_function ( N ) ;