演算法實現/數學/勾股定理
外觀

在數學中,勾股定理或畢達哥拉斯定理是歐幾里得幾何中關於直角三角形(直角三角形)的三邊之間的關係。就面積而言,它指出
在任何直角三角形中,斜邊(與直角相對的邊)為邊的正方形的面積等於兩條直角邊(兩條在直角處相交的邊)為邊的正方形的面積之和。
該定理可以用一個關於邊長 a、b 和 c 的等式來寫,通常稱為勾股方程:[1]
其中 c 表示斜邊的長度,a 和 b 表示另外兩邊的長度。
注意:對於大多數語言,內建的 hypot 函式執行相同的計算。這僅供教育目的。
(define (hypotenuse a b) (sqrt (+ (expt a 2) (expt b 2))))
;; Equivalent, but does not check the number of arguments
(define (hypotenuse . xs) (sqrt (fold (lambda (x acc) (+ (expt x 2) acc)) 0 xs)))
Function Hypotenuse(sideA as Double, sideB as Double) as Double
Hypotenuse = sqr(sideA^2 + sideB^2)
End Function
public static double hypotenuse(double sideA, double sideB) {
double hypotenuse = Math.sqrt((sideA*sideA) + (sideB*sideB));
return hypotenuse;
}
def hypotenuse(a,b):
return ( a**2 + b**2 )**.5
pythag a b = sqrt $ a^2 + b^2
const hypotenuse = (a,b) => Math.sqrt(a**2 + b**2)
- ↑ Judith D. Sally, Paul Sally (2007). "第 3 章:勾股數". 從根源到研究:數學問題的縱向發展. 美國數學會書店. p. 63. ISBN 0821844032.