diff options
| author | Michael Manring <michael@manring> | 2022-04-12 00:39:40 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-04-12 00:39:40 +0700 |
| commit | c524dafc0c4f1d9fe04ce7a7a554dda74d4d7f04 (patch) | |
| tree | 0d3876b2f96d96c9fcadaabd437b6f135f9dfbac /challenge-150 | |
| parent | 635f81890b1d36fd472afbe94df2c7cb0a289d6e (diff) | |
| download | perlweeklychallenge-club-c524dafc0c4f1d9fe04ce7a7a554dda74d4d7f04.tar.gz perlweeklychallenge-club-c524dafc0c4f1d9fe04ce7a7a554dda74d4d7f04.tar.bz2 perlweeklychallenge-club-c524dafc0c4f1d9fe04ce7a7a554dda74d4d7f04.zip | |
pwc150 solution in go
Diffstat (limited to 'challenge-150')
| -rw-r--r-- | challenge-150/pokgopun/README | 1 | ||||
| -rw-r--r-- | challenge-150/pokgopun/go/ch-1.go | 18 | ||||
| -rw-r--r-- | challenge-150/pokgopun/go/ch-2.go | 112 |
3 files changed, 131 insertions, 0 deletions
diff --git a/challenge-150/pokgopun/README b/challenge-150/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-150/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-150/pokgopun/go/ch-1.go b/challenge-150/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..5ff80669f6 --- /dev/null +++ b/challenge-150/pokgopun/go/ch-1.go @@ -0,0 +1,18 @@ +package main + +import ( + "fmt" + "os" +) + +func main() { + a, b := "1234", "5678" + if len(os.Args) > 2 { + a, b = os.Args[1], os.Args[2] + } + fmt.Printf("Input: $a = '%v' $b = '%v'\n", a, b) + for len([]rune(b)) < 51 { + a, b = b, a+b + } + fmt.Println("Output:", string([]rune(b)[50])) +} diff --git a/challenge-150/pokgopun/go/ch-2.go b/challenge-150/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..cacbe2cd46 --- /dev/null +++ b/challenge-150/pokgopun/go/ch-2.go @@ -0,0 +1,112 @@ +package main + +import ( + "fmt" + "log" + "os" + "strconv" +) + +func main() { + l := 500 + if len(os.Args) > 1 { + i, err := strconv.Atoi(os.Args[1]) + if err != nil { + log.Fatal(err) + } + l = i + } + fmt.Println("The smallest positive square-free integers are") + sf := []uint{1} + //fmt.Print("\t1") + chk, nxt, _ := primeUtil() + for i := 2; i <= l; i++ { + if chk(i) { + sf = append(sf, uint(i)) + //fmt.Print(", ", i) + } else { + n := i + p := 2 + seen := 0 + for { + if n%p != 0 { + p = nxt(p) + } else { + if p == seen { + break + } else { + seen = p + } + n /= p + if n == 1 { + sf = append(sf, uint(i)) + //fmt.Print(", ", i) + break + } else if chk(n) { + if n != seen { + sf = append(sf, uint(i)) + //fmt.Print(", ", i) + } + break + } + } + } + } + + } + fmt.Println(sf) + //fmt.Print("\n") +} + +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 +} |
