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
Explanation: In 1st move we get
Input : 2
Output : 17
Explanation: In 2nd move we get
Naive approach: The number of triangles in the nth figure are 3 times the number of triangles in the (n-1)th figure+2. We can see by observation the nth figure is made by placing 3 triangles similar to that in (n-1) figure and an inverted triangle. We also take into account the bigger triangle that has been formed. Hence the number of triangles in the nth figure becomes (number of triangles in the (n-1)th figure)*3 + 2.
C++
// C++ program to calculate the number of equilateral
// triangles
#include <bits/stdc++.h>
usingnamespacestd;
// function to calculate number of triangles in Nth step
intnumberOfTriangles(intn)
{
intanswer[n + 1] = { 0 };
answer[0] = 1;
for(inti = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
returnanswer[n];
}
// driver program
intmain()
{
intn = 2;
cout << numberOfTriangles(n);
return0;
}
Java
// Java program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
importjava.util.*;
classTriangle
{
// function to calculate number of
// triangles in Nth step
publicstaticintnumberOfTriangles(intn)
{
int[] answer = newint[n+1];
answer[0] = 1;
for(inti = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3+ 2;
returnanswer[n];
}
// driver code
publicstaticvoidmain(String[] args)
{
intn = 2;
System.out.println(numberOfTriangles(n));
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 code to calculate the
# number of equilateral triangles
# function to calculate number
# of triangles in Nth step
defnumberOfTriangles (n) :
answer =[None] *(n +1);
answer[0] =1;
i =1
whilei <=n:
answer[i] =answer[i -1] *3+2;
i =i +1
returnanswer[n];
# Driver code
n =2
print(numberOfTriangles(n))
# This code is contributed by "rishabh_jain".
C#
// C# program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
usingSystem;
classTriangle
{
// function to calculate number of
// triangles in Nth step
publicstaticintnumberOfTriangles(intn)
{
int[] answer = newint[n+1];
answer[0] = 1;
for(inti = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
returnanswer[n];
}
// Driver code
publicstaticvoidMain()
{
intn = 2;
Console.WriteLine(numberOfTriangles(n));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to calculate
// the number of equilateral
// triangles
// function to calculate number
// of triangles in Nth step
functionnumberOfTriangles($n)
{
$answer= array();
$answer[0] = 1;
for($i= 1; $i<= $n; $i++)
$answer[$i] = $answer[$i- 1] *
3 + 2;
return$answer[$n];
}
// Driver Code
$n= 2;
echonumberOfTriangles($n);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// Javascript program to calculate
// the number of equilateral
// triangles
// Function to calculate number
// of triangles in Nth step
functionnumberOfTriangles(n)
{
let answer = newUint8Array(n + 1);
answer[0] = 1;
for(let i = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
returnanswer[n];
}
// Driver code
let n = 2;
document.write(numberOfTriangles(n));
// This code is contributed by Mayank Tyagi
</script>
Output
17
Time Complexity: O(n) Auxiliary Space: O(n)
An efficient solution will be to derive a formula for Nth step: If we follow the naive approach for every step, then we get for Nth step the number of triangles to be
(2*(3^n))-1.
C++
// C++ program to calculate the number of
// equilateral triangles
#include <bits/stdc++.h>
usingnamespacestd;
// function to calculate number of triangles
// in Nth step
intnumberOfTriangles(intn)
{
intans = 2 * (pow(3, n)) - 1;
returnans;
}
// driver program
intmain()
{
intn = 2;
cout << numberOfTriangles(n);
return0;
}
Java
// Java program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
importjava.util.*;
importstaticjava.lang.Math.pow;
classTriangle
{
// function to calculate number
// of triangles in Nth step
publicstaticdoublenumberOfTriangles(intn)
{
doubleans = 2* (pow(3, n)) - 1;
returnans;
}
// driver code
publicstaticvoidmain(String[] args)
{
intn = 2;
System.out.println(numberOfTriangles(n));
}
}
// This code is contributed by rishabh_jain
Python3
# Python3 code to calculate the
# number of equilateral triangles
# function to calculate number
# of triangles in Nth step
defnumberOfTriangles (n) :
ans =2*(pow(3, n)) -1;
returnans;
# Driver code
n =2
print(numberOfTriangles(n))
# This code is contributed by "rishabh_jain".
C#
//C# program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
usingSystem;
classTriangle
{
// function to calculate number
// of triangles in Nth step
publicstaticdoublenumberOfTriangles(intn)
{
doubleans = 2 * (Math.Pow(3, n)) - 1;
returnans;
}
// Driver code
publicstaticvoidMain()
{
intn = 2;
Console.WriteLine(numberOfTriangles(n));
}
}
// This code is contributed by vt_m
PHP
<?php
// PHP program to calculate the
// number of equilateral triangles
// function to calculate
// number of triangles
// in Nth step
functionnumberOfTriangles($n)
{
$ans= 2 * (pow(3, $n)) - 1;
return$ans;
}
// Driver Code
$n= 2;
echonumberOfTriangles($n);
// This code is contributed by anuj_67.
?>
Javascript
<script>
// javascript program to find middle of three
// distinct numbers to calculate the
// number of equilateral triangles
// function to calculate number
// of triangles in Nth step
functionnumberOfTriangles(n)
{
varans = 2 * (Math.pow(3, n)) - 1;
returnans;
}
// Driver code
varn = 2;
document.write(numberOfTriangles(n));
// This code is contributed by aashish1995
</script>
Output
17
Time Complexity: O(log n), due to the inbuilt pow() function. 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!