Chinaunix首页 | 论坛 | 博客
  • 博客访问: 86660
  • 博文数量: 47
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 625
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-11 12:11
文章分类

全部博文(47)

文章存档

2008年(47)

我的朋友

分类:

2008-11-12 12:31:55

Problem 9

25 January 2002

A Pythagorean triplet is a set of three natural numbers, a b c, for which,

a2 + b2 = c2

For example, 32 + 42 = 9 + 16 = 25 = 52.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

a2 + b2 = c2知道,a, b, c可以构成三角形,我们令a<=b<=c,则a的取值范围为[1,333]

又有a2 + b2 = c2,则a2=(b+c)(c-b), a + b + c = 1000, 得到a2=(1000-a)*(c-b)

这样我们先可以得到符合条件的a, 在进一步得到b, c

def fun9():
  a = -1
  b = -1
  c = -1
 
  for a in range(1, 334):
    bc = 1000-a
    s = a**2
    if s%bc == 0:
      cb = s/bc
      if (bc+cb)%2 == 0:
        c = (bc+cb)/2
        b = bc - c
        if (c>0) and (b>0):
          print a, b, c, a*b*c
          return a*b*c

answer = 31875000

阅读(324) | 评论(0) | 转发(0) |
0

上一篇:Problem 8

下一篇:Problem 10

给主人留下些什么吧!~~