Exercise 3: Integer Right Triangles

Posted ladyrui

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Exercise 3: Integer Right Triangles相关的知识,希望对你有一定的参考价值。

Given a perimeter of 60, we can find two right triangles with integral length sides: [(10, 24, 26), (15, 20, 25)]. Complete the following function, which takes an integer p and returns the number of unique integer right triangles with perimeter p.

Note that your solution should take care to limit the number of triangles it tests --- your function must complete in under 3 seconds for all values of p used in the test cells below to earn credit.

def integer_right_triangles(p):
    i=0
    for x in range(p//3,p//2):
        for y in range((p-x)//2,(p-x)):
            if x<y:
                break
            z = p-x-y
            if y<z:
                continue
            if x*x == y*y + z*z:
                i+=1
    return i

# (2 points)
import unittest
tc = unittest.TestCase()
tc.assertEqual(integer_right_triangles(60), 2)
tc.assertEqual(integer_right_triangles(100), 0)
tc.assertEqual(integer_right_triangles(180), 3)

以上是关于Exercise 3: Integer Right Triangles的主要内容,如果未能解决你的问题,请参考以下文章

Project Euler 75: Singular integer right triangles

3.1 EXERCISE

Exercise 3

C++ ssd5 16 optional exercise 3

MYSQL数据库SQL语句练习实验

数据流的中位数