Python Program for Maximum Number of Squares that Can Fit in a Right Angle Isosceles Triangle

In the previous article, we have discussed Python Program for Minimum Height of a Triangle with Given Base and Area
Given the base b of an isosceles triangle and a value m, the task is to find the count of the maximum number of squares of side length m that can be fitted inside the given isosceles triangle.

Formula:

(b / m – 1) * (b / m) / 2

where b is the base of an isosceles triangle.

m is the sidelength of the square.

Examples:

Example1:

Input:

Given base of triangle = 8
Given side of square = 3

Output:

The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = 
2

Example2:

Input:

Given base of triangle = 5
Given side of square =  2

Output:

The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = 
1

Program for Maximum Number of Squares that Can Fit in a Right Angle Isosceles Triangle in Python

Below are the ways to find the count of the maximum number of squares of side length m that can be fitted inside the given isosceles triangle.

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the base of the triangle as static input and store it in a variable.
  • Give the side of the square as static input and store it in another variable.
  • Create a function to say count_maximumsqures() which takes the given base of the isosceles triangle and side of the square as the arguments and returns the count of the maximum number of squares of the given sidelength required that can be fitted inside the given isosceles triangle.
  • Inside the function, calculate the count of the maximum number of squares using the above mathematical formula and store it in a variable.
  • Return the above count value.
  • Pass the given base of the isosceles triangle and side of the square as the arguments to the count_maximumsqures() function, convert it into Integer using int() function, and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say count_maximumsqures() which takes the given base of the
# isosceles triangle and side of the square as the arguments and returns the
# count of the maximum number of squares of the given sidelength required that
# can be fitted inside the given isosceles triangle.


def count_maximumsqures(gvn_base, squre_side):
    # Inside the function, calculate the count of the maximum number of squares using the
    # above mathematical formula and store it in a variable.
    cnt = (gvn_base / squre_side - 1) * (gvn_base / squre_side) / 2
    # Return the above count value.
    return cnt


# Give the base of the triangle as static input and store it in a variable.
gvn_base = 8
# Give the side of the square as static input and store it in another variable.
squre_side = 3
print("The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = ")
# Pass the given base of the isosceles triangle and side of the square as the arguments
# to the count_maximumsqures() function, convert it into Integer using int()
# function, and print it.
print(int(count_maximumsqures(gvn_base, squre_side)))

Output:

The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = 
2

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the base of the triangle as user input using the int(input()) function and store it in a variable.
  • Give the side of the square as user input using the int(input()) function and store it in another variable.
  • Create a function to say count_maximumsqures() which takes the given base of the isosceles triangle and side of the square as the arguments and returns the count of the maximum number of squares of the given sidelength required that can be fitted inside the given isosceles triangle.
  • Inside the function, calculate the count of the maximum number of squares using the above mathematical formula and store it in a variable.
  • Return the above count value.
  • Pass the given base of the isosceles triangle and side of the square as the arguments to the count_maximumsqures() function, convert it into Integer using int() function, and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say count_maximumsqures() which takes the given base of the
# isosceles triangle and side of the square as the arguments and returns the
# count of the maximum number of squares of the given sidelength required that
# can be fitted inside the given isosceles triangle.


def count_maximumsqures(gvn_base, squre_side):
    # Inside the function, calculate the count of the maximum number of squares using the
    # above mathematical formula and store it in a variable.
    cnt = (gvn_base / squre_side - 1) * (gvn_base / squre_side) / 2
    # Return the above count value.
    return cnt


# Give the base of the triangle as user input using the int(input()) function
# and store it in a variable.
gvn_base =  int(input("Enter some random number = "))
# Give the side of the square as user input using the int(input()) function and 
# store it in another variable.
squre_side =  int(input("Enter some random number = "))
print("The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = ")
# Pass the given base of the isosceles triangle and side of the square as the arguments
# to the count_maximumsqures() function, convert it into Integer using int()
# function, and print it.
print(int(count_maximumsqures(gvn_base, squre_side)))


Output:

Enter some random number = 5
Enter some random number = 2
The maximum number of squares with given sidelength that can be fitted inside the isosceles triangle = 
1

Explore more Example Python Programs with output and explanation and practice them for your interviews, assignments and stand out from the rest of the crowd.