Number of triangles after N moves
Last Updated :
17 Feb, 2023
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++
#include <bits/stdc++.h>
using namespace std;
int numberOfTriangles( int n)
{
int answer[n + 1] = { 0 };
answer[0] = 1;
for ( int i = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
return answer[n];
}
int main()
{
int n = 2;
cout << numberOfTriangles(n);
return 0;
}
|
Java
import java.util.*;
class Triangle
{
public static int numberOfTriangles( int n)
{
int [] answer = new int [n+ 1 ];
answer[ 0 ] = 1 ;
for ( int i = 1 ; i <= n; i++)
answer[i] = answer[i - 1 ] * 3 + 2 ;
return answer[n];
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(numberOfTriangles(n));
}
}
|
Python3
def numberOfTriangles (n) :
answer = [ None ] * (n + 1 );
answer[ 0 ] = 1 ;
i = 1
while i < = n:
answer[i] = answer[i - 1 ] * 3 + 2 ;
i = i + 1
return answer[n];
n = 2
print (numberOfTriangles(n))
|
C#
using System;
class Triangle
{
public static int numberOfTriangles( int n)
{
int [] answer = new int [n+1];
answer[0] = 1;
for ( int i = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
return answer[n];
}
public static void Main()
{
int n = 2;
Console.WriteLine(numberOfTriangles(n));
}
}
|
PHP
<?php
function numberOfTriangles( $n )
{
$answer = array ();
$answer [0] = 1;
for ( $i = 1; $i <= $n ; $i ++)
$answer [ $i ] = $answer [ $i - 1] *
3 + 2;
return $answer [ $n ];
}
$n = 2;
echo numberOfTriangles( $n );
?>
|
Javascript
<script>
function numberOfTriangles(n)
{
let answer = new Uint8Array(n + 1);
answer[0] = 1;
for (let i = 1; i <= n; i++)
answer[i] = answer[i - 1] * 3 + 2;
return answer[n];
}
let n = 2;
document.write(numberOfTriangles(n));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
int numberOfTriangles( int n)
{
int ans = 2 * ( pow (3, n)) - 1;
return ans;
}
int main()
{
int n = 2;
cout << numberOfTriangles(n);
return 0;
}
|
Java
import java.util.*;
import static java.lang.Math.pow;
class Triangle
{
public static double numberOfTriangles( int n)
{
double ans = 2 * (pow( 3 , n)) - 1 ;
return ans;
}
public static void main(String[] args)
{
int n = 2 ;
System.out.println(numberOfTriangles(n));
}
}
|
Python3
def numberOfTriangles (n) :
ans = 2 * ( pow ( 3 , n)) - 1 ;
return ans;
n = 2
print (numberOfTriangles(n))
|
C#
using System;
class Triangle
{
public static double numberOfTriangles( int n)
{
double ans = 2 * (Math.Pow(3, n)) - 1;
return ans;
}
public static void Main()
{
int n = 2;
Console.WriteLine(numberOfTriangles(n));
}
}
|
PHP
<?php
function numberOfTriangles( $n )
{
$ans = 2 * (pow(3, $n )) - 1;
return $ans ;
}
$n = 2;
echo numberOfTriangles( $n );
?>
|
Javascript
<script>
function numberOfTriangles(n)
{
var ans = 2 * (Math.pow(3, n)) - 1;
return ans;
}
var n = 2;
document.write(numberOfTriangles(n));
</script>
|
Time Complexity: O(log n), due to the inbuilt pow() function.
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...