aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Lee Firkins <michael@firkins>2022-04-05 19:34:18 +0700
committerMichael Lee Firkins <michael@firkins>2022-04-05 19:44:04 +0700
commitc5c0bcb820dbf29e576f4ece23868b050450a3d4 (patch)
treead4927cb70e0ca7bce583ee1b15dfcff6ae81fcb
parente408fdd922b96e10a1e1ee05be6f036ef5404d3e (diff)
downloadperlweeklychallenge-club-c5c0bcb820dbf29e576f4ece23868b050450a3d4.tar.gz
perlweeklychallenge-club-c5c0bcb820dbf29e576f4ece23868b050450a3d4.tar.bz2
perlweeklychallenge-club-c5c0bcb820dbf29e576f4ece23868b050450a3d4.zip
pwc159 solution in go
-rw-r--r--challenge-159/pokgopun/go/ch-1.go44
-rw-r--r--challenge-159/pokgopun/go/ch-2.go124
2 files changed, 168 insertions, 0 deletions
diff --git a/challenge-159/pokgopun/go/ch-1.go b/challenge-159/pokgopun/go/ch-1.go
new file mode 100644
index 0000000000..719d6a8953
--- /dev/null
+++ b/challenge-159/pokgopun/go/ch-1.go
@@ -0,0 +1,44 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ s := argInts()
+ if len(s) == 0 {
+ s = []int{5, 7, 4}
+ }
+ for _, n := range s {
+ fmt.Println("Input: $n =", n)
+ a, b, c, d := 0, 1, 1, n
+ fmt.Printf("Output: %d/%d, %d/%d", a, b, c, d)
+ if n > 1 {
+ for {
+ k := (n + b) / d
+ a, b, c, d = c, d, k*c-a, k*d-b
+ fmt.Printf(", %d/%d", c, d)
+ if c == 1 && d == 1 {
+ break
+ }
+ }
+ }
+ fmt.Printf("\n\n")
+ }
+}
+
+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
+}
diff --git a/challenge-159/pokgopun/go/ch-2.go b/challenge-159/pokgopun/go/ch-2.go
new file mode 100644
index 0000000000..d61f36a7aa
--- /dev/null
+++ b/challenge-159/pokgopun/go/ch-2.go
@@ -0,0 +1,124 @@
+package main
+
+import (
+ "fmt"
+ "log"
+ "os"
+ "strconv"
+)
+
+func main() {
+ s := argInts()
+ if len(s) == 0 {
+ s = []int{5, 10, 20}
+ }
+ for _, n := range s {
+ fmt.Println("Input: $n =", n)
+ m := map[int]int{}
+ for _, v := range factor(n) {
+ m[v]++
+ }
+ //fmt.Println(m)
+ o := -1
+ if len(m)%2 == 0 {
+ o = 1
+ }
+ for _, v := range m {
+ if v > 1 {
+ o = 0
+ break
+ }
+ }
+ fmt.Printf("Output: %d\n\n", o)
+ }
+}
+
+func factor(n int) (s []int) {
+ if n > 1 {
+ chk, nxt, _ := primeUtil()
+ p := 2
+ if chk(n) {
+ s = append(s, n)
+ } else {
+ for {
+ if n%p != 0 {
+ p = nxt(p)
+ } else {
+ s = append(s, p)
+ n /= p
+ if n == 1 {
+ break
+ } else if chk(n) {
+ s = append(s, n)
+ break
+ }
+ }
+ }
+ }
+ }
+ return s
+}
+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
+}
+func primeUtil() (func(i int) bool, func(i int) int, func()) {
+ m := map[int]bool{}
+ return func(i int) bool {
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check prime from cache ###")
+ return is
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return true
+ }
+ m[i] = false
+ return false
+ }
+ }, func(i int) int {
+ for {
+ i++
+ is, ok := m[i]
+ if ok {
+ //fmt.Println("### check next number from cache ###")
+ if is {
+ return i
+ } else {
+ continue
+ }
+ } else {
+ if isPrime(i) {
+ m[i] = true
+ return i
+ }
+ m[i] = false
+ }
+ }
+ }, func() {
+ fmt.Println("=>", m)
+ }
+}
+func isPrime(n int) bool {
+ //fmt.Println("In isPrime")
+ 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
+}