From 6cb1c64612605961d54da2cdfdccf4ac1b59501c Mon Sep 17 00:00:00 2001 From: Michael Manring Date: Sun, 31 Jul 2022 14:08:23 +0700 Subject: pwc139 solution in go --- challenge-139/pokgopun/README | 1 + challenge-139/pokgopun/go/ch-1.go | 51 +++++++++++++++++++++++++ challenge-139/pokgopun/go/ch-2.go | 78 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 130 insertions(+) create mode 100644 challenge-139/pokgopun/README create mode 100644 challenge-139/pokgopun/go/ch-1.go create mode 100644 challenge-139/pokgopun/go/ch-2.go diff --git a/challenge-139/pokgopun/README b/challenge-139/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-139/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-139/pokgopun/go/ch-1.go b/challenge-139/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..c5c24b9c86 --- /dev/null +++ b/challenge-139/pokgopun/go/ch-1.go @@ -0,0 +1,51 @@ +/* https://theweeklychallenge.org/blog/perl-weekly-challenge-139/ + +TASK #1 › JortSort + +Submitted by: [41]Mohammad S Anwar + __________________________________________________________________ + + You are given a list of numbers. + + Write a script to implement JortSort. It should return true/false + depending if the given list of numbers are already sorted. + +Example 1: + +Input: @n = (1,2,3,4,5) +Output: 1 + +Since the array is sorted, it prints 1. + +Example 2: + +Input: @n = (1,3,2,4,5) +Output: 0 + +Since the array is NOT sorted, it prints 0. + +*/ +package main + +import ( + "fmt" + "log" + "os" + "sort" + "strconv" +) + +func main() { + var n []int + if len(os.Args) <= 1 { + log.Fatal("please provide integers as arguments") + } + for _, v := range os.Args[1:] { + i, err := strconv.Atoi(v) + if err != nil { + log.Fatal(err) + } + n = append(n, i) + } + fmt.Printf("Input: n = %v\nOutput: %t\n", n, sort.IntsAreSorted(n)) +} diff --git a/challenge-139/pokgopun/go/ch-2.go b/challenge-139/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..84de0d6f31 --- /dev/null +++ b/challenge-139/pokgopun/go/ch-2.go @@ -0,0 +1,78 @@ +/* https://theweeklychallenge.org/blog/perl-weekly-challenge-139/ + +TASK #2 › Long Primes + +Submitted by: [42]Mohammad S Anwar + __________________________________________________________________ + + Write a script to generate first 5 Long Primes. + + A prime number (p) is called Long Prime if (1/p) has an infinite + decimal expansion repeating every (p-1) digits. + +Example + +7 is a long prime since 1/7 = 0.142857142857... +The repeating part (142857) size is 6 i.e. one less than the prime number 7. + +Also 17 is a long prime since 1/17 = 0.05882352941176470588235294117647... +The repeating part (0588235294117647) size is 16 i.e. one less than the prime nu +mber 17. + +Another example, 2 is not a long prime as 1/2 = 0.5. +There is no repeating part in this case. + +*/ +package main + +import ( + "fmt" + "log" + "math/big" + "os" + "strconv" + "strings" + + "github.com/jbarham/primegen" +) + +func main() { + var n uint = 5 + fmt.Sscanf(strings.Join(os.Args[1:], " "), "%d", &n) + pg := primegen.New() + pg.SkipTo(3) + var p uint64 + var strb strings.Builder + k := n + for k > 0 { + p = pg.Next() + if 10%p == 0 { + continue + } + b, ok := new(big.Int).SetString(fmt.Sprintf("1%0[1]*d", p-1, 0), 10) + if !ok { + log.Fatal("failed on SetString()") + } + b.Sub(b, big.NewInt(1)) + str := fmt.Sprintf("%0[1]*v", p-1, b.Div(b, big.NewInt(int64(p))).String()) + l := len(str) + if str[:l/2] == str[l/2:l] { + goto skip + } + for i := 4; i <= l; i += 2 { + if str[:i/2] == str[i/2:i] { + goto skip + } + } + //fmt.Printf("%d => 0.%s\n", p, str) + strb.WriteString(", " + strconv.FormatUint(p, 10)) + k-- + skip: + } + res := strb.String()[2:] + fmt.Println(res) + /* + ans := "7, 17, 19, 23, 29, 47, 59, 61, 97, 109, 113, 131, 149, 167, 179, 181, 193, 223, 229, 233, 257, 263, 269, 313, 337, 367, 379, 383, 389, 419, 433, 461, 487, 491, 499, 503, 509, 541, 571, 577, 593, 619, 647, 659, 701, 709, 727, 743, 811, 821, 823, 857, 863, 887, 937, 941, 953, 971, 977, 983" + fmt.Printf("it is %t that first %d long prime(s) = %s\n", res == strings.Join(strings.Split(ans, ", ")[:n], ", "), n, res) + */ +} -- cgit