Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Check if it is possible to create a polygon with a given angle in Python
Suppose we have an angle a. We need to check whether we can create a regular polygon where all interior angles are equal to a.
For example, if the input angle is 120°, the output will be True because a hexagon has all interior angles equal to 120°.
Mathematical Formula
The interior angle of a regular polygon is calculated using the formula ?
If n is a positive integer greater than 2, then we can form a regular polygon with the given angle.
Algorithm
To solve this problem, we follow these steps ?
- Calculate sides = 360 / (180 - a)
- Check if sides is a positive integer and greater than 2
- Return True if valid, False otherwise
Example
Let us see the implementation to get better understanding ?
def solve(a):
sides = 360 / (180 - a)
if sides == int(sides) and sides > 2:
return True
return False
# Test with angle 120 degrees
a = 120
result = solve(a)
print(f"Can create polygon with {a}° angles: {result}")
# Test with more examples
test_angles = [60, 90, 108, 135, 150]
for angle in test_angles:
result = solve(angle)
if result:
n = int(360 / (180 - angle))
print(f"{angle}° - Yes (forms {n}-sided polygon)")
else:
print(f"{angle}° - No")
Can create polygon with 120° angles: True 60° - Yes (forms 3-sided polygon) 90° - Yes (forms 4-sided polygon) 108° - Yes (forms 5-sided polygon) 135° - Yes (forms 8-sided polygon) 150° - Yes (forms 12-sided polygon)
How It Works
The formula n = 360 / (180 - a) comes from rearranging the interior angle formula. For a valid polygon ?
- The result must be a positive integer
- Must be greater than 2 (minimum 3 sides for a polygon)
- The angle must be less than 180° (interior angle constraint)
Edge Cases
def solve_with_validation(a):
if a <= 0 or a >= 180:
return False
sides = 360 / (180 - a)
return sides == int(sides) and sides > 2
# Test edge cases
test_cases = [0, 180, 179, 1, 60, 100]
for angle in test_cases:
result = solve_with_validation(angle)
print(f"Angle {angle}°: {result}")
Angle 0°: False Angle 180°: False Angle 179°: False Angle 1°: False Angle 60°: True Angle 100°: False
Conclusion
Use the formula n = 360 / (180 - a) to check if a regular polygon can be formed with a given interior angle. The result must be a positive integer greater than 2 for a valid polygon.
