CODING JOY

The stunning potpourri of coding and mundane life

GENERATING THE LAZY CATERER'S SEQUENCE IN C++ LANGUAGE



AMRUHA AHMED
14th October,2023.



The lazy caterer's sequence provides you with a lucid way to calculate the maximum number of pieces of a disk shaped object which can be made using straight cuts.

The formula to generate this sequence is:

(n^2+n+2)/2 or simply nC0+nC1+nC2(if n>=2)

Where n represents the number of cuts made to the disk-like object.

blog1

In this blog post, I will be discussing about the generation of the lazy caterer's sequence using the formulae mentioned above.

METHOD-1

In this method, the formula of nC0+nC1+nC2 will be used to calculate the terms of the lazy caterer's sequence. The following things need to be kept in mind while following this approach:

VARIABLES REQUIRED:

FUNCTIONS REQUIRED:

ALGORITHM FOR THE MAIN FUNCTION:


PROGRAM:

                
    #include<iostream>
    using namespace std;
    int fact(int a)//function to calculate factorial
    {
        if(a==0)
        return 1;
        else
        return(a*fact(a-1));
    }
    int main()
    {
        int n;
        int i;
        cout<<"Enter the maximum number of cuts to be made:";
        cin>>n;
        int term1=0,term2=0,term3=0; //term1 =nC0, term2 for nC1, term3 for nC2    
        cout<<"The lazy Caterer's Sequence is ...\n";
        for(i=0;i<=n;i++)// i is the loop counter
        {
            int finalterm=0;//to store the sum of term1,term2 and term3(substitution of formula)
            term1=fact(i)/(fact(i)*fact(0));
            if(i!=0)
            {               
                term2=fact(i)/(fact(i-1)*fact(1));
                if(i!=1)
                term3=fact(i)/(fact(i-2)*fact(2));
                else
                term3=0;        
            }
            else
            {
                term2=0;
            }
            finalterm=term1+term2+term3;
            cout<<finalterm<<"\t";
            
        }
        return 0;
    }

                
             
         
CODE COPIED

DRY RUN:

if n=4 then the following steps are executed in the loop: blog1

OUTPUT:

Enter the maximum number of cuts to be made : 4
The lazy caterer's sequence is...

1 2 4 7 11

METHOD-2:

In this method, the formula of (n^2+n+2)/2 will be used to calculate the terms of the lazy caterer's sequence.

VARIABLES REQUIRED:

ALGORITHM FOR THE MAIN FUNCTION:

PROGRAM:

                       
                        #include<iostream>
                        using namespace std;
                        int main()
                        {
                            int n;
                            int i=0;//loop counter
                            cout<<"Enter the maximum number of cuts to be made:";
                            cin>>n;
                            cout<<"The lazy Caterer's Sequence is ...\n";
                            while(i<=n)
                            {
                                int temp=((i*i)+i+2)/2;//temporary variable
                                cout<<temp<<"\t";
                                i++;
                            }
                            return 0;
                        }


                    
                

DRY RUN:

if n=4 then the following steps are executed in the loop: blog1

OUTPUT:

Enter the maximum number of cuts to be made : 4
The lazy caterer's sequence is...
1 2 4 7 11