- 論壇徽章:
- 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:
畢達(dá)哥拉斯數(shù)(勾股數(shù))是三個自然數(shù),他們有關(guān)系:a*a + b*b = c*c
例如:3*3 + 4*4 = 25 = 5*5
在1000內(nèi)有一組畢達(dá)哥拉斯數(shù),并且滿足:a + b + c = 1000。
試找出這三個數(shù)的乘積。
代碼:
- 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))
- }
復(fù)制代碼 |
|