HomeData Modelling & AITriangular Matchstick Number

Triangular Matchstick Number

Given a number X which represents the floor of a matchstick pyramid, write a program to print the total number of matchstick required to form pyramid of matchsticks of x floors.

Examples: 

Input : X = 1
Output : 3

Input : X = 2
Output : 9 

This is mainly an extension of triangular numbers. For a number X, the matchstick required will be three times of X-th triangular numbers, i.e., (3*X*(X+1))/2

C++




// C++ program to find X-th triangular
// matchstick number
 
#include <bits/stdc++.h>
using namespace std;
 
int numberOfSticks(int x)
{
    return (3 * x * (x + 1)) / 2;
}
 
int main()
{
    cout<<numberOfSticks(7);
    return 0;
}


Java




// Java program to find X-th triangular
// matchstick number
public class TriangularPyramidNumber {
    public static int numberOfSticks(int x)
    {
        return (3 * x * (x + 1)) / 2;
    }
    public static void main(String[] args)
    {
        System.out.println(numberOfSticks(7));
    }
}


Python3




# Python program to find X-th triangular
# matchstick number
 
def numberOfSticks(x):
    return (3 * x * (x + 1)) / 2
     
# main()
print(int(numberOfSticks(7)))


C#




// C# program to find X-th triangular
// matchstick number
using System;
 
class GFG
{
    // Function to ind missing number
    static int numberOfSticks(int x)
    {
        return (3 * x * (x + 1)) / 2;
    }
 
    public static void Main()
    {
        Console.Write(numberOfSticks(7));
    }
}
 
// This code is contributed by _omg


PHP




<?php
// PHP program to find
// X-th triangular
// matchstick number
 
function numberOfSticks($x)
{
    return (3 * $x * ($x + 1)) / 2;
}
 
// Driver code
echo(numberOfSticks(7));
 
// This code is contributed by Ajit.
?>


Javascript




<script>
// javascript program to find X-th triangular
// matchstick number
 
function numberOfSticks( x)
{
    return (3 * x * (x + 1)) / 2;
}
 
   document.write(numberOfSticks(7));
 
// This code is contributed by aashish1995
 
</script>


Output: 

84

 

Time Complexity: O(1)

Auxiliary Space: O(1)

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32729 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
7111 POSTS0 COMMENTS
Nicole Veronica
12248 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12331 POSTS0 COMMENTS
Shaida Kate Naidoo
7225 POSTS0 COMMENTS
Ted Musemwa
7481 POSTS0 COMMENTS
Thapelo Manthata
7204 POSTS0 COMMENTS
Umr Jansen
7177 POSTS0 COMMENTS