Triangles#2400

Triangles (Editorial)

Consider a triangle whose each side is divided into equal parts. The largest number of identical shapes will be among the smallest equal triangles. Let us count them.

Let us divide the triangle into horizontal strips, numbered from top to bottom.

Continuing this reasoning, we obtain:

Thus, the total number of the smallest equal triangles is the sum of these two quantities:

2 \cdot (n + (n - 1) + (n - 2) + ... + 2 + 1) - n = n^2

Example

For n = 1, 2, and , the number of equal triangles is , and , respectively. For example, when n = 3, the answer is (1 + 2 + 3) + (1 + 2) = 9.

Algorithm implementation

Read the input value .

scanf("%lld",&n);

Compute and print the answer.

res = n * n;
printf("%lld\n",res);

Java implementation

import java.util.*;

class Main 
{
  public static void main(String[] args) 
  {
    Scanner con = new Scanner(System.in);
    long n = con.nextLong();
    long res = n * n;
    System.out.println(res);
    con.close();
  }
}

Python implementation

Read the input value .

n = int(input())

Compute and print the answer.

res = n * n
print(res)