CODING JOY

The stunning potpourri of coding and mundane life

GENERATING THE LAZY CATERER'S SEQUENCE IN PYTHON 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:

                
def fact(a): #function to calculate factorial
  if a==0:
    return 1
  else:
    return(a*fact(a-1))

n=int(input("Enter the maximum number of cuts to be made:"))
term1=0 #term1 =nC0
term2=0 #term2 for nC1
term3=0 #term3 for nC2
for i in range(0,n+1): #i is the loop counter
  finalterm=0 #to store the sum of term1,term2 and term3(substitution of formula)
  term1=int(fact(i)/(fact(i)*fact(0)))
  if i!=0:
    term2=int(fact(i)/(fact(i-1)*fact(1)))
    if(i!=1):
      term3=int(fact(i)/(fact(i-2)*fact(2)))
    else:
      term3=0
  else:
    term2=0
  finalterm=term1+term2+term3
  print("{}\t".format(finalterm),end="")
                      
             
         
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:

                       
i=0 #loop counter
n=int(input("Enter the maximum number of cuts to be made:"))
print("The lazy caterer's Sequence is ...")
while i<=n:
  temp=int(((i*i)+i+2)/2) #temporary variable
  print("{}\t".format(temp),end="")
  i=i+1
                        
                    
                

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