Spt function or Smallest Parts Function of a given number
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++
#include <bits/stdc++.h>
using namespace std;
int spt = 0;
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]);
int freq = count(arr.begin(),
arr.end(),
min_i);
spt += freq;
}
void findWays(vector< int >& arr,
int i, int n)
{
if (n == 0)
printVector(arr);
for ( int j = i; j <= n; j++) {
arr.push_back(j);
findWays(arr, j, n - j);
arr.pop_back();
}
}
void spt_function( int n)
{
vector< int > arr;
findWays(arr, 1, n);
cout << spt;
}
int main()
{
int N = 4;
spt_function(N);
return 0;
}
|
Java
import java.util.*;
class GFG{
static int spt = 0 ;
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;
}
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));
int freq = count(arr, min_i);
spt += freq;
}
static void findWays(Vector<Integer> arr,
int i, int n)
{
if (n == 0 )
printVector(arr);
for ( int j = i; j <= n; j++)
{
arr.add(j);
findWays(arr, j, n - j);
arr.remove(arr.size() - 1 );
}
}
static void spt_function( int n)
{
Vector<Integer> arr = new Vector<>();
findWays(arr, 1 , n);
System.out.print(spt);
}
public static void main(String[] args)
{
int N = 4 ;
spt_function(N);
}
}
|
Python3
import sys
spt = 0
def printVector(arr):
global spt
min_i = sys.maxsize
for i in range ( len (arr)):
min_i = min (min_i, arr[i])
freq = arr.count(min_i)
spt + = freq
def findWays(arr, i, n):
if (n = = 0 ):
printVector(arr)
for j in range (i, n + 1 ):
arr.append(j)
findWays(arr, j, n - j)
del arr[ - 1 ]
def spt_function(n):
arr = []
findWays(arr, 1 , n)
print (spt)
if __name__ = = '__main__' :
N = 4
spt_function(N)
|
C#
using System;
using System.Collections.Generic;
class GFG{
static int spt = 0;
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;
}
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]);
int freq = count(arr, min_i);
spt += freq;
}
static void findWays(List< int > arr,
int i, int n)
{
if (n == 0)
printList(arr);
for ( int j = i; j <= n; j++)
{
arr.Add(j);
findWays(arr, j, n - j);
arr.RemoveAt(arr.Count - 1);
}
}
static void spt_function( int n)
{
List< int > arr = new List< int >();
findWays(arr, 1, n);
Console.Write(spt);
}
public static void Main(String[] args)
{
int N = 4;
spt_function(N);
}
}
|
Javascript
<script>
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);
</script>
|
Last Updated :
01 Jul, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...