Open In App

Spt function or Smallest Parts Function of a given number

Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to find the Spt Function of the number N.

The spt function (smallest parts function) is a function in number theory that counts the sum of the number of smallest parts in each partition of a positive integer. It is related to the partition function. 
For example Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]. 1 appears 4 times in the first, 1 twice in the second, 2 twice in the third, etc.; thus spt(4) = 4 + 2 + 2 + 1 + 1 = 10. 

Examples: 

Input: N = 4
Output: 10
Explanation:
Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]. 
1 appears 4 times in the first, 1 twice in the second, 2 twice in the third, etc.
Thus, spt(4) = 4 + 2 + 2 + 1 + 1 = 10 

Input: 5
Output: 14

 

Approach: The idea is to consider every integer from 1 to N and add it in an answer vector and again recur for remaining elements with reduced sum. To avoid printing the same representations again, each representation will be constructed in ascending order. If the representation of N is reached, we will find the frequency of minimum element present in the answer vector and add to the Spt value variable and print the value of Spt variable at last.

Below is the implementation of the above approach:

C++




// C++ implementation to find the
// Spt Function to given number
 
#include <bits/stdc++.h>
using namespace std;
 
// variable to store spt
// function of a number
int spt = 0;
 
// Function to add value
// of frequency of minimum element
// among all representations of n
void printVector(vector<int>& arr)
{
    int min_i = INT_MAX;
    for (int i = 0; i < arr.size(); i++)
        min_i = min(min_i, arr[i]);
 
    // find the value of frequency
    // of minimum element
    int freq = count(arr.begin(),
                     arr.end(),
                     min_i);
 
    // calculate spt
    spt += freq;
}
 
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
void findWays(vector<int>& arr,
              int i, int n)
{
    // if sum becomes n,
    // consider this representation
    if (n == 0)
        printVector(arr);
 
    // start from previous element
    // in the representation till n
    for (int j = i; j <= n; j++) {
 
        // include current element
        // from representation
        arr.push_back(j);
 
        // call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // backtrack - remove current
        // element from representation
        arr.pop_back();
    }
}
 
// Function to find
// the spt function
void spt_function(int n)
{
 
    vector<int> arr;
 
    // Using recurrence find
    // different ways in which
    // n can be written as a sum of
    // at 1 or more positive integers
    findWays(arr, 1, n);
 
    cout << spt;
}
 
// Driver Code
int main()
{
    int N = 4;
    spt_function(N);
    return 0;
}


Java




// Java implementation to find the
// Spt Function to given number
import java.util.*;
 
class GFG{
 
// Variable to store spt
// function of a number
static int spt = 0;
 
// Find the value of frequency
// of minimum element
static int count(Vector<Integer> arr, int min_i)
{
    int count = 0;
     
    for(int i = 0; i < arr.size(); i++)
        if(min_i == arr.get(i))
        count++;
         
    return count;
}
 
// Function to add value of
// frequency of minimum element
// among all representations of n
static void printVector(Vector<Integer> arr)
{
    int min_i = Integer.MAX_VALUE;
    for(int i = 0; i < arr.size(); i++)
        min_i = Math.min(min_i, arr.elementAt(i));
 
    // Find the value of frequency
    // of minimum element
    int freq = count(arr, min_i);
 
    // Calculate spt
    spt += freq;
}
 
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
static void findWays(Vector<Integer> arr,
                     int i, int n)
{
     
    // If sum becomes n, consider
    // this representation
    if (n == 0)
        printVector(arr);
 
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
         
        // Include current element
        // from representation
        arr.add(j);
 
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // backtrack - remove current
        // element from representation
        arr.remove(arr.size() - 1);
    }
}
 
// Function to find
// the spt function
static void spt_function(int n)
{
    Vector<Integer> arr = new Vector<>();
 
    // Using recurrence find
    // different ways in which
    // n can be written as a sum of
    // at 1 or more positive integers
    findWays(arr, 1, n);
 
    System.out.print(spt);
}
 
// Driver Code
public static void main(String[] args)
{
    int N = 4;
     
    spt_function(N);
}
}
 
// This code is contributed by amal kumar choubey


Python3




# Python3 implementation to find the
# Spt Function to given number
import sys
 
# Variable to store spt
# function of a number
spt = 0
 
# Function to add value
# of frequency of minimum element
# among all representations of n
def printVector(arr):
   
    global spt
     
    min_i = sys.maxsize
    for i in range(len(arr)):
        min_i = min(min_i, arr[i])
 
    # Find the value of frequency
    # of minimum element
    freq = arr.count(min_i)
 
    # Calculate spt
    spt += freq
 
# Recursive function to find
# different ways in which
# n can be written as a sum of
# at one or more positive integers
def findWays(arr, i, n):
   
    # If sum becomes n,
    # consider this representation
    if (n == 0):
        printVector(arr)
 
    # Start from previous element
    # in the representation till n
    for j in range(i, n + 1):
 
        # Include current element
        # from representation
        arr.append(j)
 
        # Call function again
        # with reduced sum
        findWays(arr, j, n - j)
 
        # Backtrack - remove current
        # element from representation
        del arr[-1]
 
# Function to find
# the spt function
def spt_function(n):
 
    arr = []
 
    # Using recurrence find
    # different ways in which
    # n can be written as a sum of
    # at 1 or more positive integers
    findWays(arr, 1, n)
 
    print(spt)
 
# Driver Code
if __name__ == '__main__':
   
    N = 4
    spt_function(N)
 
# This code is contributed by mohit kumar 29


C#




// C# implementation to find the
// Spt Function to given number
using System;
using System.Collections.Generic;
 
class GFG{
 
// Variable to store spt
// function of a number
static int spt = 0;
 
// Find the value of frequency
// of minimum element
static int count(List<int> arr, int min_i)
{
    int count = 0;
 
    for(int i = 0; i < arr.Count; i++)
        if (min_i == arr[i])
            count++;
 
    return count;
}
 
// Function to add value of
// frequency of minimum element
// among all representations of n
static void printList(List<int> arr)
{
    int min_i = int.MaxValue;
    for(int i = 0; i < arr.Count; i++)
        min_i = Math.Min(min_i, arr[i]);
 
    // Find the value of frequency
    // of minimum element
    int freq = count(arr, min_i);
 
    // Calculate spt
    spt += freq;
}
 
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
static void findWays(List<int> arr,
                          int i, int n)
{
 
    // If sum becomes n, consider
    // this representation
    if (n == 0)
        printList(arr);
 
    // Start from previous element
    // in the representation till n
    for(int j = i; j <= n; j++)
    {
         
        // Include current element
        // from representation
        arr.Add(j);
 
        // Call function again
        // with reduced sum
        findWays(arr, j, n - j);
 
        // backtrack - remove current
        // element from representation
        arr.RemoveAt(arr.Count - 1);
    }
}
 
// Function to find
// the spt function
static void spt_function(int n)
{
    List<int> arr = new List<int>();
 
    // Using recurrence find
    // different ways in which
    // n can be written as a sum of
    // at 1 or more positive integers
    findWays(arr, 1, n);
 
    Console.Write(spt);
}
 
// Driver Code
public static void Main(String[] args)
{
    int N = 4;
 
    spt_function(N);
}
}
 
// This code is contributed by amal kumar choubey


Javascript




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


Output: 

10

 



Last Updated : 01 Jul, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads