Share a pen question [How many parts can a plane be divided into at most n straight lines]

Source: Internet
Author: User

Copy codeThe Code is as follows:
<Html>
Question: <br/>
A maximum of n straight lines can divide a plane into several parts <br/>
Number of lines: <input type = "text" id = "line"/> <br/>
Inner point: <label id = "innerPoint"> </label> <br/>
Number of partitions: <label id = "part" style = "background: yellow;"> </label> <br/>
<Input type = "button" onclick = "calculate ()" value = "computing"/>
</Html>
<Script type = "text/javascript">
Function calculate (line)
{
Var line = document. getElementById ('line'). value;
If (line = "")
{
Line = 0;
Document. getElementById ('line'). value = line;
}
Var line = parseInt (line );
Var innerPoint = line * (line-1)/2;
Var part = (Math. pow (line, 2) + line)/2 + 1; // line + innerPoint + 1 equals (the square of the number of lines + the number of lines)/2 + 1

Document. getElementById ('innerpoint'). innerText = innerPoint;
Document. getElementById ('part'). innerText = part;
}
</Script>

Rule:

① Most divided parts: number of lines + inner points + 1

② Inner point = (number of lines-1) Inner point + (number of lines-1), the newly added line can have an intersection with other lines

③ Use recursion to find the number of inner points, and then substitute it into ① for Calculation

The above is a normal mathematical thinking. Next we will talk about the field testing knowledge that I use, that is, the stuff of my code.

I listed 1 ~ Some available parameters for five straight lines:

Number of intersection diplomatic points in a straight line

1 0 2 2

2 1 4 4

3 3 6 7

4 6 8 11

5 10 10 16

It is found that diplomatic points are meaningless, and they are two times the number of straight lines.

The part number = the number of straight lines + the number of inner points + 1

The number of intersections in the number of adjacent straight lines forms an arithmetic difference series. The tolerances of this arithmetic difference series are 1, 1-0 =-1 =-3 =-6 = 4, 1 + 0 = 1 + 1 = 3 + 3 = 6... however, we still use recursion to obtain the number of corresponding inner intersections, so we look at the Law vertically, 2*1 = 2 3*2 = 6 4*3 = 12... exactly twice the number of inner points

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.