Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

A partition of a positive integer N is a sequence of integers which sum to N , u

ID: 3827419 • Letter: A

Question

A partition of a positive integer N is a sequence of integers which sum to N , usually written with plus signs between the numbers of the partition. For example

15 = 1+2+3+4+5 = 1+2+1+7+1+2+1

A partition is palindromic if it reads the same forward and backward. The first partition in the example is not palindromic while the second is. If a partition containing m integers is palindromic, its left half is the first floor(m/2) integers and its right half is the last floor(m/2) integers (which must be the reverse of the left half. (floor(x) is the greatest integer less than or equal to x .)

A partition is recursively palindromic if it is palindromic and its left half is recursively palindromic or empty. Note that every integer has at least two recursively palindromic partitions one consisting of all ones and a second consisting of the integer itself. The second example above is also recursively palindromic.

For example, the recursively palindromic partitions of 7 are:

7, 1+5+1, 2+3+2, 1+1+3+1+1, 3+1+3, 1+1+1+1+1+1+1

Write a program which takes as input an integer N and outputs the number of recursively palindromic partitions of N .

Explanation / Answer

#include<iostream>
using namespace std;
int palendromPartitionCount=0;
void calculateCount(int p[], int n)// method to count the no of recursive plandromic partitions
{
if(n%2!=0)
{
int temp=n/2;
int count=0;
   for (int i = 0; i < n-1; i++)//checking for recursive plandromic partitions
   {
   for(int j=i+1;j<n;j++)
   {
   if(p[i]==p[j])
   {
   count++;
   break;
   }
   }
  
   }
   if(count>=temp)
   palendromPartitionCount++;
}

}

void findRecursivePal(int n)// Method for finding possible partitions for the provided number
{
   int p[n];
   int k = 0;
   p[k] = n;

   while (true)
   {
       calculateCount(p, k+1);
       int rem_val = 0;
       while (k >= 0 && p[k] == 1)
       {
           rem_val += p[k];
           k--;
       }
       if (k < 0) return;
       p[k]--;
       rem_val++;
       while (rem_val > p[k])
       {
           p[k+1] = p[k];
           rem_val = rem_val - p[k];
           k++;
       }
       p[k+1] = rem_val;
       k++;
   }
}

int main()
{
cout<<"Enter the number:";
int number=0;
cin>>number;
   findRecursivePal(number);
   cout<<"The number of recursively palindromic partitions of "<<number<<" is :"<<palendromPartitionCount;
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote