Count the total number of triangles after Nth operation Last Updated : 07 Dec, 2022 Summarize Comments Improve Suggest changes Share Like Article Like Report 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: At every operation, 3 uncolored triangles, 1 colored triangle, and the triangle itself is formedOn writing the above statement mathematically; count of triangles at Nth move = 3 * count of triangles at (N-1)th move + 2Therefore, initializing a variable curr = 1 and tri_count = 0Next, a loop is iterated from 1 to NFor every iteration, the operation mentioned above is performed.Finally, the tri_count is returned Below is the implementation of the above approach: C++ #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; } Java 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)); } } Python # 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)) C# 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 JavaScript <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) Comment More infoAdvertise with us Next Article Count the total number of triangles after Nth operation I imran_shaik Follow Improve Article Tags : Mathematical Aptitude DSA Quiz Computer Science Fundamentals GRE Quiz DSA triangle +3 More Practice Tags : Mathematical Similar Reads Sum of all the numbers in the Nth row of the given triangle Given a positive integer N, the task is to find the sum of all the numbers in the Nth row of the below triangle. 1 3 2 6 2 3 10 2 3 4 15 2 3 4 5 ... ... ... Examples: Input: N = 2 Output: 5 3 + 2 = 5Input: N = 3 Output: 11 6 + 2 + 3 = 11 Approach: Taking a closer look at the pattern, it can be obser 5 min read Number of triangles after N moves Find the number of triangles in Nth step, Rules: Draw an equilateral triangle at the start. In the i-th move, take the uncolored triangles, divides each of them in 4 parts of equal areas and color the central part. Keep a count of triangles till the Nth step. Examples: Input : 1 Output : 5 Explanati 6 min read Count number of right triangles possible with a given perimeter Given a perimeter P, the task is to find the number of right triangles possible with perimeter equal to p.Examples: Input: P = 12 Output: number of right triangles = 1 The only right angle possible is with sides hypotenuse = 5, perpendicular = 4 and base = 3. Input: p = 840 Output: number of right t 6 min read Count of triangles with total n points with m collinear There are 'n' points in a plane, out of which 'm' points are co-linear. Find the number of triangles formed by the points as vertices ?Examples : Input : n = 5, m = 4 Output : 6 Out of five points, four points are collinear, we can make 6 triangles. We can choose any 2 points from 4 collinear points 6 min read Count number of triangles possible for the given sides range Given four integers A, B, C, and D, the task is to find the number of distinct sets (X, Y, and Z) where X, Y and Z denotes the length of sides forming a valid triangle. A ? X ? B, B ? Y ? C, and C ? Z ? D.Examples: Input: A = 2, B = 3, C = 4, D = 5 Output: 7 Explanation: Possible Length of Side of T 10 min read Like