From 1354cf80de7115334f0f499a0f09bdcc020de0cc Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 14 Mar 2022 18:28:20 +0700 Subject: pwc156 solution in GO --- challenge-156/pokgopun/go/ch-1.go | 74 +++++++++++++++++++++++++++++++++ challenge-156/pokgopun/go/ch-2.go | 87 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 161 insertions(+) create mode 100644 challenge-156/pokgopun/go/ch-1.go create mode 100644 challenge-156/pokgopun/go/ch-2.go diff --git a/challenge-156/pokgopun/go/ch-1.go b/challenge-156/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..44adad9f21 --- /dev/null +++ b/challenge-156/pokgopun/go/ch-1.go @@ -0,0 +1,74 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + n := getArgInt(10) + var ( + ps string + pc int + ) + chk := checkPrime() + for i := 1; i < 10000; i++ { + sum := sumb(i) + //fmt.Println("binary sum of", i, " is", sum) + if chk(sum) { + ps += fmt.Sprint(i, ", ") + pc++ + } + if pc == n { + fmt.Println(ps[:len(ps)-2]) + break + } + } +} +func sumb(i int) (s int) { + for j := i; j > 0; j /= 2 { + s += j % 2 + } + return s +} +func getArgInt(n int) int { + if len(os.Args) > 1 { + u, err := strconv.ParseUint(os.Args[1], 10, 64) + if err != nil { + log.Fatal(err) + } + n = int(u) + } + return n +} +func checkPrime() func(i int) bool { + m := map[int]bool{} + return func(i int) bool { + is, ok := m[i] + if ok { + return is + } else { + if isPrime(i) { + m[i] = true + return true + } + m[i] = false + return false + } + } +} +func isPrime(n int) bool { + if n <= 3 { + return n > 1 + } else if n%2 == 0 || n%3 == 0 { + return false + } + for i := 5; i*i <= n; i += 6 { + if n%i == 0 || n%(i+2) == 0 { + return false + } + } + return true +} diff --git a/challenge-156/pokgopun/go/ch-2.go b/challenge-156/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..b856d58314 --- /dev/null +++ b/challenge-156/pokgopun/go/ch-2.go @@ -0,0 +1,87 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + i := getArgInt(12) + d := getDivisors(i) + sum := 0 + for _, v := range d { + sum += v + } + var o int + ds := []int{} + var sums int + if sum > i { + o = 1 + for j := 2; j < len(d); j++ { + sos := getCombo(j, d) + for _, s := range sos { + var sum int + for _, v := range s { + sum += v + } + if sum == i { + o = 0 + ds = s + sums = sum + goto done + } + } + } + } +done: + fmt.Println("Input: $n =", i) + msg := fmt.Sprint("The proper divisors ", d, " sum to ", sum, " which is ") + if sum > i { + fmt.Print(msg, "greater than $n") + } else { + fmt.Print(msg, "less than $n\n") + } + if sums != 0 { + fmt.Println(", but its subset", ds, "sums to", sums) + } else if o == 1 { + fmt.Println(" and none of its subset sums to", i) + } + fmt.Println("Output:", o) +} +func getArgInt(n int) int { + if len(os.Args) > 1 { + u, err := strconv.ParseUint(os.Args[1], 10, 64) + if err != nil { + log.Fatal(err) + } + n = int(u) + } + return n +} +func getDivisors(n int) (d []int) { + for i := 1; i <= n/2; i++ { + if n%i == 0 { + d = append(d, i) + } + } + return d +} +func getCombo(n int, e []int) (r [][]int) { + c := []int{} + cTree(n, e, c, func(s []int) { + r = append(r, s) + }) + return r +} +func cTree(n int, e []int, c []int, f func(s []int)) { + if len(c) == n || len(c)+len(e) == n { + s := append(c, e...) + f(s[:n]) + } else { + for i := 0; len(c)+len(e)-i >= n; i++ { + cTree(n, e[i+1:], append(c, e[i]), f) + } + } +} -- cgit