Program for Triangular Matchstick Number

Python Program for Triangular Matchstick Number

In the previous article, we have discussed Python Program for Maximum Number of Squares that Can Fit in a Right Angle Isosceles Triangle
Given a number x, which is the floor of the matchstick pyramid and the task is to find the number of matchsticks needed to form a pyramid of matchsticks of x floors.

Formula:

(3*x*(x+1))/2

Examples:

Example1:

Input:

Given number of floors = 3

Output:

The Number of matchsticks needed to form a pyramid of matchsticks :
18

Example2:

Input:

Given number of floors = 5

Output:

The Number of matchsticks needed to form a pyramid of matchsticks :
45

Program for Triangular Matchstick Number in Python

Below are the ways to find the number of matchsticks needed to form a pyramid of matchsticks of x floors:

Method #1: Using Mathematical Formula (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Create a function to say count_MatchSticks() which takes the given number as an argument and returns the count of the number of matchsticks needed to form a pyramid of matchsticks for a given number of floors.
  • Inside the function, calculate the number of matchsticks needed using the above given mathematical formula and store it in a variable.
  • Return the above result.
  • Pass the given number as an argument to the count_MatchSticks() function, convert it into an integer using the int() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say count_MatchSticks() which takes the given number as an
# argument and returns the count of the number of matchsticks needed to form a
# pyramid of matchsticks for a given number of floors.


def count_MatchSticks(no_floors):
    # Inside the function, calculate the number of matchsticks needed using the above
    # given mathematical formula and store it in a variable.
    no_sticks = (3 * no_floors * (no_floors + 1)) / 2
    # Return the above result.
    return no_sticks


# Give the number as static input and store it in a variable.
no_floors = 3
print("The Number of matchsticks needed to form a pyramid of matchsticks :")
# Pass the given number as an argument to the count_MatchSticks() function, convert
# it into an integer using the int() function and print it.
print(int(count_MatchSticks(no_floors)))

Output:

The Number of matchsticks needed to form a pyramid of matchsticks :
18

Method #2: Using Mathematical Formula (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Create a function to say count_MatchSticks() which takes the given number as an argument and returns the count of the number of matchsticks needed to form a pyramid of matchsticks for a given number of floors.
  • Inside the function, calculate the number of matchsticks needed using the above given mathematical formula and store it in a variable.
  • Return the above result.
  • Pass the given number as an argument to the count_MatchSticks() function, convert it into an integer using the int() function and print it.
  • The Exit of the Program.

Below is the implementation:

# Create a function to say count_MatchSticks() which takes the given number as an
# argument and returns the count of the number of matchsticks needed to form a
# pyramid of matchsticks for a given number of floors.


def count_MatchSticks(no_floors):
    # Inside the function, calculate the number of matchsticks needed using the above
    # given mathematical formula and store it in a variable.
    no_sticks = (3 * no_floors * (no_floors + 1)) / 2
    # Return the above result.
    return no_sticks


# Give the number as user input using the int(input()) function and store it in a variable.
no_floors = int(input("Enter some random number = "))
print("The Number of matchsticks needed to form a pyramid of matchsticks :")
# Pass the given number as an argument to the count_MatchSticks() function, convert
# it into an integer using the int() function and print it.
print(int(count_MatchSticks(no_floors)))

Output:

Enter some random number = 5
The Number of matchsticks needed to form a pyramid of matchsticks :
45

Remediate your knowledge gap by attempting the Python Code Examples regularly and understand the areas of need and work on them.