Initial upload

This commit is contained in:
2022-08-24 14:28:45 +02:00
parent c67653ddee
commit 57bc7b0289
370 changed files with 18479 additions and 0 deletions

View File

@@ -0,0 +1,32 @@
{
"blurb": "There exists exactly one Pythagorean triplet for which a + b + c = 1000. Find the product a * b * c.",
"authors": [
"soniakeys"
],
"contributors": [
"alebaffa",
"bitfield",
"ekingery",
"ferhatelmas",
"hilary",
"kytrinyx",
"leenipper",
"petertseng",
"robphoenix",
"sebito91",
"tleen"
],
"files": {
"solution": [
"pythagorean_triplet.go"
],
"test": [
"pythagorean_triplet_test.go"
],
"example": [
".meta/example.go"
]
},
"source": "Problem 9 at Project Euler",
"source_url": "http://projecteuler.net/problem=9"
}

View File

@@ -0,0 +1 @@
{"track":"go","exercise":"pythagorean-triplet","id":"0e897c0fbe50436d8a7e3f121f06ac6d","url":"https://exercism.org/tracks/go/exercises/pythagorean-triplet","handle":"halfdan","is_requester":true,"auto_approve":false}

View File

@@ -0,0 +1,40 @@
# Help
## Running the tests
To run the tests run the command `go test` from within the exercise directory.
If the test suite contains benchmarks, you can run these with the `--bench` and `--benchmem`
flags:
go test -v --bench . --benchmem
Keep in mind that each reviewer will run benchmarks on a different machine, with
different specs, so the results from these benchmark tests may vary.
## Submitting your solution
You can submit your solution using the `exercism submit pythagorean_triplet.go` command.
This command will upload your solution to the Exercism website and print the solution page's URL.
It's possible to submit an incomplete solution which allows you to:
- See how others have completed the exercise
- Request help from a mentor
## Need to get help?
If you'd like help solving the exercise, check the following pages:
- The [Go track's documentation](https://exercism.org/docs/tracks/go)
- [Exercism's support channel on gitter](https://gitter.im/exercism/support)
- The [Frequently Asked Questions](https://exercism.org/docs/using/faqs)
Should those resources not suffice, you could submit your (incomplete) solution to request mentoring.
To get help if you're having trouble, you can use one of the following resources:
- [How to Write Go Code](https://golang.org/doc/code.html)
- [Effective Go](https://golang.org/doc/effective_go.html)
- [Go Resources](http://golang.org/help)
- [StackOverflow](http://stackoverflow.com/questions/tagged/go)

View File

@@ -0,0 +1,53 @@
# Pythagorean Triplet
Welcome to Pythagorean Triplet on Exercism's Go Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
## Instructions
A Pythagorean triplet is a set of three natural numbers, {a, b, c}, for
which,
```text
a² + b² = c²
```
and such that,
```text
a < b < c
```
For example,
```text
3² + 4² = 9 + 16 = 25 = 5².
```
Given an input integer N, find all Pythagorean triplets for which `a + b + c = N`.
For example, with N = 1000, there is exactly one Pythagorean triplet for which `a + b + c = 1000`: `{200, 375, 425}`.
## Source
### Created by
- @soniakeys
### Contributed to by
- @alebaffa
- @bitfield
- @ekingery
- @ferhatelmas
- @hilary
- @kytrinyx
- @leenipper
- @petertseng
- @robphoenix
- @sebito91
- @tleen
### Based on
Problem 9 at Project Euler - http://projecteuler.net/problem=9

View File

@@ -0,0 +1,3 @@
module pythagorean
go 1.13

View File

@@ -0,0 +1,39 @@
package pythagorean
import "math"
type Triplet [3]int
func isTriplet(a, b, c int) bool {
return a*a + b*b == c*c && a < b && b < c
}
func Range(min, max int) []Triplet {
var triplets = []Triplet{}
for c := min; c <= max; c++ {
for b := min; b <= c - 1; b++ {
a := int(math.Sqrt(float64(c*c - b*b)))
if isTriplet(a, b, c) && a >= min {
triplets = append(triplets, Triplet{a,b,c})
}
}
}
return triplets
}
func Sum(p int) []Triplet {
temp_triplets := Range(1, p / 2)
triplets := make([]Triplet, 0, len(temp_triplets))
for _, triplet := range temp_triplets {
if triplet[0] + triplet[1] + triplet[2] == p {
triplets = append(triplets, triplet)
}
}
// Reverse slice..
for i, j := 0, len(triplets)-1; i < j; i, j = i+1, j-1 {
triplets[i], triplets[j] = triplets[j], triplets[i]
}
return triplets
}

View File

@@ -0,0 +1,60 @@
package pythagorean
import (
"reflect"
"testing"
)
var rangeTests = []struct {
min, max int
ts []Triplet
}{
{1, 10, []Triplet{{3, 4, 5}, {6, 8, 10}}},
{11, 20, []Triplet{{12, 16, 20}}},
}
func TestRange(t *testing.T) {
for _, test := range rangeTests {
ts := Range(test.min, test.max)
if !reflect.DeepEqual(ts, test.ts) {
t.Fatalf("Range(%d, %d) = %v, want %v",
test.min, test.max, ts, test.ts)
}
}
}
var sumTests = []struct {
sum int
ts []Triplet
}{
{180, []Triplet{{18, 80, 82}, {30, 72, 78}, {45, 60, 75}}},
{1000, []Triplet{{200, 375, 425}}},
}
func TestSum(t *testing.T) {
for _, test := range sumTests {
ts := Sum(test.sum)
if !reflect.DeepEqual(ts, test.ts) {
t.Fatalf("Sum(%d) = %v, want %v",
test.sum, ts, test.ts)
}
}
}
func BenchmarkRange(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
Range(1, 100)
}
}
func BenchmarkSum(b *testing.B) {
if testing.Short() {
b.Skip("skipping benchmark in short mode.")
}
for i := 0; i < b.N; i++ {
Sum(1000)
}
}