# Euler Solution 9

### From ProgSoc Wiki

## Contents |

# Solutions for Problem 9

Find the only Pythagorean triplet, {a, b, c}, for which a + b + c = 1000, then find the product of a, b and c

## Ruby by tomchristmas

Runtime: 1432ms (eek!)

1.upto(500){|c| c.upto(499){|b| a = 1000 - b - c puts "#{a * b * c} (#{a}^2 + #{b}^2 = #{c}^2)" if c **2 == a ** 2 + b ** 2 } }

## Python by Althalus

Runtime: 43 ms

from math import sqrt import time start_time = time.time() #These ranges don't make much sense, but it does satisfy the requirement a < b brange = range(1,499) arange = range(1,488) for a in arange: for b in brange: if sqrt(a**2 + b**2) + a + b == 1000: print(a*b*sqrt(a**2 + b**2)) run_time = time.time() - start_time print (run_time) exit()

## Caml by SanguineV

Runtime: 41.991 ms

(* Given a limit, an a and a list of b's, check if any of them satisfy: * a^2 + b^2 = c^2 && a + b + c = lim * if not, return 0. *) let rec check lim a = function | b::bs -> let c = sqrt (a *. a +. b *. b) in if a +. b +. c = lim && c = floor c then a *. b *. c else check lim a bs | [] -> 0. ;; (* Given a total and a list of numbers, see if any two numbers in the list * will be the a and b for the equation we want to solve. *) let rec findABC tot = function | a::aas -> let res = check tot a aas in if res = 0. then findABC tot aas else res | [] -> 0. ;; (* Generate floats from n down to m *) let rec floatsN2M n m = if n = m then [] else n::(floatsN2M (n -. 1.) m) ;; (* Find the solution. *) Printf.printf "a * b * c = %f\n" (findABC 1000. (floatsN2M 499. 1.));;

## Excel VBA by mmaster

Runtime: Less than a second

Sub ProblemNine() Dim Limit As Integer Limit = 1000 Dim a, b, c As Integer For a = 1 To Limit - 2 For b = a + 1 To Limit - 1 c = Limit - (a + b) If a ^ 2 + b ^ 2 = c ^ 2 Then MsgBox "a = " & a & ", b = " & b & ", c = " & c & ", T = " & a * b * c Exit Sub End If Next b Next a End Sub