diff options
| author | Mohammad S Anwar <Mohammad.Anwar@yahoo.com> | 2022-06-05 15:06:50 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-06-05 15:06:50 +0100 |
| commit | 4312a5e8f3b559d045febb81f8b5d27b3e7844e2 (patch) | |
| tree | 1be815355f3444ecd528af98859d16ff0f12140c | |
| parent | 20edd19cb6aaf0443b4558f1ad6bda0ec3b45bce (diff) | |
| parent | 093a4b73de288e182fcf269d133ec0c923a80d1d (diff) | |
| download | perlweeklychallenge-club-4312a5e8f3b559d045febb81f8b5d27b3e7844e2.tar.gz perlweeklychallenge-club-4312a5e8f3b559d045febb81f8b5d27b3e7844e2.tar.bz2 perlweeklychallenge-club-4312a5e8f3b559d045febb81f8b5d27b3e7844e2.zip | |
Merge pull request #6199 from pokgopun/pwc148
Pwc148
8 files changed, 403 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..bd2adb804b --- /dev/null +++ b/challenge-148/pokgopun/go/ch-2.go @@ -0,0 +1,202 @@ +// 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" + "log" + "math" + "os" + "sort" + "strconv" + "strings" +) + +func main() { + var i uint = 2 + if len(os.Args) > 1 { + n, err := strconv.ParseUint(os.Args[1], 10, 64) + if err != nil { + log.Fatal(err) + } + i = uint(n) + } + cdn := newCdn3(i) + 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 (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) { + t1 := int(k + 1) + t2 := int(8*k + 5) + t := t1 * t1 * t2 + s = append(s, [2]int{1, t}) + factor := make(map[int]int) + if cdn.pmap[t2] { + if t1 == 1 { + return s + } + if cdn.pmap[t1] { + s = append(s, [2]int{t1, t2}) + return s + } else { + factor[t2] += 1 + } + } else { + for _, v := range cdn.factor(t2) { + factor[v] += 1 + } + } + if cdn.pmap[t1] { + factor[t1] += 2 + } else { + for _, v := range cdn.factor(t1) { + factor[v] += 2 + } + } + var pair []uint + for k, v := range factor { + if v >= 2 { + pair = append(pair, uint(k)) + } + } + sort.SliceStable(pair, func(i, j int) bool { + return pair[i] < pair[j] + }) + for i := 1; i <= len(pair); i++ { + getC, _ := getCombo(i, pair) + for cmb := range getC { + res := make(chan int) + go func(cmb []uint) { + findB(cmb, factor, 1, res) + close(res) + }(cmb) + for v := range res { + s = append(s, [2]int{v, t / (v * v)}) + } + } + } + return s +} + +func findB(s []uint, m map[int]int, b int, res chan<- int) { + if len(s) == 0 { + res <- b + return + } + for i := 1; i <= m[int(s[0])]/2; i++ { + b *= int(s[0]) + findB(s[1:], m, b, res) + } +} + +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() +} +func getCombo(r int, e []uint) (<-chan []uint, func()) { + c := []uint{} + res := make(chan []uint, 50) + done := make(chan struct{}) + go func() { + cTree(r, e, c, res, done) + close(res) + }() + return res, func() { + close(done) + } +} + +func cTree(r int, e []uint, c []uint, res chan<- []uint, done chan struct{}) { + if len(c) == r || len(c)+len(e) == r { + s := make([]uint, r) + l := copy(s, c) + if l < r { + copy(s[l:], e) + } + select { + case <-done: + return + case res <- s: + } + } else { + for i := 0; len(c)+len(e)-i >= r; i++ { + cTree(r, e[i+1:], append(c, e[i]), res, done) + } + } +} 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 |
