<jupyter_start><jupyter_text>第21讲 正方形与平方  Problem 问题描述 使用`qianglib`库提供的方法，绘制方格坐标纸，其中坐标系的原点`(0,0)`位于绘图区的最左下方，使用的`scale`值为`20`. Use the methods provided in the library `qianglib`, draw a grid coordinate system where the origin `(0, 0)` is at the bottom left of the coordinate system. 1. 以点A(10, 12), B(20, 12)两点为端点，绘制一条线段AB，编程计算这条边的边长。Given the coordiante values of point A(10, 12) and B(20, 12), Draw a line segment with A and B as endpoints. Calculate the length of AB by programming. 2. 绘制一个正方形，使得先前绘制的线段AB为正方形的一条边。Draw a square and make line segment AB be one side of the square. 3. 数一数这个正方形内一共有多少个边长为1的单位正方形，这个数量同时也是这个正方形的面积。 Count how many unit square(side length with 1) does the Square have. The result will be the area of the square as well. 4. 保持点A的横纵坐标值不变，保持点B的纵坐标不便，将点B的横坐标分别改为11至20之间的任何一个数，重新绘制线段AB，计算它的长度，并以它为一条边绘制正方形，对于每画出一次的正方形，计算或数出来这个正方形里有多少边长为1的单位正方形。将结果填入下面的表格。Keep the both coordinate values of A and vertical vlue of B as they are, change only the horizontal value of B to every integer from 11 to 20. redraw the line segment AB and corresponding square. For each square you draw, calculate its side length and how many unit square it contains. Write the result into the following table.| A       |  B      | Side Length | Unit Square(Area) ||:---------:|:---------:|-----|--------------------||(10, 12) |(11, 12) |     |                        ||(10, 12) |(12, 12) |     |                        ||(10, 12) |(13, 12) |     |                        ||(10, 12) |(14, 12) |     |                        ||(10, 12) |(15, 12) |     |                        ||(10, 12) |(16, 12) |     |                        ||(10, 12) |(17, 12) |     |                        ||(10, 12) |(18, 12) |     |                        ||(10, 12) |(19, 12) |     |                        ||(10, 12) |(20, 12) |     |                        | 5. 编写一个方法，该方法接受一个整数参数，计算这个整数参数乘以自己的积，并把这个结果作为方法的返回值。然后调用该方法，将1-10之间的每一个整数作为参数传给该方法，打印每一次方法得到的返回值。Write a method which receives an integer `a` as a parameter and return the value of `a*a`。 For each integer from 1 to 10, call this method with the integer as parameter, print out the return value of the method.  6. 用目前已经学过的编程知识，尝试编写一个方法来计算一个给定面积的正方形它的（近似）边长。调用你写的方法来计算当一个正方形面积分别位1，4，9，15，25，30时，它的边长大概是多少。Use the coding skills learned so far, try to write a method to get the side length of a square with given area. call the method you implemented to get the side length of the square whose areas are the following: 1, 4, 9, 15, 25, 30.     Math Background 数学背景1. 正方形的面积等于边长与自己的乘积2. 两个相同的数相乘又称为这个数的平方或2次方。3. 一个数$a$的平方可以写成$a\times a$或者$a^2$  Prerequisites 预备知识 无  Solution 编程求解<jupyter_code>from turtle import setup, reset, pu, pd, bye, left, right, fd, bk, screensize
from turtle import goto, seth, write, ht, st, home, dot, pen, speed
from qianglib import prepare_paper, draw_grid, mark, lines, line, polygon, text
width, height = 800, 600
setup(width, height, 0, 0)
prepare_paper(width, height, scale=20, min_x=0, min_y=0, max_y=25)<jupyter_output><empty_output><jupyter_text>1.<jupyter_code>A, B = (10, 12), (20, 12)
line(A, B)
mark(A, "A"+str(A))
mark(B, "B"+str(B))
side_length = B[0] - A[0]
print(side_length)<jupyter_output>10<jupyter_text>2.<jupyter_code>C = (B[0], B[1]-side_length)
mark(C, "C"+str(C))
D = (A[0], A[1]-side_length)
mark(D, "D"+str(D))
square_down = [A, B, C, D]  
            # [A, D, C, B]
            # [B, C, D ，A]
polygon(square_down, color="red", line_width=3)       
E = (B[0], B[1]+side_length)
mark(E, "E"+str(E))
F = (A[0], A[1]+side_length)
mark(F, "F"+str(F))
square_up = [A, B, E, F]
polygon(square_up, color="green", line_width=3)<jupyter_output><empty_output><jupyter_text>3.<jupyter_code>num_square_unit = side_length * side_length
print("number of square unit: {}".format(num_square_unit))<jupyter_output>number of square unit: 100<jupyter_text>4.| A       |  B      | Side Length | Unit Square(Area) ||:---------:|:---------:|----:|--------------------:||(10, 12) |(11, 12) |  1   |      1                   ||(10, 12) |(12, 12) |  2   |      4                   ||(10, 12) |(13, 12) |  3   |      9                   ||(10, 12) |(14, 12) |  4   |       16                 ||(10, 12) |(15, 12) |  5   |      25                  ||(10, 12) |(16, 12) |  6   |      36                  ||(10, 12) |(17, 12) |  7   |      49                  ||(10, 12) |(18, 12) |  8   |      64                  ||(10, 12) |(19, 12) |  9   |      81                  ||(10, 12) |(20, 12) |  10   |     100                 | 5.<jupyter_code>def square_area(side_length):
    return side_length**2
for side_length in range(1, 11):
    area = square_area(side_length)
    print("area for side length {:2} is {:3}".format(side_length, area))<jupyter_output>area for side length  1 is   1
area for side length  2 is   4
area for side length  3 is   9
area for side length  4 is  16
area for side length  5 is  25
area for side length  6 is  36
area for side length  7 is  49
area for side length  8 is  64
area for side length  9 is  81
area for side length 10 is 100<jupyter_text>6.<jupyter_code>def side_length_of_square(area):
    side_length = 0
    dx = 0.001
    while square_area(side_length) < area:
        side_length += dx
    
    return side_length
for area in [1, 4, 9, 15, 25, 30]:
    side_length = side_length_of_square(area)
    print("side length for square area {:2} is close to {:<4.3}".format(
        area, side_length))<jupyter_output>side length for square area  1 is close to 1.0 
side length for square area  4 is close to 2.0 
side length for square area  9 is close to 3.0 
side length for square area 15 is close to 3.87
side length for square area 25 is close to 5.0 
side length for square area 30 is close to 5.48<jupyter_text>Summary 知识点小结 1. 乘方运算符`**`2. 复习tuple3. 复习qianglib的一些方法4. 复习方法的返回值  计算机小知识 暂缺  Assignments 作业 1. 在本讲中，我们用一个包含4个元素的`list`变量来描述一个四边平行于坐标轴的正方形，其中这个`list`变量里的每一个元素都是长度为类似`(x, y)`形式的`tuple`类型的数据，它表示正方形四个顶点中的一个顶点的坐标。事实上，这样的边平行于坐标轴的正方形还可以用这个正方形中心点的坐标外加一个表示长度的变量来描述。   例如：`[(10, 12), (20, 12), (20, 2), (10, 2)]`表示的正方形可以用`(15, 7)`和`10`来表示。其中`(15, 7)`是这个正方形中心点的坐标，`10`则代表正方形的边长。      1). 请你声明并实现一个方法，该方法通过调用`qianglib`库的一些绘图方法绘制出一个边平行于坐标轴的正方形。同时，这个方法接受两个参数，第一个参数为要绘制的正方形的中心点的坐标，数据为`tuple`类型，第二个参数为正方形的边长，数据为整数`integer`或小数类型。该方法仅完成绘制，没有返回值。      2). 请调用你创建的绘制正方形的方法，绘制出中心点在`(15, 15)`边长分别为`2，4，6，10，12`这5个边平行于坐标轴的正方形。      3). 比较并用文字说出两种描述正方形的方式各自的优点和缺点。        In this lecture, we use a `list` variable containing 4 elements to describe a square with four sides parallel to coordinate axises, where each element in the `list` variable has a `tuple` data structure of `(x, y)`, representing the coordinates of one of the four vertices of the square. In fact, such a square can also be described by the coordinates of the center point of the square plus a variable representing the length.   For example: consider a square defined by a list like: `[(10, 12), (20, 12), (20, 2), (10, 2)]`， it can also be represented by `(15, 7)` and `10`, where `(15, 7)` is the coordinates of the center point of the square, and `10` is the side length of the square.      1). Please declare and implement a method that draws a square with sides parallel to the coordinate axis by calling some drawing methods in the `qianglib` library. More specifically, this method should accept two parameters with the first parameter indicating the coordinates of the center point (`tuple` of `(x, y)`) of the square to be drawn and the second parameter being equal to the side length (`integer` or a `decimal`) of the square. This method only completes the drawing and has no return value.      2). Please call the method you created, draw 5 squares (side parallel to axis) whose center point are the same as `(15, 15)` and side lengths are `2, 4, 6, 10, 12`, respectively.      3). Compare and use your own words to describe the advantages and disadvantages of the above two ways of describing squares.    2. 整数3，4和5这三个数字之间有一种奇妙的关系：$$3^2 + 4^2 = 5^2$$ 这种关系可以描述为：**3个整数（可以不是相邻的）中，两个较小整数的平方的和恰好等于最大的那个整数的平方**。事实上满足这样关系的整数有无数组。例如5，12和13也是这样($5^2 + 12^2 = 13^2$)。请你编写一段程序找出最大的数不超过100时的所有满足这种关系的3个整数组合。   There is a wonderful relationship between the three numbers of integers 3, 4 and 5:$$3^2 + 4^2 = 5^2$$ This relationship can be described as: **Among 3 integers (which may not be adjacent), the sum of the squares of the two smaller integers is exactly equal to the square of the largest integer**. In fact, there are infinite number of such set. Here is another case: 5, 12, and 13, where $5^2 + 12^2 = 13^2$. Please write a program to find all combinations of 3 integers that satisfy this relationship when the largest number does not exceed 100. 3. (可选做) **从1开始的连续奇数的和总能表示成这些连续奇数的个数的平方**。数学表达形式为：$$1 + 3 + 5 + \cdots + (2n-1) = n^2$$ 总是成立的，对于任何$n\gt1$。        例如，$$1+3=2^2$$ $$1+3+5=3^2$$ $$1+3+5+7=4^2$$ 等等。        请你编写一个方法来验证这个规律，该方法只接受整数`n`作为参数，在这个方法内部分别计算上面等式左右两边表达式的值，并比较他们是否相等，如果相等方法返回值为`True`，否则为`False`。随后使用循环控制语句在循环代码内部调用你编写的方法对 $n\le100$ 内的所有情况进行验证，打印输出验证结果。            (optional) **The sum of consecutive odd integers starting from 1 can always be expressed as the square of the number of these consecutive odd integers**. The mathematical expression for this law is: $$1 + 3 + 5 + \cdots + (2n-1) = n^2$$ always holds, for any $n\gt 1$.         For example, $$1+3=2^2$$ $$1+3+5=3^2$$ $$1+3+5+7=4^2$$ and so on.         Please write a method to verify this law. The method only accepts an integer `n` as its parameter. Inside the method, the values of the left and right expressions of the above equation are calculated separately and compared whether they are equal. If they are equal, the method returns `True`; otherwise, returns `False`. Then call the method inside a loop control statement to verify all the scenarios where $n\le100$. Print out the verification result.<jupyter_code>def verify_law(n):
    is_law_correct = None
    return is_law_correct
for i in range(1, 101):
    #TODO call your method and print out the result.
    result = verify_law(i)
    if result is True:
        print("for n = {:3}, the law holds".format(i))
    else:
        print("for n = {:3}, the law doesn't hold".format(i))
    pass<jupyter_output>for n =   1, the law doesn't hold
for n =   2, the law doesn't hold
for n =   3, the law doesn't hold
for n =   4, the law doesn't hold
for n =   5, the law doesn't hold
for n =   6, the law doesn't hold
for n =   7, the law doesn't hold
for n =   8, the law doesn't hold
for n =   9, the law doesn't hold
for n =  10, the law doesn't hold
for n =  11, the law doesn't hold
for n =  12, the law doesn't hold
for n =  13, the law doesn't hold
for n =  14, the law doesn't hold
for n =  15, the law doesn't hold
for n =  16, the law doesn't hold
for n =  17, the law doesn't hold
for n =  18, the law doesn't hold
for n =  19, the law doesn't hold
for n =  20, the law doesn't hold
for n =  21, the law doesn't hold
for n =  22, the law doesn't hold
for n =  23, the law doesn't hold
for n =  24, the law doesn't hold
for n =  25, the law doesn't hold
for n =  26, the law doesn't hold
for n =  27, the law doesn't hold
for n =  28, the law doesn't hold
for n =  29, the law doesn't hold
for n =  30, t[...]
{'fraction_non_alphanumeric': 0.11951833213515456, 'fraction_numerical': 0.056524083393242275, 'mean_word_length': 3.2803846153846155, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '7', 'dir': 'jupyter-structured-clean-dedup', 'id': '33451', 'license': 'MIT', 'n_tokens_mistral': 4863, 'n_tokens_neox': 4672, 'n_words': 1459, 'path': 'docs/2021/100Beginner/content/021_square.ipynb', 'repo_name': 'qqiang00/ElementaryMathPython', 'size': '11128'}