xah icon

0009 - Project Euler

xah | PRO | 11/23/21 06:56:15 AM UTC | 0 ⭐ | 15360 👁️ | Never ⏰ | []
Go |

770 B

|

None

|

0 👍

/

0 👎

// 0009.go contains some functions to solve ProjectEuler.net's Problem #9
// https://projecteuler.net/problem=9
package main
 
import (
    "fmt"
    "math"
)
 
// Pythagorean triplet returns the pythagorean a, b, c given the sum of a, b, c.
// If the combination does not exist, the result is -1, -1, -1
func PythagoreanTriplet(sum int) (int, int, int) {
    // Initialise variables
    var a2b2, c float64
 
    for i := 1; i <= sum; i++ {
        for j := 1; j <= sum; j++ {
            a2b2 = float64(i*i + j*j)
            c = math.Sqrt(a2b2)
 
            if c == math.Trunc(c) && i+j+int(c) == sum {
                // Combination found, return results
                return i,j,int(c)
            }
        }
    }
    // Nothing found
    return -1, -1, -1
}
 
func main(){
    // Get result
    a,b,c := PythagoreanTriplet(1000)
    fmt.Println(a*b*c)
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎