diff options
| author | Michael Manring <michael@manring> | 2022-04-14 00:56:30 +0700 |
|---|---|---|
| committer | Michael Manring <michael@manring> | 2022-06-04 11:58:41 +0700 |
| commit | 57bbd26fc8a8075c5eeb66cbadb2d3650cfd7942 (patch) | |
| tree | 0b87deaf554b11fdfda8b6d40150553ac0f96a77 | |
| parent | 72ba70a96cfd587443c3eaa0f5ba02e3557f4b82 (diff) | |
| download | perlweeklychallenge-club-57bbd26fc8a8075c5eeb66cbadb2d3650cfd7942.tar.gz perlweeklychallenge-club-57bbd26fc8a8075c5eeb66cbadb2d3650cfd7942.tar.bz2 perlweeklychallenge-club-57bbd26fc8a8075c5eeb66cbadb2d3650cfd7942.zip | |
pwc148 solution in go
8 files changed, 353 insertions, 0 deletions
diff --git a/challenge-148/pokgopun/README b/challenge-148/pokgopun/README new file mode 100644 index 0000000000..33dfd303a4 --- /dev/null +++ b/challenge-148/pokgopun/README @@ -0,0 +1 @@ +Solution by PokGoPun diff --git a/challenge-148/pokgopun/go/ch-1.go b/challenge-148/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..d387fd2e5b --- /dev/null +++ b/challenge-148/pokgopun/go/ch-1.go @@ -0,0 +1,19 @@ +package main + +import ( + "fmt" + "regexp" + + "github.com/divan/num2words" +) + +func main() { + eban := []int{} + re := regexp.MustCompile(`[eE]`) + for i := 1; i <= 100; i++ { + if !re.MatchString(num2words.Convert(i)) { + eban = append(eban, i) + } + } + fmt.Println(eban) +} diff --git a/challenge-148/pokgopun/go/ch-2.go b/challenge-148/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..5c3c3bb22d --- /dev/null +++ b/challenge-148/pokgopun/go/ch-2.go @@ -0,0 +1,152 @@ +// From https://math.stackexchange.com/questions/1885095/parametrization-of-cardano-triplet +// a = 3k + 2 +// b^2 * c = (k+1)^2 * (8k + 5) +package main + +import ( + "fmt" + "math" + "sort" + "strings" +) + +func main() { + cdn := newCdn3(400) + fmt.Println(cdn) +} + +func pmap(n uint) []bool { + m := make([]bool, int(n)+1) + for i := 2; i <= int(n); i++ { + m[i] = true + } + for i := 2; float64(i) <= math.Sqrt(float64(n)); i++ { + j := i * i + for j <= int(n) { + m[j] = false + j += i + } + + } + return m +} + +type cdn3 struct { + k uint + pmap []bool + plist []int +} + +func newCdn3(n uint) (c cdn3) { + c.k = n + // create prime map as large as sqrt of (8k + 5) which is the term we need to do factoriing + //c.pmap = pmap(uint(math.Floor(math.Sqrt(float64(8*n) + 5)))) + // create prime map as large as (8k + 5) to make it easy to check prime for factoring the number + c.pmap = pmap(uint((8 * n) + 5)) + for i := 2; i < len(c.pmap); i++ { + if c.pmap[i] { + c.plist = append(c.plist, i) + } + } + return c +} + +func (cdn cdn3) a(k uint) int { + return int(3*k + 2) +} + +func (cdn cdn3) bc(k uint) (s [][2]int) { + b := int(k + 1) + c := int(8*k + 5) + //bIsPrime := big.NewInt(int64(b)).ProbablyPrime(10) + //cIsPrime := big.NewInt(int64(c)).ProbablyPrime(10) + s = append(s, [2]int{1, b * b * c}) + factor := []int{} + if cdn.pmap[c] { + if b == 1 { + return s + } + if cdn.pmap[b] { + s = append(s, [2]int{b, c}) + return s + } else { + factor = append(factor, cdn.factor(b)...) + factor = append(factor, factor...) + factor = append(factor, c) + } + } else { + if cdn.pmap[b] { + factor = append(factor, b, b) + } else { + factor = append(factor, cdn.factor(b)...) + factor = append(factor, factor...) + } + factor = append(factor, cdn.factor(c)...) + } + sort.SliceStable(factor, func(i, j int) bool { + return factor[i] < factor[j] + }) + fmt.Println("b =", b, "c =", c, "factor =>", factor, len(factor)) + var ( + prev int + pair, lone []int + ) + for _, v := range factor { + if prev == v { + pair = append(pair, v) + prev = 0 + } else if prev == 0 { + prev = v + } else { + lone = append(lone, prev) + prev = v + } + } + if prev != 0 { + lone = append(lone, prev) + } + fmt.Println("factor =>", "pair =", pair, "lone =", lone) + return s +} + +func (cdn cdn3) factor(n int) (s []int) { + if n > 1 { + if cdn.pmap[n] { + return []int{n} + } + i := 0 + for { + if n%cdn.plist[i] != 0 { + i++ + } else { + s = append(s, cdn.plist[i]) + n /= cdn.plist[i] + if n == 1 { + break + } else if cdn.pmap[n] { + s = append(s, n) + break + } + } + } + } + return s +} + +func (cdn cdn3) abc(k uint) (s [][3]int) { + for _, v := range cdn.bc(k) { + s = append(s, [3]int{cdn.a(k), v[0], v[1]}) + } + return s +} + +func (cdn cdn3) String() string { + var b strings.Builder + for k := 0; k <= int(cdn.k); k++ { + for _, v := range cdn.abc(uint(k)) { + //fmt.Println(v) + b.WriteString(fmt.Sprintln(v)) + } + } + return b.String() +} diff --git a/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/.travis.yml b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/.travis.yml new file mode 100644 index 0000000000..03ca914415 --- /dev/null +++ b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/.travis.yml @@ -0,0 +1,8 @@ +language: go +go: + - 1.8.3 + - 1.9 + + +script: + - go test -v . diff --git a/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/LICENSE b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/LICENSE new file mode 100644 index 0000000000..635f49f003 --- /dev/null +++ b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/LICENSE @@ -0,0 +1,20 @@ +The MIT License (MIT) + +Copyright (c) 2013 Ivan Daniluk + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/README.md b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/README.md new file mode 100644 index 0000000000..8197c11925 --- /dev/null +++ b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/README.md @@ -0,0 +1,29 @@ +num2words +========= + +[](https://travis-ci.org/divan/num2words) +[](https://godoc.org/github.com/divan/num2words) + +num2words - Numbers to words converter in Go (Golang) + +## Usage + +First, import package num2words + +```import github.com/divan/num2words``` + +Convert number +```go + str := num2words.Convert(17) // outputs "seventeen" + ... + str := num2words.Convert(1024) // outputs "one thousand twenty four" + ... + str := num2words.Convert(-123) // outputs "minus one hundred twenty three" +``` + +Convert number with " and " between number groups: +```go + str := num2words.ConvertAnd(514) // outputs "five hundred and fourteen" + ... + str := num2words.ConvertAnd(123) // outputs "one hundred and twenty three" +``` diff --git a/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/num2words.go b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/num2words.go new file mode 100644 index 0000000000..dc86854c6b --- /dev/null +++ b/challenge-148/pokgopun/go/vendor/github.com/divan/num2words/num2words.go @@ -0,0 +1,119 @@ +// Package num2words implements numbers to words converter. +package num2words + +import "math" + +// how many digit's groups to process +const groupsNumber int = 4 + +var _smallNumbers = []string{ + "zero", "one", "two", "three", "four", + "five", "six", "seven", "eight", "nine", + "ten", "eleven", "twelve", "thirteen", "fourteen", + "fifteen", "sixteen", "seventeen", "eighteen", "nineteen", +} +var _tens = []string{ + "", "", "twenty", "thirty", "forty", "fifty", + "sixty", "seventy", "eighty", "ninety", +} +var _scaleNumbers = []string{ + "", "thousand", "million", "billion", +} + +type digitGroup int + +// Convert converts number into the words representation. +func Convert(number int) string { + return convert(number, false) +} + +// ConvertAnd converts number into the words representation +// with " and " added between number groups. +func ConvertAnd(number int) string { + return convert(number, true) +} + +func convert(number int, useAnd bool) string { + // Zero rule + if number == 0 { + return _smallNumbers[0] + } + + // Divide into three-digits group + var groups [groupsNumber]digitGroup + positive := math.Abs(float64(number)) + + // Form three-digit groups + for i := 0; i < groupsNumber; i++ { + groups[i] = digitGroup(math.Mod(positive, 1000)) + positive /= 1000 + } + + var textGroup [groupsNumber]string + for i := 0; i < groupsNumber; i++ { + textGroup[i] = digitGroup2Text(groups[i], useAnd) + } + combined := textGroup[0] + and := useAnd && (groups[0] > 0 && groups[0] < 100) + + for i := 1; i < groupsNumber; i++ { + if groups[i] != 0 { + prefix := textGroup[i] + " " + _scaleNumbers[i] + + if len(combined) != 0 { + prefix += separator(and) + } + + and = false + + combined = prefix + combined + } + } + + if number < 0 { + combined = "minus " + combined + } + + return combined +} + +func intMod(x, y int) int { + return int(math.Mod(float64(x), float64(y))) +} + +func digitGroup2Text(group digitGroup, useAnd bool) (ret string) { + hundreds := group / 100 + tensUnits := intMod(int(group), 100) + + if hundreds != 0 { + ret += _smallNumbers[hundreds] + " hundred" + + if tensUnits != 0 { + ret += separator(useAnd) + } + } + + tens := tensUnits / 10 + units := intMod(tensUnits, 10) + + if tens >= 2 { + ret += _tens[tens] + + if units != 0 { + ret += "-" + _smallNumbers[units] + } + } else if tensUnits != 0 { + ret += _smallNumbers[tensUnits] + } + + return +} + +// separator returns proper separator string between +// number groups. +func separator(useAnd bool) string { + if useAnd { + return " and " + } + return " " +} diff --git a/challenge-148/pokgopun/go/vendor/modules.txt b/challenge-148/pokgopun/go/vendor/modules.txt new file mode 100644 index 0000000000..a8da285050 --- /dev/null +++ b/challenge-148/pokgopun/go/vendor/modules.txt @@ -0,0 +1,5 @@ +# github.com/divan/num2words v0.0.0-20170904212200-57dba452f942 +## explicit +github.com/divan/num2words +# github.com/smartystreets/goconvey v1.7.2 +## explicit; go 1.16 |
