Spt function or Smallest Parts Function of a given number | Python3 implementation to find the Spt Function to 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
import sys NEW_LINE spt = 0 NEW_LINE def printVector ( arr ) : NEW_LINE INDENT global spt NEW_LINE min_i = sys . maxsize NEW_LINE for i in range ( len ( arr ) ) : NEW_LINE INDENT min_i = min ( min_i , arr [ i ] ) NEW_LINE DEDENT freq = arr . count ( min_i ) NEW_LINE spt += freq NEW_LINE DEDENT def findWays ( arr , i , n ) : NEW_LINE INDENT if ( n == 0 ) : NEW_LINE INDENT printVector ( arr ) NEW_LINE DEDENT for j in range ( i , n + 1 ) : NEW_LINE INDENT arr . append ( j ) NEW_LINE findWays ( arr , j , n - j ) NEW_LINE del arr [ - 1 ] NEW_LINE DEDENT DEDENT def spt_function ( n ) : NEW_LINE INDENT arr = [ ] NEW_LINE findWays ( arr , 1 , n ) NEW_LINE print ( spt ) NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT N = 4 NEW_LINE spt_function ( N ) NEW_LINE DEDENT