Python Program for Pizza Cut Problem (Or Circle Division by Lines)

In the previous article, we have discussed Python Program to Check Whether Triangle is Valid or Not if Sides are Given
Given the number of cuts and the task is to get the maximum number of pieces from the given number of cuts in python.

Formula:

1 + n*(n+1)/2

where n= number of cuts.

Examples:

Example1:

Input:

Given number of cuts = 4

Output:

The maximum number of pieces from the given number of cuts =  11

Example2:

Input:

Given number of cuts = 5

Output:

The maximum number of pieces from the given number of cuts =  16

Program for Pizza Cut Problem (Or Circle Division by Lines) in Python

Below are the ways to get the maximum number of pieces from the given number of cuts in python:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the number of cuts as static input and store it in a variable.
  • Create a function to say Get_Max_Pieces() which takes the given number of cuts as an argument, and returns the maximum number of pieces from the given number of cuts.
  • Inside the function, calculate the maximum number of pieces by using the above given mathematical formula and convert it to an integer using the int() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given number of cuts as an argument to the Get_Max_Pieces() function and store it in another variable.
  • Print the above result which is the maximum number of pieces from the given number of cuts.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Get_Max_Pieces() which takes the given number of cuts as
# an argument, and returns the maximum number of pieces from the given number of cuts.


def Get_Max_Pieces(no_of_cuts):
    # Inside the function, calculate the maximum number of pieces by using the above
        # given mathematical formula and convert it to an integer using the int() function.
        # Store it in another variable.
    k = int(1 + no_of_cuts * (no_of_cuts + 1) / 2)
    # Return the above result.
    return k


# Give the number of cuts as static input and store it in a variable.
no_of_cuts = 4
# Pass the given number of cuts as an argument to the Get_Max_Pieces() function
# and store it in another variable.
maxim_pieces = Get_Max_Pieces(no_of_cuts)
# Print the above result which is the maximum number of pieces from the given
# number of cuts.
print("The maximum number of pieces from the given number of cuts = ", maxim_pieces)

Output:

The maximum number of pieces from the given number of cuts =  11

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the number of cuts as user input using the int(input()) function and store it in a variable.
  • Create a function to say Get_Max_Pieces() which takes the given number of cuts as an argument, and returns the maximum number of pieces from the given number of cuts.
  • Inside the function, calculate the maximum number of pieces by using the above given mathematical formula and convert it to an integer using the int() function.
  • Store it in another variable.
  • Return the above result.
  • Pass the given number of cuts as an argument to the Get_Max_Pieces() function and store it in another variable.
  • Print the above result which is the maximum number of pieces from the given number of cuts.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say Get_Max_Pieces() which takes the given number of cuts as
# an argument, and returns the maximum number of pieces from the given number of cuts.


def Get_Max_Pieces(no_of_cuts):
    # Inside the function, calculate the maximum number of pieces by using the above
        # given mathematical formula and convert it to an integer using the int() function.
        # Store it in another variable.
    k = int(1 + no_of_cuts * (no_of_cuts + 1) / 2)
    # Return the above result.
    return k


# Give the number of cuts as user input using the int(input()) function 
# and store it in a variable.
no_of_cuts = int(input("Enter some random number = "))
# Pass the given number of cuts as an argument to the Get_Max_Pieces() function
# and store it in another variable.
maxim_pieces = Get_Max_Pieces(no_of_cuts)
# Print the above result which is the maximum number of pieces from the given
# number of cuts.
print("The maximum number of pieces from the given number of cuts = ", maxim_pieces)

Output:

Enter some random number = 5
The maximum number of pieces from the given number of cuts = 16

Dive into numerous Python Programming Language Examples for practice and get the best out of the tutorial and learn python one step at a time.