CODING JOY

The stunning potpourri of coding and mundane life

GENERATING THE LAZY CATERER'S SEQUENCE IN JAVA LANGUAGE



AMRUHA AHMED
15th 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:

                
                    import java.util.*;
                    class lazycatererseq1
                    {
                        Scanner ob=new Scanner(System.in);
                        int fact(int a)//function to calculate factorial
                        {
                            if(a==0)
                            return 1;
                            else
                            return(a*fact(a-1));
                        }
                        void main()
                        {
                            int n;
                            System.out.print("Enter the maximum number of cuts to be made:");
                            n=ob.nextInt();
                            int term1=0,term2=0,term3=0; //term1 =nC0, term2 for nC1, term3 for nC2    
                            System.out.println("The lazy Caterer's Sequence is ...\n");
                            for(int 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;
                                System.out.print(finalterm+"\t");
                                
                            }
                        }
                    }     
             
         
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:

                       
import java.util.*;
class lazycatererseq2
{
    Scanner ob=new Scanner(System.in);
    void main()
    {
        int n;
        int i=0;//loop counter
        System.out.print("Enter the maximum number of cuts to be made:");
        n=ob.nextInt();
        System.out.println("The lazy Caterer's Sequence is ...\n");
        while(i<=n)
        {
            int temp=((i*i)+i+2)/2;//temporary variable
            System.out.print(temp+"\t");
            i++;
        }
    }
}

                        
                    
                

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