Counting Isosceles Acute Triangles in a Regular n‑gon – Analysis and Java Solution
This article explains how to count the number of isosceles acute‑angled triangles whose vertices lie on the vertices of a regular n‑gon, discusses separate cases for even and odd n, handles duplicate equilateral triangles, and provides a correct Java implementation that avoids integer overflow.
The problem asks for the number of isosceles acute‑angled triangles that can be formed using vertices of a regular n‑gon, where two triangles are considered different if any vertex differs. The input range is 3 ≤ n ≤ 10^7.
Analysis – even n : By arranging the polygon on a circle, each vertex can form a certain number of acute‑angled isosceles triangles. The count reduces to the number of horizontal lines below the right angle, which is ⌊(n‑2)/4⌋ per vertex. Multiplying by n gives total = n * ((n-2)/4).
Analysis – odd n : For odd n the same reasoning leads to ⌈(n‑1)/2⌉ lines that can serve as bases, yielding total = n * (((n-1)/2 + 1)/2).
Duplicate triangles : When n is a multiple of 3, some counted triangles are equilateral and thus counted three times. To remove duplicates we subtract (n/3) * 2 from the total.
Final formula (before duplicate correction):
if (n % 2 == 0) {
total = n * ((n - 2) / 4);
} else {
total = n * (((n - 1) / 2 + 1) / 2);
}
if (n % 3 == 0) {
total -= (n / 3) * 2;
}Implementation pitfalls : Using int for intermediate calculations overflows for n up to 10^7, so long must be used and the cast applied before the multiplication. Also, integer division truncates, so explicit casting to long (or double when needed) is required.
Correct Java code:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long total;
if (n % 2 == 0) {
total = (long) n * ((n - 2) / 4);
} else {
total = (long) n * (((n - 1) / 2 + 1) / 2);
}
if (n % 3 == 0) {
total -= (n / 3) * 2;
}
System.out.println(total);
}The article concludes that such combinatorial geometry problems appear occasionally in coding interviews, and accumulating experience with them is valuable.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
IT Services Circle
Delivering cutting-edge internet insights and practical learning resources. We're a passionate and principled IT media platform.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
