Python: Create maximum number of regions obtained by drawing n given straight lines
Python Basic - 1: Exercise-54 with Solution
if you draw a straight line on a plane, the plane is divided into two regions. For example, if you pull two straight lines in parallel, you get three areas, and if you draw vertically one to the other you get 4 areas.
Write a Python program to create maximum number of regions obtained by drawing n given straight lines.
(1 ≤ n ≤ 10,000)
while True: print("Input number of straight lines (o to exit): ") n=int(input()) if n<=0: break print("Number of regions:") print((n*n+n+2)//2)
Input number of straight lines (o to exit): 5 Number of regions: 16
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python program that accept a even number (>=4, Goldbach number) from the user and create a combinations that express the given number as a sum of two prime numbers. Print the number of combinations.
Next: Write a Python program to test AB and CD are orthogonal or not.
What is the difficulty level of this exercise?
- New Content published on w3resource :
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework