From 71fd0dff3cc0d352b7d42535d654b4fa3f741472 Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Mon, 11 Apr 2022 00:54:45 +0700 Subject: pwc149 solution in go --- challenge-149/pokgopun/README | 1 + challenge-149/pokgopun/go/ch-1.go | 63 +++++++++++++++++++++++++++++++++++++ challenge-149/pokgopun/go/ch-2.go | 66 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 challenge-149/pokgopun/README create mode 100644 challenge-149/pokgopun/go/ch-1.go create mode 100644 challenge-149/pokgopun/go/ch-2.go diff --git a/challenge-149/pokgopun/README b/challenge-149/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-149/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-149/pokgopun/go/ch-1.go b/challenge-149/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..b11bc696e5 --- /dev/null +++ b/challenge-149/pokgopun/go/ch-1.go @@ -0,0 +1,63 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + var n int + if len(os.Args) > 1 { + i, err := strconv.ParseUint(os.Args[1], 10, 32) + if err != nil { + log.Fatal(err) + } + n = int(i) + } else { + n = 20 + } + fds := make([]int, n) + i := 0 + //isFib, t := makeIsFib() + isFib, _ := makeIsFib() + for j := 0; i < n; j++ { + var sum int + if j < 10 { + sum = j + } else { + for _, v := range []byte(strconv.Itoa(j)) { + k, err := strconv.Atoi(string(v)) + if err != nil { + log.Fatal(err) + } + sum += k + } + } + if isFib(sum) { + fds[i] = j + i++ + } + } + fmt.Println(fds) + //t() +} + +func makeIsFib() (func(int) bool, func()) { + m := map[int]bool{ + 0: true, + 1: true, + } + a, b := 1, 1 + return func(n int) bool { + for b < n { + a, b = b, b+a + m[b] = true + } + return m[n] + + }, func() { + fmt.Println(m) + } +} diff --git a/challenge-149/pokgopun/go/ch-2.go b/challenge-149/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..12ab53260d --- /dev/null +++ b/challenge-149/pokgopun/go/ch-2.go @@ -0,0 +1,66 @@ +package main + +import ( + "fmt" + "log" + "math" + "os" + "sort" + "strconv" + "strings" +) + +func main() { + var base []int + if len(os.Args) > 1 { + base = argInts() + } else { + base = []int{2, 4, 10, 12} + } + for _, b := range base { + var str string + for i := b - 1; i >= 0; i-- { + str += strconv.FormatInt(int64(i), b) + } + max, err := strconv.ParseUint(str, b, 64) + //fmt.Println("max non-repeated number in base", b, "is", str, "=>", max) + if err != nil { + log.Fatal(err) + } + maxSqrt := uint64(math.Ceil(math.Sqrt(float64(max)))) + for i := maxSqrt; i > 0; i-- { + str := strconv.FormatUint(i*i, b) + if charIsUnique(str) { + fmt.Printf("f(%v)=\"%v\"\n", b, strings.ToUpper(str)) + break + } + } + } +} + +func charIsUnique(str string) bool { + var seen byte + s := []byte(str) + sort.SliceStable(s, func(i, j int) bool { + return s[i] > s[j] + }) + for i := 0; i < len(s); i++ { + if s[i] == seen { + return false + } + seen = s[i] + } + return true +} +func argInts() (s []int) { + if len(os.Args) > 1 { + for _, v := range os.Args[1:] { + i, err := strconv.Atoi(v) + if err != nil { + log.Fatal(err) + } + s = append(s, i) + } + } + return s +} -- cgit