简体   繁体   English

三角边求和程序

[英]Triangle Side Sum Program

So in math class we had this problem: 所以在数学课上我们遇到了这个问题:

You are given a triangle with sides 4 units long. 您会得到一个三角形,其边长为4个单位。 Inside the triangle, there are smaller triangles made from the midsegments of the larger triangles. 在三角形内部,有一些由较大三角形的中段组成的较小三角形。 There are 8 total inscribed triangles. 总共有8个内切三角形。 What are the total side lengths? 总边长是多少?

So, I decided to see if I could write a python program that could solve this for all number of triangles and all side lengths. 因此,我决定看看是否可以编写一个python程序来解决所有三角形和所有边长的问题。

Here it is: 这里是:

tri = int(raw_input("How many triangles are there?\n>"))
size = int(raw_input("What is the side length of the biggest triangle?\n>"))
tot = 0
for i in range(1, tri):
    tot += 3*(float(size/i))
print str(tot)

I tried it with tri = 8 and size = 4, as in the original problem, and got 24.0 . 我尝试使用tri = 8且size = 4进行尝试,就像原始问题一样,得到24.0 As wolfram alpha shows us, that is wrong . 正如Wolfram alpha向我们展示的那样,这是错误的

To try to clarify what happened in each step, I edited it to: 为了尝试澄清每个步骤中发生的情况,我将其编辑为:

tri = int(raw_input("How many triangles are there?\n>"))
size = int(raw_input("What is the side length of the biggest triangle?\n>"))
tot = 0
for i in range(1, tri):
    tot += 3*(float(size/i+0.0000001))
    print str(tot)
print str(tot)

The output was: 输出为:

12.0000003
18.0000006
21.0000009
24.0000012
24.0000015
24.0000018
24.0000021
24.0000021

Anyone think they know where I went wrong? 有人认为他们知道我哪里出问题了吗?

You are not looping tri times: 你是不是循环tri次数:

for i in range(1, tri):

The end value is not included, so you are counting 1 through to 7 here, not 8 . 最终值包括在内,因此此处您要计算的是17 ,而不是8 Remove the 1 and add 1 in the loop: 删除1并在循环中添加1

for i in range(tri):
    tot += 3.0 * size / (i + 1)

You just need one float value in the calculation, turning 3 into 3.0 does this for us. 您只需要一个浮点值即可,将3转换为3.0为我们完成此操作。 Unfortunately, in Python 2, / will use integer or floor division when both values are integers, a problem remedied in Python 3: 不幸的是,在Python 2中,当两个值都是整数时, /将使用整数底数除法,这在Python 3中得以解决:

>>> 1 / 2
0
>>> 1.0 / 2
0.5

Using 3.0 in the multiplication will make sure the left-hand operand of the division is a float. 在乘法中使用3.0将确保除法的左侧操作数是浮点数。

You can also use from __future__ import division at the top of your script to switch to proper true division for the / operator where it always returns a float result. 您还可以使用脚本顶部的from __future__ import division/运算符切换到正确的真实分隔,从而始终返回浮点结果。

Next, you are not using proper exponents in your formula; 其次,您在公式中使用的指数不正确; it is not size / (i + 1) ; 不是size / (i + 1) ; it is size / (2 ** i) ; 它是size / (2 ** i) ; you need to divide by the powers of 2 here; 您需要在这里除以2的幂; not divide by 1, 2, 3, 4, but 1, 2, 4, 8, etc: 不除以1,2,3,4,而是1,2,4,8,等等:

tot = 0
for i in range(tri):
    tot += 3.0 * size / 2 ** i

Now you get a result that matches Wolfram Alpha: 现在,您得到与Wolfram Alpha相匹配的结果:

>>> tri, size = 8, 4
>>> tot = 0
>>> for i in range(tri):
...     tot += 3.0 * size / 2 ** i
... 
>>> tot
23.90625

Change: 更改:

float(size/i)

to: 至:

float(size)/i

Due to the operator precedence in Python ; 由于Python中运算符优先级 ;

size/i+0.0000001

uses integer division 使用整数除法

size/i

then adds 0.0000001. 然后加上0.0000001。 Effectively, it's 实际上,这是

(size/i)+0.0000001

Instead, use: 而是使用:

float(size)/i

to force floating point division. 强制进行浮点除法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM