- 论坛徽章:
- 4
|
本帖最后由 icymirror 于 2015-09-28 10:23 编辑
Problem 009:
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which, a^2 + b^2 = c^2
For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product a,b,c.
问题9:
毕达哥拉斯数(勾股数)是三个自然数,他们有关系:a*a + b*b = c*c
例如:3*3 + 4*4 = 25 = 5*5
在1000内有一组毕达哥拉斯数,并且满足:a + b + c = 1000。
试找出这三个数的乘积。
代码:
- package main
- import (
- "fmt"
- )
- func Problem009(scope int) int {
- var result int
- for edgec := scope / 3 + 1; edgec < scope / 2; edgec++ {
- for edgeb := 1; edgeb < edgec; edgeb++ {
- edgea := scope - edgec - edgeb
- if (edgea < edgeb && edgeb < edgec) {
- if (edgea * edgea + edgeb * edgeb == edgec * edgec) {
- result =edgea * edgeb * edgec
- }
- }
- }
- }
-
- return result
- }
- func main() {
- fmt.Println("Problem 009 result: ", Problem009(1000))
- }
复制代码 |
|