Advertisement
xah

0009 - Project Euler

xah
Nov 23rd, 2021
802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Go 0.77 KB | None | 0 0
  1. // 0009.go contains some functions to solve ProjectEuler.net's Problem #9
  2. // https://projecteuler.net/problem=9
  3. package main
  4.  
  5. import (
  6.     "fmt"
  7.     "math"
  8. )
  9.  
  10. // Pythagorean triplet returns the pythagorean a, b, c given the sum of a, b, c.
  11. // If the combination does not exist, the result is -1, -1, -1
  12. func PythagoreanTriplet(sum int) (int, int, int) {
  13.     // Initialise variables
  14.     var a2b2, c float64
  15.  
  16.     for i := 1; i <= sum; i++ {
  17.         for j := 1; j <= sum; j++ {
  18.             a2b2 = float64(i*i + j*j)
  19.             c = math.Sqrt(a2b2)
  20.  
  21.             if c == math.Trunc(c) && i+j+int(c) == sum {
  22.                 // Combination found, return results
  23.                 return i,j,int(c)
  24.             }
  25.         }
  26.     }
  27.     // Nothing found
  28.     return -1, -1, -1
  29. }
  30.  
  31. func main(){
  32.     // Get result
  33.     a,b,c := PythagoreanTriplet(1000)
  34.     fmt.Println(a*b*c)
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement