From ada902ca3d6cf24b4bcea11ff18b1b597d696d83 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Sun, 5 Jun 2022 23:52:14 +0700 Subject: pwc146 solution in go --- challenge-146/pokgopun/go/ch-1.go | 51 +++++++++++++++++++++++++++++++++++++++ challenge-146/pokgopun/go/ch-2.go | 36 +++++++++++++++++++++++++++ 2 files changed, 87 insertions(+) create mode 100644 challenge-146/pokgopun/go/ch-1.go create mode 100644 challenge-146/pokgopun/go/ch-2.go diff --git a/challenge-146/pokgopun/go/ch-1.go b/challenge-146/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..3803018e49 --- /dev/null +++ b/challenge-146/pokgopun/go/ch-1.go @@ -0,0 +1,51 @@ +// From https://math.stackexchange.com/questions/1885095/parametrization-of-cardano-triplet +// a = 3k + 2 +// b^2 * c = (k+1)^2 * (8k + 5) +package main + +import ( + "fmt" + "log" + "math" + "os" + "strconv" +) + +func main() { + n := 10001 + if len(os.Args) > 1 { + r, err := strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatal(err) + } + n = r + } + for j := 50_000; j <= 1_000_000; j += 50_000 { + var k int + for i, v := range pmap(uint(j)) { + if v { + k++ + if k == n { + fmt.Println(i) + goto done + } + } + } + } +done: +} +func pmap(n uint) []bool { + m := make([]bool, int(n)+1) + for i := 2; i <= int(n); i++ { + m[i] = true + } + for i := 2; float64(i) <= math.Sqrt(float64(n)); i++ { + j := i * i + for j <= int(n) { + m[j] = false + j += i + } + + } + return m +} diff --git a/challenge-146/pokgopun/go/ch-2.go b/challenge-146/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..b8aea49f0f --- /dev/null +++ b/challenge-146/pokgopun/go/ch-2.go @@ -0,0 +1,36 @@ +package main + +import ( + "fmt" + "log" + "os" +) + +func main() { + samples := [][2]uint{ + [2]uint{3, 5}, + [2]uint{4, 3}, + } + if len(os.Args) > 1 { + var sample [2]uint + _, err := fmt.Sscanf(os.Args[1], "%d/%d", &sample[0], &sample[1]) + if err != nil { + log.Fatal(err) + } + samples = [][2]uint{sample} + } + for _, v := range samples { + p := parent(v) + gp := parent(p) + fmt.Printf("Input: member = '%d/%d'\nOutput: parent ='%d/%d' and grandparent = '%d/%d'\n", v[0], v[1], p[0], p[1], gp[0], gp[1]) + } +} + +func parent(s [2]uint) [2]uint { + if s[0] > s[1] { + return [2]uint{s[0] - s[1], s[1]} + } else if s[0] < s[1] { + return [2]uint{s[0], s[1] - s[0]} + } + return [2]uint{1, 1} +} -- cgit