Open In App

Count the total number of triangles after Nth operation

Given an equilateral triangle, the task is to compute the total number of triangles after performing the following operation N times. 
For every operation, the uncolored triangles are taken and divided into 4 equal equilateral triangles. Every inverted triangle formed is colored. Refer to the below figure for more details.

For N=1 the triangle formed is:



For N=2 the triangle formed is:
 



Examples: 

Input :N = 10 
Output : 118097

Input : N = 2 
Output : 17 

Approach: 

Below is the implementation of the above approach: 




#include <bits/stdc++.h>
using namespace std;
// function to return the
// total no.of Triangles
int CountTriangles(int n)
{
    int curr = 1;
    int Tri_count = 0;
    for (int i = 1; i <= n; i++) {
        // For every subtriangle formed
        // there are possibilities of
        // generating (curr*3)+2
 
        Tri_count = (curr * 3) + 2;
        // Changing the curr value to Tri_count
        curr = Tri_count;
    }
    return Tri_count;
}
 
// driver code
int main()
{
    int n = 10;
    cout << CountTriangles(n);
    return 0;
}




import java.io.*;
 
public class Gfg {
    // Method to return the
    // total no.of Triangles
    public static int CountTriangles(int n)
    {
        int curr = 1;
        int Tri_count = 0;
        for (int i = 1; i <= n; i++) {
            // For every subtriangle formed
            // there are possibilities of
            // generating (curr*3)+2
 
            Tri_count = (curr * 3) + 2;
            // Changing the curr value to Tri_count
            curr = Tri_count;
        }
        return Tri_count;
    }
 
    // driver code
    public static void main(String[] args)
    {
        int n = 10;
        System.out.println(CountTriangles(n));
    }
}




# Function to return the
# total no.of Triangles
def countTriangles(n):
     
    curr = 1
    Tri_count = 0
    for i in range(1, n + 1):
             
        # For every subtriangle formed
        # there are possibilities of
        # generating (curr * 3)+2
        Tri_count = (curr * 3) + 2
        # Changing the curr value to Tri_count
        curr = Tri_count
    return Tri_count
     
n = 10
print(countTriangles(n))




using System;
 
class Gfg
{
    // Method to return the
    // total no.of Triangles
    public static int CountTriangles(int n)
    {
        int curr = 1;
        int Tri_count = 0;
        for (int i = 1; i <= n; i++)
        {
            // For every subtriangle formed
            // there are possibilities of
            // generating (curr*3)+2
            Tri_count = (curr * 3) + 2;
             
            // Changing the curr value to Tri_count
            curr = Tri_count;
        }
        return Tri_count;
    }
 
    // Driver code
    public static void Main(String[] args)
    {
        int n = 10;
        Console.WriteLine(CountTriangles(n));
    }
}
 
// This code is contributed by 29AjayKumar




<script>
    // Method to return the
    // total no.of Triangles
    function CountTriangles(n)
    {
        var curr = 1;
        var Tri_count = 0;
        for (i = 1; i <= n; i++)
        {
         
            // For every subtriangle formed
            // there are possibilities of
            // generating (curr*3)+2
 
            Tri_count = (curr * 3) + 2;
            // Changing the curr value to Tri_count
            curr = Tri_count;
        }
        return Tri_count;
    }
 
    // driver code
        var n = 10;
        document.write(CountTriangles(n));
 
// This code is contributed by aashish1995
</script>

Output: 
118097

 

Time Complexity: O(n)
Auxiliary Space: O(1)


Article Tags :