Common Algorithms—Analytic Algorithms

Analytical algorithm, that is, analytical algorithm (analysis algorithm), refers to the use of analytical methods to find out the mathematical expressions that represent the relationship between the preconditions and results of the problem, and solve the problem through the calculation of the expression. The problem-solving process is shown in Fig. 1. 

Figure Analysis algorithm flow chart

Example 1 Use an iron wire with a length of l cm to make a rectangular frame with an area of ​​s square centimeters. It is required to calculate the height h and width w of the rectangular frame.

 

Fig . 2  Relation diagram of the parameters of the rectangular frame made of iron wire

Analysis: Let the height of the rectangular frame be h and the width be w , then according to the geometric relationship ( refer to Figure 2) :

 So h=h 1 , then w=h 2 ; or h=h 2 , then w=h 1 .

Assume d=l^{2}-16sthat there is no solution when d<0 , there are two identical solutions when d=0 , and there are two different solutions when d>0 . Program flow shown in Figure 3 .

Figure Flowchart of program design for making rectangular frame with iron wire

The complete procedure is as follows:

##############################################
# 设计 Zhang Ruilin    创建 2022-10-30 14:21 #
# 已知矩形周长(l)和面积(s),求其高(h)和宽(w) #
##############################################
l = float(input('请输入铁丝的长度'))
s = float(input('请输入矩形框面积'))
d = l * l - 16 * s
if d < 0:
    print(f'无实数解(长度为{l}厘米的铁丝制作'\
          f'不了面积为{s}平方厘米的矩形框)')
elif d == 0:
    h = w = l / 4
    print(f'长度为{l}厘米的铁丝制作面积为{s}平方厘米的'\
          f'矩形框,高为{h}厘米宽为{w}厘米')
else:
    h = (l - d ** 0.5) / 4
    w = (l + d ** 0.5) / 4
    print(f'长度为{l}厘米的铁丝制作面积为{s}平方厘米的'\
          f'矩形框,高为{h:.2f}厘米宽为{w:.2f}厘米')

The execution results are as follows:

 Example 2 In ancient times , a wise man sent a kind of chess piece to the king , called " chess " . … . Chess players move the pieces according to the rules, and the two sides face each other, just like fighting a war, to see who can defeat the other. The king can't play enough, this chess is really too much fun. So, the king called for the smart man who invented chess and said happily: " This game is really fun to play. I will reward you very much. I can satisfy your requirements for whatever you want. "

The wise man said: "I just want some rice grains." "Rice grains, what are a few grains of rice?" The king said: "How much do you want?" Put two pieces on the two-square chessboard, four pieces on the third box... put it down like this, each square is twice as big as the previous one, and I am satisfied with the eighty-eighty-sixty-four-square chessboard. .” (see Figure 4).

 Figure is a schematic diagram of the number of rice grains that should be placed in each grid on a 64 - grid chessboard

The king asked the minister in charge of grain to count. He put one, two, four, eight, sixteen grains... only twenty grids, and one bag of rice was finished. Bags, sixteen bags...not even halfway on the board. When all the food in the granary had been put out, he immediately went to report to the king: "It's terrible, it's terrible!" "What's the fuss?" the king asked. Please calculate how much food is needed to fill the 64-grid chessboard (1 kg of rice is about 50,000 grains), so that the minister in charge of food shouted "It's terrible, it's terrible!".

Solution 1: As shown in Figure 4, total s=2^{0}+2^{1}+2^{2}+2^{3}+...+2^{62}+2^{63}. The calculation procedure is as follows:

#############################################
# 设计 Zhang Ruilin   创建 2022-10-31 09:01 #
# 棋盘上的米粒。64 格棋盘,第 1 格放 1 粒, #
# 第 2 格放 2 粒,程序后一格是前一格的一倍,#
# 求放满64格需要多少大米(1 千克约 50000 粒) #
#############################################
s = 0
for i in range(64):
    s += 2**i
print(f'{s}粒,约{s/50000:.2f}千克\n约{s/5e7:.2f}吨)')

The result of the operation is:

18446744073709551615 grains, about 368934881474191.06 kg

About 368934881474.19 tons.

Solution 2: The formula s=2^{0}+2^{1}+2^{2}+2^{3}+...+2^{62}+2^{63}is obviously a sum of geometric series and a sum of geometric series, let

 

 The calculation procedure is as follows:

#############################################
# 设计 Zhang Ruilin   创建 2022-10-31 12:50 #
# 棋盘上的米粒。64 格棋盘,第 1 格放 1 粒, #
# 第 2 格放 2 粒,程序后一格是前一格的一倍,#
# 求放满64格需要多少大米(1 千克约 50000 粒) #
#############################################
a1 = 1
q = 2
n = 64
s = 2 ** 64 - 1
print(f'{s}粒,约{s/50000:.2f}千克\n约{s/5e7:.2f}吨)')

The result of the operation is:

18446744073709551615 grains, about 368934881474191.06 kg

About 368934881474.19 tons

China's rice production in 2021 is about 149 million tons

That is to say, according to China's 2021 production, the rice produced in 2476.07 years can meet the requirements.

Guess you like

Origin blog.csdn.net/hz_zhangrl/article/details/128710016