[Python] Heron's formula functionalization and calculation of the maximum area

Define the three sides of a triangle as x, y, z, and let x + y + z = L (constant). Answer what the triangle looks like that maximizes the area of the triangle when L is constant.

Was issued by the school.
Normally it would be necessary to solve this mathematically, but the moment I saw this problem.
"Isn't it easier to understand if it is proved by a program?"
I thought so.
So this time I thought about creating Heron's formula function in Python and proving it with a while loop.

Heron's formula

area= 
\sqrt{s(s-x)(s-y)(s-z)}\\
s=
\frac{x+y+z}{2}

First, let's make Heron's formula a function.

Heron's formula functionalization

def heron(a, b, c):
    x, y, z = float(a), float(b), float(c)
    s = (x + y + z) / 2

    if x+y>z and y+z>x and z+x>y:
        return (s * (s - x) * (s - y) * (s - z)) ** 0.5 #Heron's formula
    else:
        return 0

if __name__ == '__main__':
    x = input('Value of x: ')
    y = input('Value of y: ')
    z = input('value of z: ')
    print(heron(x, y, z))

You have now functionalized Heron's formula.
In the if statement, there is a conditional branch depending on whether the triangle holds or does not hold.
If it holds, Heron's formula calculation result is returned, and if it does not hold, 0 is returned.
You can put the input statement in the function, but I didn't put it because I felt that it would be better to assign it as an argument.

Calculation of maximum area

~~ What is the shape of the triangle that maximizes the area when the total L of the lengths of the three sides is constant?
When thinking about that, the first thing that humans think about is brute force </ b>.
However, it is not impossible for humans to perform this enormous amount of calculation, but it is extremely difficult.
Then what would you do? Just let the computer do it. It leads to the idea. ~~

Source code

import random

def heron(a, b, c): #Heron's formula
    x, y, z = float(a), float(b), float(c)
    s = (x + y + z) / 2

    if x+y>z and y+z>x and z+x>y:
        return (s * (s - x) * (s - y) * (s - z)) ** 0.5
    else:
        return 0

if __name__ == '__main__':
    s = 0
    i = 0
    while i < 20000000:
        x = random.randint(1, 120) #Can be changed
        y = random.randint(1, 120) #Can be changed
        z = random.randint(1, 120) #Can be changed

        if (x+y+z) == 120.0: #The total of sides is 120
            if heron(x, y, z) > s:
                s = heron(x, y, z)
                print('x={}, y={}, z={}With area s={}'.format(x, y, z, s))
            else:
                continue
            i += 1
        else:
            i += 1

Here, in order to use random numbers, we will import the library function random.
This time, the total length of the three sides is set to 120. I'll explain why it's 120 later.
___ randint(a, b) randint () returns a random number of integers greater than or equal to a and less than or equal to b.

If you want to use real random numbers, use uniform (a, b) </ b>.


Since it returns 3 types of random numbers of integers ~~ 1 ~ 120, it is necessary to loop 1728000 times of 120 cubed by simple calculation.
Just in case, this time it loops 2 million times.
It is output every time the maximum value is updated, and the last output is the maximum value of the area of this triangle. ~~

result

First of all, the equilateral triangle </ b> has the maximum value.
The reason why the total length of the three sides is 120 is because I expected the result of x = 40, y = 40, z = 40 </ b>.
You should get the same result when you actually run the source code.
Even if you change the value, if "(total length of 3 sides) / 3" holds, then x = y = z should be satisfied.

Recommended Posts