Number of occurrences of a given angle formed using 3 vertices of a n-sided regular polygon
Given an n-sided regular polygon and an angle ?, the task is to find number of occurrences of angle ( Ai, Aj, Ak ) = ? ( i < j < k) in a regular n-gon (regular polygon with n vertices) with vertices marked as A1, A2, …, An.
Examples:
Input: n = 4, ang = 90
Output: 4
Input: n = 6, ang = 50
Output: 0
Approach:
- First we check whether such an angle can exist or not.
- Consider the vertices to be x, y, and z and the angle to find be ? xyz.
- The number of edges between x and y be a and the number of edges between y and z be b.
- Then ? xyz = 180 – (180*(a+b)) / n.
- Thus ? xyz * n (mod 180) = 0.
- Next we need to find the count of such angles.
- As the polygon is regular we just need to calculate the count of such an angle at one vertex and can directly multiply our result by n (the number of vertices).
- At each vertex the angle can be found at n-1-freq times where freq = (n*ang)/180 and depicts the number of edges remaining after creating the required angle i.e. the number of edges between z and x.
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
int solve( int ang, int n)
{
if ((ang * n) > (180 * (n - 2))) {
return 0;
}
else if ((ang * n) % 180 != 0) {
return 0;
}
int ans = 1;
int freq = (ang * n) / 180;
ans = ans * (n - 1 - freq);
ans = ans * n;
return ans;
}
int main()
{
int ang = 90, n = 4;
cout << solve(ang, n);
return 0;
}
|
Java
class GFG
{
static int solve( int ang, int n)
{
if ((ang * n) > ( 180 * (n - 2 )))
{
return 0 ;
}
else if ((ang * n) % 180 != 0 )
{
return 0 ;
}
int ans = 1 ;
int freq = (ang * n) / 180 ;
ans = ans * (n - 1 - freq);
ans = ans * n;
return ans;
}
public static void main (String[] args)
{
int ang = 90 , n = 4 ;
System.out.println(solve(ang, n));
}
}
|
Python3
def solve(ang, n):
if ((ang * n) > ( 180 * (n - 2 ))):
return 0
elif ((ang * n) % 180 ! = 0 ):
return 0
ans = 1
freq = (ang * n) / / 180
ans = ans * (n - 1 - freq)
ans = ans * n
return ans
ang = 90
n = 4
print (solve(ang, n))
|
C#
using System;
class GFG
{
static int solve( int ang, int n)
{
if ((ang * n) > (180 * (n - 2)))
{
return 0;
}
else if ((ang * n) % 180 != 0)
{
return 0;
}
int ans = 1;
int freq = (ang * n) / 180;
ans = ans * (n - 1 - freq);
ans = ans * n;
return ans;
}
public static void Main (String[] args)
{
int ang = 90, n = 4;
Console.WriteLine(solve(ang, n));
}
}
|
Javascript
<script>
function solve(ang , n)
{
if ((ang * n) > (180 * (n - 2)))
{
return 0;
}
else if ((ang * n) % 180 != 0)
{
return 0;
}
var ans = 1;
var freq = (ang * n) / 180;
ans = ans * (n - 1 - freq);
ans = ans * n;
return ans;
}
var ang = 90, n = 4;
document.write(solve(ang, n));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Last Updated :
13 Mar, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...